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.



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/openc3/microservices/timeline_microservice.rb', line 190

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 time



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/openc3/microservices/timeline_microservice.rb', line 236

def add_expire_activity
  now = Time.now.to_i
  @expire = now + 3_000
  activity = ActivityModel.new(
    name: @timeline_name,
    scope: @scope,
    start: (now - 86_400 * 7),
    stop: (now - 82_800 * 7),
    kind: 'EXPIRE',
    data: {}
  )
  @queue << activity
  return activity
end

#generate_thread_poolObject



202
203
204
205
206
207
208
209
# File 'lib/openc3/microservices/timeline_microservice.rb', line 202

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.



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/openc3/microservices/timeline_microservice.rb', line 254

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/openc3/microservices/timeline_microservice.rb', line 211

def run
  @logger.info "#{@timeline_name} timeline manager running"
  loop do
    start = Time.now.to_i
    @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} timeine manager exiting"
end

#shutdownObject



268
269
270
271
272
273
# File 'lib/openc3/microservices/timeline_microservice.rb', line 268

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