Class: Schematic::RulesEngine
- Inherits:
-
Object
- Object
- Schematic::RulesEngine
- Defined in:
- lib/schematic/rules_engine.rb
Constant Summary collapse
- WASM_PATH =
File.join(__dir__, "wasm", "rulesengine.wasm")
Instance Method Summary collapse
- #check_flag(flag, company = nil, user = nil) ⇒ Object
-
#initialize(logger: nil) ⇒ RulesEngine
constructor
A new instance of RulesEngine.
- #initialize! ⇒ Object
- #initialized? ⇒ Boolean
- #version_key ⇒ Object
Constructor Details
#initialize(logger: nil) ⇒ RulesEngine
Returns a new instance of RulesEngine.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/schematic/rules_engine.rb', line 9 def initialize(logger: nil) @logger = logger || ConsoleLogger.new @initialized = false @mutex = Mutex.new @store = nil @memory = nil @alloc_fn = nil @dealloc_fn = nil @check_flag_fn = nil @set_time_fn = nil @get_result_json_fn = nil @get_result_json_length_fn = nil @get_version_key_fn = nil end |
Instance Method Details
#check_flag(flag, company = nil, user = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/schematic/rules_engine.rb', line 75 def check_flag(flag, company = nil, user = nil) raise "WASM rules engine not initialized" unless @initialized # Build combined JSON envelope (same format as Python/C#) envelope = { flag: strip_nulls(flag) } envelope[:company] = strip_nulls(company) if company envelope[:user] = strip_nulls(user) if user json_bytes = JSON.generate(envelope).encode("UTF-8") @mutex.synchronize do ptr = @alloc_fn.call(json_bytes.bytesize) begin @memory.write(ptr, json_bytes) # Supply the host's current time so the engine can compute # metric-period reset timestamps (SCHY-471). No-op on older wasm. @set_time_fn&.call((Time.now.to_f * 1000).to_i) result_len = @check_flag_fn.call(ptr, json_bytes.bytesize) raise "WASM checkFlagCombined returned error" if result_len.negative? result_ptr = @get_result_json_fn.call actual_len = @get_result_json_length_fn.call result_json = @memory.read(result_ptr, actual_len).force_encoding("UTF-8") normalize_keys(JSON.parse(result_json, symbolize_names: true)) ensure @dealloc_fn.call(ptr, json_bytes.bytesize) end end rescue StandardError => e @logger.error("WASM check_flag failed: #{e.}") raise end |
#initialize! ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/schematic/rules_engine.rb', line 24 def initialize! @mutex.synchronize do return if @initialized unless File.exist?(WASM_PATH) @logger.warn("WASM binary not found at #{WASM_PATH}. Run scripts/download-wasm.sh to fetch it.") return end require "wasmtime" engine = Wasmtime::Engine.new mod = Wasmtime::Module.from_file(engine, WASM_PATH) linker = Wasmtime::Linker.new(engine) wasi_config = Wasmtime::WasiConfig.new wasi_config.set_stdin_string("") @store = Wasmtime::Store.new(engine, wasi_config: wasi_config) @instance = linker.instantiate(@store, mod) # Get raw WASM exports (C ABI — no wasm-bindgen) @memory = @instance.export("memory").to_memory @alloc_fn = export_func("alloc") @dealloc_fn = export_func("dealloc") @check_flag_fn = export_func("checkFlagCombined") # Optional: feed the wasm the current wall-clock time before each check. # The raw wasm32-unknown-unknown build has no clock under wasmtime # (SCHY-471); without this, metric-period reset timestamps are omitted. # Absent on older wasm — hence the safe-navigation call below. @set_time_fn = @instance.export("setCurrentTimeMillis")&.to_func @get_result_json_fn = export_func("getResultJson") @get_result_json_length_fn = export_func("getResultJsonLength") @get_version_key_fn = export_func("get_version_key_wasm") @initialized = true @logger.debug("WASM rules engine initialized") rescue LoadError => e @logger.warn("wasmtime gem not available, WASM rules engine disabled: #{e.}") @initialized = false rescue StandardError => e @logger.error("Failed to initialize WASM rules engine: #{e.}") @initialized = false end end |
#initialized? ⇒ Boolean
71 72 73 |
# File 'lib/schematic/rules_engine.rb', line 71 def initialized? @initialized end |
#version_key ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/schematic/rules_engine.rb', line 112 def version_key raise "WASM rules engine not initialized" unless @initialized @mutex.synchronize do ptr = @get_version_key_fn.call # Read null-terminated string from WASM memory raw = @memory.read(ptr, 256) null_idx = raw.index("\0") || raw.bytesize raw[0, null_idx].force_encoding("UTF-8") end end |