Class: OpenC3::TimelineManager

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

Overview

The timeline manager starts a thread pool and looks at the schedule and if an “activity” should be run. TimelineManager adds the “activity” to the thread pool and the thread will execute the “activity”.

Instance Method Summary collapse

Constructor Details

#initialize(name:, logger:, scope:, schedule:) ⇒ TimelineManager

Returns a new instance of TimelineManager.



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/openc3/microservices/timeline_microservice.rb', line 213

def initialize(name:, logger:, scope:, schedule:)
  @timeline_name = name
  @logger = logger
  @scope = scope
  @schedule = schedule
  @worker_count = 3
  @queue = Queue.new
  @thread_pool = generate_thread_pool()
  @cancel_thread = false
  @expire = 0
end

Instance Method Details

#add_expire_activityObject

Add task to remove events older than 7 days



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/openc3/microservices/timeline_microservice.rb', line 259

def add_expire_activity
  now = Time.now.to_f
  @expire = now + 3540 # Needs to be less than 3600 which is the hour we store in memory
  activity = ActivityModel.new(
    name: @timeline_name,
    scope: @scope,
    start: 0,
    stop: (now - (86_400 * 7)),
    kind: 'expire',
    data: {}
  )
  @queue << activity
  return activity
end

#generate_thread_poolObject



225
226
227
228
229
230
231
232
# File 'lib/openc3/microservices/timeline_microservice.rb', line 225

def generate_thread_pool
  thread_pool = []
  @worker_count.times {
    worker = TimelineWorker.new(name: @timeline_name, logger: @logger, scope: @scope, queue: @queue)
    thread_pool << Thread.new { worker.run }
  }
  return thread_pool
end

#request_update(start:) ⇒ Object

This can feedback to ensure the schedule will not run out so this should fire once an hour to make sure the TimelineMicroservice will collect the next hour and update the schedule.



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

def request_update(start:)
  notification = {
    'data' => JSON.generate({ 'time' => start }),
    'kind' => 'refresh',
    'type' => 'timeline',
    'timeline' => @timeline_name
  }
  begin
    TimelineTopic.write_activity(notification, scope: @scope)
  rescue StandardError
    @logger.error "#{@name} manager failed to request update"
  end
end

#runObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/openc3/microservices/timeline_microservice.rb', line 234

def run
  @logger.info "#{@timeline_name} timeline manager running"
  loop do
    start = Time.now.to_f
    @schedule.activities.each do |activity|
      start_difference = activity.start - start
      if start_difference <= 0 && @schedule.not_queued?(activity.start)
        @logger.debug "#{@timeline_name} #{@scope} current start: #{start}, vs #{activity.start}, #{start_difference}"
        activity.add_event(status: 'queued')
        @queue << activity
      end
    end
    if start >= @expire
      add_expire_activity()
      request_update(start: start)
    end
    break if @cancel_thread

    sleep(1)
    break if @cancel_thread
  end
  @logger.info "#{@timeline_name} timeline manager exiting"
end

#shutdownObject



291
292
293
294
295
296
# File 'lib/openc3/microservices/timeline_microservice.rb', line 291

def shutdown
  @cancel_thread = true
  @worker_count.times {
    @queue << nil
  }
end