Class: OpenC3::ReactionMicroservice

Inherits:
Microservice show all
Defined in:
lib/openc3/microservices/reaction_microservice.rb

Overview

The reaction microservice starts a manager then gets the reactions and triggers from redis. It then monitors the AutonomicTopic for changes.

Constant Summary collapse

ACTION_METRIC_NAME =
'reactions_duration_seconds'.freeze

Instance Attribute Summary collapse

Attributes inherited from Microservice

#count, #custom, #error, #logger, #microservice_status_thread, #secrets, #state

Instance Method Summary collapse

Methods inherited from Microservice

#as_json, run

Constructor Details

#initialize(*args) ⇒ ReactionMicroservice

Returns a new instance of ReactionMicroservice.



450
451
452
453
454
455
456
# File 'lib/openc3/microservices/reaction_microservice.rb', line 450

def initialize(*args)
  super(*args)
  @share = ReactionShare.new(scope: @scope)
  @manager = ReactionSnoozeManager.new(name: @name, logger: @logger, scope: @scope, share: @share)
  @manager_thread = nil
  @read_topic = true
end

Instance Attribute Details

#managerObject (readonly)

Returns the value of attribute manager.



448
449
450
# File 'lib/openc3/microservices/reaction_microservice.rb', line 448

def manager
  @manager
end

#manager_threadObject (readonly)

Returns the value of attribute manager_thread.



448
449
450
# File 'lib/openc3/microservices/reaction_microservice.rb', line 448

def manager_thread
  @manager_thread
end

#nameObject (readonly)

Returns the value of attribute name.



448
449
450
# File 'lib/openc3/microservices/reaction_microservice.rb', line 448

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



448
449
450
# File 'lib/openc3/microservices/reaction_microservice.rb', line 448

def scope
  @scope
end

#shareObject (readonly)

Returns the value of attribute share.



448
449
450
# File 'lib/openc3/microservices/reaction_microservice.rb', line 448

def share
  @share
end

Instance Method Details

#block_for_updatesObject



503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/openc3/microservices/reaction_microservice.rb', line 503

def block_for_updates
  @read_topic = true
  while @read_topic
    begin
      AutonomicTopic.read_topics(@topics) do |_topic, _msg_id, msg_hash, _redis|
        @logger.debug "ReactionMicroservice block_for_updates: #{msg_hash.to_s}"
        public_send(topic_lookup_functions[msg_hash['type']][msg_hash['kind']], msg_hash)
      end
    rescue StandardError => e
      @logger.error "ReactionMicroservice failed to read topics #{@topics}\n#{e.formatted}"
    end
  end
end

#no_op(data) ⇒ Object



517
518
519
# File 'lib/openc3/microservices/reaction_microservice.rb', line 517

def no_op(data)
  @logger.debug "ReactionMicroservice web socket event: #{data}"
end

#reaction_created_event(msg_hash) ⇒ Object

Add the reaction to the shared data.



533
534
535
536
# File 'lib/openc3/microservices/reaction_microservice.rb', line 533

def reaction_created_event(msg_hash)
  @logger.debug "ReactionMicroservice reaction created msg_hash: #{msg_hash}"
  @share.reaction_base.add(reaction: JSON.parse(msg_hash['data'], :allow_nan => true, :create_additions => true))
end

#reaction_deleted_event(msg_hash) ⇒ Object

Remove the reaction from the shared data



545
546
547
548
# File 'lib/openc3/microservices/reaction_microservice.rb', line 545

def reaction_deleted_event(msg_hash)
  @logger.debug "ReactionMicroservice reaction deleted msg_hash: #{msg_hash}"
  @share.reaction_base.remove(reaction: JSON.parse(msg_hash['data'], :allow_nan => true, :create_additions => true))
end

#reaction_updated_event(msg_hash) ⇒ Object

Update the reaction to the shared data.



539
540
541
542
# File 'lib/openc3/microservices/reaction_microservice.rb', line 539

def reaction_updated_event(msg_hash)
  @logger.debug "ReactionMicroservice reaction updated msg_hash: #{msg_hash}"
  @share.reaction_base.update(reaction: JSON.parse(msg_hash['data'], :allow_nan => true, :create_additions => true))
end

#refresh_event(data) ⇒ Object



521
522
523
524
# File 'lib/openc3/microservices/reaction_microservice.rb', line 521

def refresh_event(data)
  @logger.debug "ReactionMicroservice web socket schedule refresh: #{data}"
  @read_topic = false
end

#runObject



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/openc3/microservices/reaction_microservice.rb', line 458

def run
  @logger.info "ReactionMicroservice running"
  @manager_thread = Thread.new { @manager.run }
  loop do
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    reactions = ReactionModel.all(scope: @scope)
    @share.reaction_base.setup(reactions: reactions)
    diff = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float
    @metric.add_sample(name: ACTION_METRIC_NAME, value: diff, labels: { 'thread' => 'microservice' })
    break if @cancel_thread

    block_for_updates()
    break if @cancel_thread
  end
  @logger.info "ReactionMicroservice exiting"
end

#shutdownObject



550
551
552
553
554
# File 'lib/openc3/microservices/reaction_microservice.rb', line 550

def shutdown
  @read_topic = false
  @manager.shutdown()
  super
end

#topic_lookup_functionsObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/openc3/microservices/reaction_microservice.rb', line 475

def topic_lookup_functions
  return {
    'group' => {
      'created' => :no_op,
      'updated' => :no_op,
      'deleted' => :no_op,
    },
    'trigger' => {
      'created' => :no_op,
      'updated' => :no_op,
      'deleted' => :no_op,
      'enabled' => :trigger_enabled_event,
      'disabled' => :no_op,
      'activated' => :no_op,
      'deactivated' => :no_op,
    },
    'reaction' => {
      'created' => :reaction_created_event,
      'updated' => :refresh_event,
      'deleted' => :reaction_deleted_event,
      'sleep' => :no_op,
      'awaken' => :no_op,
      'activated' => :reaction_updated_event,
      'deactivated' => :reaction_updated_event,
    }
  }
end

#trigger_enabled_event(msg_hash) ⇒ Object



527
528
529
530
# File 'lib/openc3/microservices/reaction_microservice.rb', line 527

def trigger_enabled_event(msg_hash)
  @logger.debug "ReactionMicroservice trigger event msg_hash: #{msg_hash}"
  @share.queue_base.enqueue(kind: 'trigger', data: JSON.parse(msg_hash['data'], :allow_nan => true, :create_additions => true))
end