Class: Cosmos::TriggerGroupMicroservice

Inherits:
Microservice show all
Defined in:
lib/cosmos/microservices/trigger_group_microservice.rb

Overview

The trigger microservice starts a manager then gets the activities from the sorted set in redis and updates the schedule for the manager. Timeline will then wait for an update on the timeline stream this will trigger an update again to the schedule.

Constant Summary collapse

TRIGGER_METRIC_NAME =
'update_triggers_duration_seconds'.freeze

Instance Attribute Summary collapse

Attributes inherited from Microservice

#count, #custom, #error, #microservice_status_thread, #state

Instance Method Summary collapse

Methods inherited from Microservice

#as_json, run

Constructor Details

#initialize(*args) ⇒ TriggerGroupMicroservice

Returns a new instance of TriggerGroupMicroservice.



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

def initialize(*args)
  super(*args)
  @group = TriggerGroupShare.get_group(name: @name)
  @share = TriggerGroupShare.new(scope: @scope)
  @manager = TriggerGroupManager.new(name: @name, scope: @scope, group: @group, share: @share)
  @manager_thread = nil
  @read_topic = true
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



545
546
547
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 545

def group
  @group
end

#managerObject (readonly)

Returns the value of attribute manager.



545
546
547
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 545

def manager
  @manager
end

#manager_threadObject (readonly)

Returns the value of attribute manager_thread.



545
546
547
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 545

def manager_thread
  @manager_thread
end

#nameObject (readonly)

Returns the value of attribute name.



545
546
547
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 545

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



545
546
547
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 545

def scope
  @scope
end

#shareObject (readonly)

Returns the value of attribute share.



545
546
547
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 545

def share
  @share
end

Instance Method Details

#block_for_updatesObject



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 586

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

#created_trigger_event(data) ⇒ Object

Add the trigger to the share.



613
614
615
616
617
618
619
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 613

def created_trigger_event(data)
  Logger.debug "TriggerGroupMicroservice created_trigger_event #{data}"
  if data['group'] == @group
    @share.trigger_base.add(trigger: data)
    @manager.refresh()
  end
end

#deleted_trigger_event(data) ⇒ Object

Remove the trigger from the share.



622
623
624
625
626
627
628
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 622

def deleted_trigger_event(data)
  Logger.debug "TriggerGroupMicroservice deleted_trigger_event #{data}"
  if data['group'] == @group
    @share.trigger_base.remove(trigger: data)
    @manager.refresh()
  end
end

#no_op(data) ⇒ Object



603
604
605
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 603

def no_op(data)
  Logger.debug "TriggerGroupMicroservice web socket event: #{data}"
end

#refresh_event(data) ⇒ Object



607
608
609
610
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 607

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

#runObject



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

def run
  Logger.info "TriggerGroupMicroservice running"
  @manager_thread = Thread.new { @manager.run }
  loop do
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    triggers = TriggerModel.all(scope: @scope, group: @group)
    @share.trigger_base.update(triggers: triggers)
    diff = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float
    metric_labels = { 'trigger_group' => @group, 'thread' => 'microservice' }
    @metric.add_sample(name: TRIGGER_METRIC_NAME, value: diff, labels: metric_labels)
    break if @cancel_thread

    block_for_updates()
    break if @cancel_thread
  end
  Logger.info "TriggerGroupMicroservice exiting"
end

#shutdownObject



630
631
632
633
634
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 630

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

#topic_lookup_functionsObject



574
575
576
577
578
579
580
581
582
583
584
# File 'lib/cosmos/microservices/trigger_group_microservice.rb', line 574

def topic_lookup_functions
  return {
    'created' => :created_trigger_event,
    'updated' => :created_trigger_event,
    'deleted' => :deleted_trigger_event,
    'enabled' => :created_trigger_event,
    'disabled' => :created_trigger_event,
    'activated' => :created_trigger_event,
    'deactivated' => :created_trigger_event,
  }
end