Class: OpenC3::TriggerGroupManager

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/microservices/trigger_group_microservice.rb

Overview

The trigger manager starts a thread pool and subscribes to the telemtry decom topic. It adds the “packet” to the thread pool queue and the thread will evaluate the “trigger”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, logger:, scope:, group:, share:) ⇒ TriggerGroupManager

Returns a new instance of TriggerGroupManager.



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 501

def initialize(name:, logger:, scope:, group:, share:)
  @name = name
  @logger = logger
  @scope = scope
  @group = group
  @share = share
  @worker_count = 3
  @queue = Queue.new
  @read_topic = true
  @topics = []
  @thread_pool = nil
  @cancel_thread = false
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



499
500
501
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 499

def group
  @group
end

#nameObject (readonly)

Returns the value of attribute name.



499
500
501
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 499

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



499
500
501
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 499

def scope
  @scope
end

#shareObject (readonly)

Returns the value of attribute share.



499
500
501
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 499

def share
  @share
end

#thread_poolObject (readonly)

Returns the value of attribute thread_pool.



499
500
501
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 499

def thread_pool
  @thread_pool
end

#topicsObject (readonly)

Returns the value of attribute topics.



499
500
501
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 499

def topics
  @topics
end

Instance Method Details

#block_for_updatesObject



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 557

def block_for_updates
  @read_topic = true
  while @read_topic
    begin
      Topic.read_topics(@topics) do |topic, _msg_id, msg_hash, _redis|
        @logger.debug "TriggerGroupManager block_for_updates: #{topic} #{msg_hash.to_s}"
        if topic != @share.trigger_base.autonomic_topic
          packet = JsonPacket.new(:TLM, msg_hash['target_name'], msg_hash['packet_name'], msg_hash['time'].to_i, false, msg_hash["json_data"])
          @share.packet_base.add(topic: topic, packet: packet)
        end
        @queue << "#{topic}"
      end
    rescue StandardError => e
      @logger.error "TriggerGroupManager failed to read topics #{@topics}\n#{e.formatted}"
    end
  end
end

#generate_thread_poolObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 515

def generate_thread_pool()
  thread_pool = []
  @worker_count.times do | i |
    worker = TriggerGroupWorker.new(
      name: @name,
      logger: @logger,
      scope: @scope,
      group: @group,
      queue: @queue,
      share: @share,
      ident: i,
    )
    thread_pool << Thread.new { worker.run }
  end
  return thread_pool
end

#refreshObject



575
576
577
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 575

def refresh
  @read_topic = false
end

#runObject



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 532

def run
  @logger.info "TriggerGroupManager running"
  @thread_pool = generate_thread_pool()
  loop do
    begin
      update_topics()
    rescue StandardError => e
      @logger.error "TriggerGroupManager failed to update topics.\n#{e.formatted}"
    end
    break if @cancel_thread
    block_for_updates()
    break if @cancel_thread
  end
  @logger.info "TriggerGroupManager exiting"
end

#shutdownObject



579
580
581
582
583
584
585
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 579

def shutdown
  @read_topic = false
  @cancel_thread = true
  @worker_count.times do | i |
    @queue << nil
  end
end

#update_topicsObject



548
549
550
551
552
553
554
555
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 548

def update_topics
  past_topics = @topics
  @topics = @share.trigger_base.topics()
  @logger.debug "TriggerGroupManager past_topics: #{past_topics} topics: #{@topics}"
  (past_topics - @topics).each do | removed_topic |
    @share.packet_base.remove(topic: removed_topic)
  end
end