Class: OpenC3::ReactionSnoozeManager
- Defined in:
- lib/openc3/microservices/reaction_microservice.rb
Overview
The reaction snooze manager starts a thread pool and keeps track of when a reaction is activated and to evalute triggers when the snooze is complete.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#share ⇒ Object
readonly
Returns the value of attribute share.
-
#thread_pool ⇒ Object
readonly
Returns the value of attribute thread_pool.
Instance Method Summary collapse
- #active_triggers(reaction:) ⇒ Object
- #generate_thread_pool ⇒ Object
-
#initialize(name:, logger:, scope:, share:) ⇒ ReactionSnoozeManager
constructor
A new instance of ReactionSnoozeManager.
- #manage_snoozed_reactions(current_time:) ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(name:, logger:, scope:, share:) ⇒ ReactionSnoozeManager
342 343 344 345 346 347 348 349 350 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 342 def initialize(name:, logger:, scope:, share:) @name = name @logger = logger @scope = scope @share = share @worker_count = 3 @thread_pool = nil @cancel_thread = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
340 341 342 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 340 def name @name end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
340 341 342 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 340 def scope @scope end |
#share ⇒ Object (readonly)
Returns the value of attribute share.
340 341 342 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 340 def share @share end |
#thread_pool ⇒ Object (readonly)
Returns the value of attribute thread_pool.
340 341 342 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 340 def thread_pool @thread_pool end |
Instance Method Details
#active_triggers(reaction:) ⇒ Object
378 379 380 381 382 383 384 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 378 def active_triggers(reaction:) reaction.triggers.each do | trigger | t = TriggerModel.get(name: trigger['name'], group: trigger['group'], scope: @scope) return true if t && t.state end return false end |
#generate_thread_pool ⇒ Object
352 353 354 355 356 357 358 359 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 352 def generate_thread_pool() thread_pool = [] @worker_count.times do | i | worker = ReactionWorker.new(name: @name, logger: @logger, scope: @scope, share: @share, ident: i) thread_pool << Thread.new { worker.run } end return thread_pool end |
#manage_snoozed_reactions(current_time:) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 386 def manage_snoozed_reactions(current_time:) @share.reaction_base.get_snoozed.each do | reaction | time_difference = reaction.snoozed_until - current_time if time_difference <= 0 && @share.snooze_base.not_queued?(reaction: reaction) @logger.info "#{reaction.name} current: #{current_time}, vs #{reaction.snoozed_until}, #{time_difference}" unless reaction.review @logger.debug "#{reaction.name} review set to false, setting snoozed_until back to nil" @share.reaction_base.wake(name: reaction.name) next end if active_triggers(reaction: reaction) @share.queue_base.enqueue(kind: 'reaction', data: reaction.as_json(:allow_nan => true)) else @share.reaction_base.wake(name: reaction.name) end end end end |
#run ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 361 def run @logger.info "ReactionSnoozeManager running" @thread_pool = generate_thread_pool() loop do begin current_time = Time.now.to_i manage_snoozed_reactions(current_time: current_time) rescue StandardError => e @logger.error "ReactionSnoozeManager failed to snooze reactions.\n#{e.formatted}" end break if @cancel_thread sleep(1) break if @cancel_thread end @logger.info "ReactionSnoozeManager exiting" end |
#shutdown ⇒ Object
405 406 407 408 409 410 |
# File 'lib/openc3/microservices/reaction_microservice.rb', line 405 def shutdown @cancel_thread = true @worker_count.times do | i | @share.queue_base.enqueue(kind: nil, data: nil) end end |