lib/openc3/microservices/timeline_microservice.rb in openc3-5.19.0 vs lib/openc3/microservices/timeline_microservice.rb in openc3-5.20.0
- old
+ new
@@ -163,19 +163,20 @@
class Schedule
def initialize(name)
@name = name
@activities_mutex = Mutex.new
@activities = []
+ # This can stay relatively small since it's only the currently queued
+ # activities. Once a worker thread processes it we no longer care.
@size = 20
@queue = Array.new(@size)
@index = 0
end
- def not_queued?(start)
- return false if @queue.index(start)
-
- @queue[@index] = start
+ def not_queued?(activity)
+ return false if @queue.index(activity.uuid)
+ @queue[@index] = activity.uuid
@index = @index + 1 >= @size ? 0 : @index + 1
return true
end
def activities
@@ -188,21 +189,21 @@
@activities_mutex.synchronize do
@activities = input_activities.dup
end
end
- def add_activity(input_activity)
+ def add_activity(activity)
@activities_mutex.synchronize do
- if @activities.find { |x| x.start == input_activity.start }.nil?
- @activities << input_activity
- end
+ @activities << activity
end
end
- def remove_activity(input_activity)
+ def remove_activity(data)
@activities_mutex.synchronize do
- @activities.delete_if { |h| h.start == input_activity.start }
+ @activities.delete_if do |a|
+ a.start == data['start'] && a.uuid == data['uuid']
+ end
end
end
end
# The timeline manager starts a thread pool and looks at the
@@ -235,18 +236,18 @@
@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)
+ if start_difference <= 0 && @schedule.not_queued?(activity)
@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()
+ @queue << add_expire_activity()
request_update(start: start)
end
break if @cancel_thread
sleep(1)
@@ -265,11 +266,10 @@
start: 0,
stop: (now - (86_400 * 7)),
kind: 'expire',
data: {}
)
- @queue << activity
return activity
end
# 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
@@ -379,12 +379,10 @@
# 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.
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)
+ @schedule.remove_activity(data)
end
def shutdown
@manager.shutdown
# super also sets @cancel_thread = true but we want to set it first