Class: OpenC3::TimelineMicroservice

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

Overview

The timeline 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.

Instance Attribute Summary

Attributes inherited from Microservice

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

Instance Method Summary collapse

Methods inherited from Microservice

#as_json, run

Constructor Details

#initialize(name) ⇒ TimelineMicroservice

Returns a new instance of TimelineMicroservice.



280
281
282
283
284
285
286
287
# File 'lib/openc3/microservices/timeline_microservice.rb', line 280

def initialize(name)
  super(name)
  @timeline_name = name.split('__')[2]
  @schedule = Schedule.new(@timeline_name)
  @manager = TimelineManager.new(name: @timeline_name, logger: @logger, scope: scope, schedule: @schedule)
  @manager_thread = nil
  @read_topic = true
end

Instance Method Details

#block_for_updatesObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/openc3/microservices/timeline_microservice.rb', line 320

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

#create_activity_from_event(data) ⇒ Object

Add the activity to the schedule. We don’t need to hold the job in memory if it is longer than an hour away. A refresh task will update that.



347
348
349
350
351
352
353
# File 'lib/openc3/microservices/timeline_microservice.rb', line 347

def create_activity_from_event(data)
  diff = data['start'] - Time.now.to_i
  return unless (2..3600).include? diff

  activity = ActivityModel.from_json(data, name: @timeline_name, scope: @scope)
  @schedule.add_activity(activity)
end

#remove_activity_from_event(data) ⇒ Object

Remove the activity from the schedule. We don’t need to remove the activity if it is longer than an hour away. It will be removed from the data.



357
358
359
360
361
362
363
# File 'lib/openc3/microservices/timeline_microservice.rb', line 357

def remove_activity_from_event(data)
  diff = data['start'] - Time.now.to_i
  return unless (2..3600).include? diff

  activity = ActivityModel.from_json(data, name: @timeline_name, scope: @scope)
  @schedule.remove_activity(activity)
end

#runObject



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/openc3/microservices/timeline_microservice.rb', line 289

def run
  @logger.info "#{@name} timeine running"
  @manager_thread = Thread.new { @manager.run }
  loop do
    current_activities = ActivityModel.activities(name: @timeline_name, scope: @scope)
    @schedule.update(current_activities)
    break if @cancel_thread

    block_for_updates()
    break if @cancel_thread
  end
  @logger.info "#{@name} timeine exitting"
end

#schedule_refresh(data) ⇒ Object



340
341
342
343
# File 'lib/openc3/microservices/timeline_microservice.rb', line 340

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

#shutdownObject



365
366
367
368
369
# File 'lib/openc3/microservices/timeline_microservice.rb', line 365

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

#timeline_nop(data) ⇒ Object



336
337
338
# File 'lib/openc3/microservices/timeline_microservice.rb', line 336

def timeline_nop(data)
  @logger.debug "#{@name} timeline web socket event: #{data}"
end

#topic_lookup_functionsObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/openc3/microservices/timeline_microservice.rb', line 303

def topic_lookup_functions
  {
    'timeline' => {
      'created' => :timeline_nop,
      'refresh' => :schedule_refresh,
      'updated' => :timeline_nop,
      'deleted' => :timeline_nop
    },
    'activity' => {
      'event' => :timeline_nop,
      'created' => :create_activity_from_event,
      'updated' => :schedule_refresh,
      'deleted' => :remove_activity_from_event
    }
  }
end