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.



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

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



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

def add_expire_activity
  now = Time.now.to_i
  @expire = now + 3_000
  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



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

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.



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

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



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

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



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

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