Class: OpenC3::ReactionSnoozeManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name:, logger:, scope:, share:) ⇒ ReactionSnoozeManager

Returns a new instance of ReactionSnoozeManager.



375
376
377
378
379
380
381
382
383
# File 'lib/openc3/microservices/reaction_microservice.rb', line 375

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

#nameObject (readonly)

Returns the value of attribute name.



373
374
375
# File 'lib/openc3/microservices/reaction_microservice.rb', line 373

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



373
374
375
# File 'lib/openc3/microservices/reaction_microservice.rb', line 373

def scope
  @scope
end

#shareObject (readonly)

Returns the value of attribute share.



373
374
375
# File 'lib/openc3/microservices/reaction_microservice.rb', line 373

def share
  @share
end

#thread_poolObject (readonly)

Returns the value of attribute thread_pool.



373
374
375
# File 'lib/openc3/microservices/reaction_microservice.rb', line 373

def thread_pool
  @thread_pool
end

Instance Method Details

#active_triggers(reaction:) ⇒ Object



411
412
413
414
415
416
417
# File 'lib/openc3/microservices/reaction_microservice.rb', line 411

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_poolObject



385
386
387
388
389
390
391
392
# File 'lib/openc3/microservices/reaction_microservice.rb', line 385

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



419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/openc3/microservices/reaction_microservice.rb', line 419

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)
      # LEVEL triggers mean we run if the trigger is active
      if reaction.triggerLevel == 'LEVEL' and 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

#runObject



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/openc3/microservices/reaction_microservice.rb', line 394

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

#shutdownObject



433
434
435
436
437
438
# File 'lib/openc3/microservices/reaction_microservice.rb', line 433

def shutdown
  @cancel_thread = true
  @worker_count.times do |i|
    @share.queue_base.enqueue(kind: nil, data: nil)
  end
end