Class: Streamdal::Client
- Inherits:
-
Object
- Object
- Streamdal::Client
- Includes:
- Audiences, Schemas, Validation
- Defined in:
- lib/streamdal.rb
Direct Known Subclasses
Constant Summary collapse
- CounterEntry =
Aliases to keep lines short
Streamdal::Metrics::CounterEntry
- Metrics =
Streamdal::Metrics
Instance Method Summary collapse
-
#initialize(cfg) ⇒ Client
constructor
A new instance of Client.
- #process(data, audience) ⇒ Object
- #shutdown ⇒ Object
Methods included from Schemas
#_get_schema, #_handle_schema, #_set_schema
Methods included from Validation
#validate_kv_command, #validate_kv_instruction, #validate_kv_object, #validate_set_pipelines, #validate_tail_request
Methods included from Audiences
#_add_audience, #_add_audiences, #_seen_audience, #aud_to_str, #str_to_aud
Constructor Details
#initialize(cfg) ⇒ Client
Returns a new instance of Client.
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 |
# File 'lib/streamdal.rb', line 81 def initialize(cfg) _validate_cfg(cfg) @cfg = cfg @log = cfg[:log] @functions = {} @session_id = SecureRandom.uuid @pipelines = {} @audiences = {} @schemas = {} @tails = {} @paused_tails = {} @metrics = Metrics.new(cfg) @workers = [] @exit = false @kv = Streamdal::KeyValue.new @hostfunc = Streamdal::HostFunc.new(@kv) # # Connect to Streamdal External gRPC API @stub = Streamdal::Protos::Internal::Stub.new(@cfg[:streamdal_url], :this_channel_is_insecure) _pull_initial_pipelines @workers << Thread.new { _heartbeat } @workers << Thread.new { _register } end |
Instance Method Details
#process(data, audience) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/streamdal.rb', line 121 def process(data, audience) raise 'data is required' if data.empty? raise 'audience is required' if audience.nil? resp = Streamdal::Protos::SDKResponse.new resp.status = :EXEC_STATUS_TRUE resp.pipeline_status = Google::Protobuf::RepeatedField.new(:message, Streamdal::Protos::PipelineStatus, []) resp.data = data aud = audience.to_proto(@cfg[:service_name]) labels = { "service": @cfg[:service_name], "operation_type": aud.operation_type, "operation": aud.operation_name, "component": aud.component_name, "pipeline_name": '', "pipeline_id": '', } # TODO: metrics bytes_processed = Metrics::COUNTER_CONSUME_BYTES errors_counter = Metrics::COUNTER_CONSUME_ERRORS total_counter = Metrics::COUNTER_CONSUME_PROCESSED rate_processed = Metrics::COUNTER_CONSUME_PROCESSED_RATE if aud.operation_type == OPERATION_TYPE_PRODUCER bytes_processed = Metrics::COUNTER_PRODUCE_BYTES errors_counter = Metrics::COUNTER_PRODUCE_ERRORS total_counter = Metrics::COUNTER_PRODUCE_PROCESSED rate_processed = Metrics::COUNTER_PRODUCE_PROCESSED_RATE end payload_size = data.length if payload_size > MAX_PAYLOAD_SIZE # TODO: add metrics resp.status = :EXEC_STATUS_ERROR resp.error = 'payload size exceeds maximum allowed size' resp end # Needed for send_tail() original_data = data pipelines = _get_pipelines(aud) if pipelines.empty? _send_tail(aud, '', original_data, original_data) return resp end @metrics.incr(CounterEntry.new(bytes_processed, aud, labels, data.length)) @metrics.incr(CounterEntry.new(rate_processed, aud, labels, 1)) # Used for passing data between steps isr = nil Timeout::timeout(@cfg[:pipeline_timeout]) do pipelines.each do |pipeline| pipeline_status = Streamdal::Protos::PipelineStatus.new pipeline_status.id = pipeline.id pipeline_status.name = pipeline.name pipeline_status.step_status = Google::Protobuf::RepeatedField.new(:message, Streamdal::Protos::StepStatus, []) @log.debug "Running pipeline: '#{pipeline.name}'" labels[:pipeline_id] = pipeline.id labels[:pipeline_name] = pipeline.name @metrics.incr(CounterEntry.new(total_counter, aud, labels, 1)) @metrics.incr(CounterEntry.new(bytes_processed, aud, labels, data.length)) pipeline.steps.each do |step| step_status = Streamdal::Protos::StepStatus.new step_status.name = step.name step_status.status = :EXEC_STATUS_TRUE begin wasm_resp = _call_wasm(step, data, isr) rescue => e @log.error "Error running step '#{step.name}': #{e}" step_status.status = :EXEC_STATUS_ERROR step_status.error = e.to_s pipeline_status.step_status.push(step_status) break end @log.debug "Running step '#{step.name}' in dry-run mode" if @cfg[:dry_run] resp.data = wasm_resp.output_payload if wasm_resp.output_payload.length.positive? _handle_schema(aud, step, wasm_resp) isr = wasm_resp.inter_step_result case wasm_resp.exit_code when :WASM_EXIT_CODE_FALSE cond = step.on_false cond_type = :CONDITION_TYPE_ON_FALSE exec_status = :EXEC_STATUS_FALSE when :WASM_EXIT_CODE_ERROR cond = step.on_error cond_type = :CONDITION_TYPE_ON_ERROR exec_status = :EXEC_STATUS_ERROR isr = nil # errors_counter, 1, labels, aud @metrics.incr(CounterEntry.new(errors_counter, aud, labels, 1)) else cond = step.on_true exec_status = :EXEC_STATUS_TRUE cond_type = :CONDITION_TYPE_ON_TRUE end _notify_condition(pipeline, step, aud, cond, resp.data, cond_type) if @cfg[:dry_run] @log.debug("Step '#{step.name}' completed with status: #{exec_status}, continuing to next step") next end # Whether we are aborting early, aborting current, or continuing, we need to set the step status step_status.status = exec_status step_status. = "Step returned: #{wasm_resp.exit_msg}" # Pull metadata from step into SDKResponse unless cond.nil? resp. = cond. case cond.abort when :ABORT_CONDITION_ABORT_CURRENT step_status.status = exec_status step_status. = "Step returned: #{wasm_resp.exit_msg}" pipeline_status.step_status.push(step_status) resp.pipeline_status.push(pipeline_status) # Continue outer pipeline loop, there might be additional pipelines break when :ABORT_CONDITION_ABORT_ALL # Set step status and push to pipeline status step_status.status = exec_status step_status. = "Step returned: #{wasm_resp.exit_msg}" pipeline_status.step_status.push(step_status) resp.pipeline_status.push(pipeline_status) # Since we're returning early here, also need to set the response status resp.status = exec_status resp. = "Step returned: #{wasm_resp.exit_msg}" _send_tail(aud, pipeline.id, original_data, resp.data) return resp else # Do nothing end end # Append step status to the current pipeline status' array pipeline_status.step_status.push(step_status) end # Append pipeline status to the response resp.pipeline_status.push(pipeline_status) end # pipelines.each end # timeout _send_tail(aud, '', original_data, resp.data) if @cfg[:dry_run] @log.debug 'Dry-run, setting response data to original data' resp.data = original_data end resp end |
#shutdown ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/streamdal.rb', line 108 def shutdown # Set exit flag so workers exit @exit = true # Let loops exit sleep(1) # Exit any remaining threads @workers.each do |w| w.exit if w.running? end end |