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, #microservice_cmd, run, #setup_microservice_topic

Constructor Details

#initialize(name) ⇒ TimelineMicroservice

Returns a new instance of TimelineMicroservice.



304
305
306
307
308
309
310
311
# File 'lib/openc3/microservices/timeline_microservice.rb', line 304

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/openc3/microservices/timeline_microservice.rb', line 344

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.



371
372
373
374
375
376
377
# File 'lib/openc3/microservices/timeline_microservice.rb', line 371

def create_activity_from_event(data)
  diff = data['start'] - Time.now.to_f
  return if diff < 0 or diff > 3600

  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.



381
382
383
384
385
386
387
# File 'lib/openc3/microservices/timeline_microservice.rb', line 381

def remove_activity_from_event(data)
  diff = data['start'] - Time.now.to_f
  return if diff < 0 or diff > 3600

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

#runObject



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/openc3/microservices/timeline_microservice.rb', line 313

def run
  @logger.info "#{@name} timeline 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} timeline exiting"
end

#schedule_refresh(data) ⇒ Object



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

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

#shutdownObject



389
390
391
392
393
394
395
396
# File 'lib/openc3/microservices/timeline_microservice.rb', line 389

def shutdown
  @manager.shutdown
  # super also sets @cancel_thread = true but we want to set it first
  # so when we set @read_topic = false the run loop stops
  @cancel_thread = true
  @read_topic = false
  super()
end

#timeline_noop(data) ⇒ Object



360
361
362
# File 'lib/openc3/microservices/timeline_microservice.rb', line 360

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

#topic_lookup_functionsObject



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/openc3/microservices/timeline_microservice.rb', line 327

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