Class: Cosmos::TimelineManager
- Defined in:
- lib/cosmos/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
-
#add_expire_activity ⇒ Object
Add task to remove events older than 7 time.
- #generate_thread_pool ⇒ Object
-
#initialize(name:, scope:, schedule:) ⇒ TimelineManager
constructor
A new instance of TimelineManager.
-
#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.
- #run ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(name:, scope:, schedule:) ⇒ TimelineManager
Returns a new instance of TimelineManager.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 174 def initialize(name:, scope:, schedule:) @timeline_name = name @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_activity ⇒ Object
Add task to remove events older than 7 time
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 219 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_pool ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 185 def generate_thread_pool thread_pool = [] @worker_count.times { worker = TimelineWorker.new(name: @timeline_name, 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.
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 237 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 |
#run ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 194 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 |
#shutdown ⇒ Object
251 252 253 254 255 256 |
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 251 def shutdown @cancel_thread = true @worker_count.times { @queue << nil } end |