Class: DevCycle::LocalBucketing
- Inherits:
-
Object
- Object
- DevCycle::LocalBucketing
- Extended by:
- T::Sig
- Defined in:
- lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb
Constant Summary collapse
- @@rand =
Random.new(seed = Random.new_seed)
- @@engine =
Wasmtime::Engine.new
- @@wasmmodule =
Wasmtime::Module.from_file(@@engine, "#{__dir__}/bucketing-lib.release.wasm")
- @@wasi_ctx =
Wasmtime::WasiCtxBuilder.new .inherit_stdout .inherit_stderr .set_argv(ARGV) .set_env(ENV)
- @@store =
Wasmtime::Store.new(@@engine, wasi_ctx: @@wasi_ctx)
- @@linker =
Wasmtime::Linker.new(@@engine, wasi: true)
- @@stack_tracer_raise =
lambda { || raise }
- @@stack_tracer =
each method reassigns stack_tracer so the call stack is properly displayed
lambda {}
- @@instance =
@@linker.instantiate(@@store, @@wasmmodule)
- @@memory =
@@instance.export("memory").to_memory
Instance Attribute Summary collapse
-
#has_config ⇒ Object
Returns the value of attribute has_config.
-
#initialized ⇒ Object
Returns the value of attribute initialized.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#variable_type_codes ⇒ Object
readonly
Returns the value of attribute variable_type_codes.
Instance Method Summary collapse
- #check_event_queue_size ⇒ Object
- #close ⇒ Object
- #flush_event_queue ⇒ Object
- #generate_bucketed_config(user) ⇒ Object
- #init_event_queue(options) ⇒ Object
-
#initialize(sdkkey, options, wait_for_init) ⇒ LocalBucketing
constructor
A new instance of LocalBucketing.
- #on_payload_failure(payload_id, retryable) ⇒ Object
- #on_payload_success(payload_id) ⇒ Object
- #queue_aggregate_event(event, bucketeduser) ⇒ Object
- #queue_event(user, event) ⇒ Object
- #set_client_custom_data(customdata) ⇒ Object
- #store_config(config) ⇒ Object
- #variable_for_user(user, key, variable_type) ⇒ Object
Constructor Details
#initialize(sdkkey, options, wait_for_init) ⇒ LocalBucketing
Returns a new instance of LocalBucketing.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 91 def initialize(sdkkey, , wait_for_init) @initialized = false @has_config = false @sdkkey = sdkkey = @logger = .logger @wasm_mutex = Mutex.new @variable_type_codes = { boolean: @@instance.export("VariableType.Boolean").to_global.get.to_i, string: @@instance.export("VariableType.String").to_global.get.to_i, number: @@instance.export("VariableType.Number").to_global.get.to_i, json: @@instance.export("VariableType.JSON").to_global.get.to_i } set_sdk_key_internal(sdkkey) platform_data = PlatformData.new('server', VERSION, RUBY_VERSION, nil, 'Ruby', Socket.gethostname) set_platform_data(platform_data) @config_manager = ConfigManager.new(@sdkkey, self, wait_for_init) end |
Instance Attribute Details
#has_config ⇒ Object
Returns the value of attribute has_config.
18 19 20 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 18 def has_config @has_config end |
#initialized ⇒ Object
Returns the value of attribute initialized.
17 18 19 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 17 def initialized @initialized end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 15 def end |
#variable_type_codes ⇒ Object (readonly)
Returns the value of attribute variable_type_codes.
16 17 18 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 16 def variable_type_codes @variable_type_codes end |
Instance Method Details
#check_event_queue_size ⇒ Object
161 162 163 164 165 166 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 161 def check_event_queue_size @wasm_mutex.synchronize do @@stack_tracer = @@stack_tracer_raise @@instance.invoke("eventQueueSize", @sdkKeyAddr) end end |
#close ⇒ Object
110 111 112 113 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 110 def close @config_manager.close @config_manager = nil end |
#flush_event_queue ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 146 def flush_event_queue @wasm_mutex.synchronize do @@stack_tracer = @@stack_tracer_raise payload_addr = @@instance.invoke("flushEventQueue", @sdkKeyAddr) raw_json = read_asc_string(payload_addr) raw_payloads = Oj.load(raw_json) if raw_payloads == nil return [] end raw_payloads.map { |raw_payload| EventsPayload.new(raw_payload["records"], raw_payload["payloadId"], raw_payload["eventCount"]) } end end |
#generate_bucketed_config(user) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 116 def generate_bucketed_config(user) @wasm_mutex.synchronize do user_addr = malloc_asc_string(user.to_json) @@stack_tracer = @@stack_tracer_raise config_addr = @@instance.invoke("generateBucketedConfigForUser", @sdkKeyAddr, user_addr) bucketed_config_json = read_asc_string(config_addr) bucketed_config_hash = Oj.load(bucketed_config_json) BucketedUserConfig.new(bucketed_config_hash['project'], bucketed_config_hash['environment'], bucketed_config_hash['features'], bucketed_config_hash['featureVariationMap'], bucketed_config_hash['variableVariationMap'], bucketed_config_hash['variables'], bucketed_config_hash['knownVariableKeys']) end end |
#init_event_queue(options) ⇒ Object
232 233 234 235 236 237 238 239 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 232 def init_event_queue() @wasm_mutex.synchronize do = Oj.dump() = malloc_asc_string() @@stack_tracer = @@stack_tracer_raise @@instance.invoke("initEventQueue", @sdkKeyAddr, ) end end |
#on_payload_failure(payload_id, retryable) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 178 def on_payload_failure(payload_id, retryable) @wasm_mutex.synchronize do payload_addr = malloc_asc_string(payload_id) @@stack_tracer = @@stack_tracer_raise @@instance.invoke("onPayloadFailure", @sdkKeyAddr, payload_addr, retryable ? 1 : 0) end end |
#on_payload_success(payload_id) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 169 def on_payload_success(payload_id) @wasm_mutex.synchronize do payload_addr = malloc_asc_string(payload_id) @@stack_tracer = @@stack_tracer_raise @@instance.invoke("onPayloadSuccess", @sdkKeyAddr, payload_addr) end end |
#queue_aggregate_event(event, bucketeduser) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 202 def queue_aggregate_event(event, bucketeduser) @wasm_mutex.synchronize do begin variable_variation_map = if !bucketeduser.nil? bucketeduser.variable_variation_map else {} end varmap_addr = malloc_asc_string(Oj.dump(variable_variation_map)) asc_pin(varmap_addr) event_addr = malloc_asc_string(event.to_json) @@stack_tracer = @@stack_tracer_raise @@instance.invoke("queueAggregateEvent", @sdkKeyAddr, event_addr, varmap_addr) ensure asc_unpin(varmap_addr) end end end |
#queue_event(user, event) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 187 def queue_event(user, event) @wasm_mutex.synchronize do begin user_addr = malloc_asc_string(user.to_json) asc_pin(user_addr) event_addr = malloc_asc_string(event.to_json) @@stack_tracer = @@stack_tracer_raise @@instance.invoke("queueEvent", @sdkKeyAddr, user_addr, event_addr) ensure asc_unpin(user_addr) end end end |
#set_client_custom_data(customdata) ⇒ Object
242 243 244 245 246 247 248 249 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 242 def set_client_custom_data(customdata) @wasm_mutex.synchronize do customdata_json = Oj.dump(customdata) customdata_addr = malloc_asc_string(customdata_json) @@stack_tracer = @@stack_tracer_raise @@instance.invoke("setClientCustomData", @sdkKeyAddr, customdata_addr) end end |
#store_config(config) ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 223 def store_config(config) @wasm_mutex.synchronize do config_addr = malloc_asc_string(config) @@stack_tracer = @@stack_tracer_raise @@instance.invoke("setConfigData", @sdkKeyAddr, config_addr) end end |
#variable_for_user(user, key, variable_type) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 135 def variable_for_user(user, key, variable_type) @wasm_mutex.synchronize do user_addr = malloc_asc_string(user.to_json) key_addr = malloc_asc_string(key) @@stack_tracer = @@stack_tracer_raise var_addr = @@instance.invoke("variableForUser", @sdkKeyAddr, user_addr, key_addr, variable_type, 1) read_asc_string(var_addr) end end |