2021-12-10 10:10:24 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
class ProcessSyncEventsService
|
|
|
|
include ExclusiveLeaseGuard
|
|
|
|
|
|
|
|
BATCH_SIZE = 1000
|
|
|
|
|
|
|
|
def initialize(sync_event_class, sync_class)
|
|
|
|
@sync_event_class = sync_event_class
|
|
|
|
@sync_class = sync_class
|
2022-02-04 07:17:40 -05:00
|
|
|
@results = {}
|
2021-12-10 10:10:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
# preventing parallel processing over the same event table
|
|
|
|
try_obtain_lease { process_events }
|
|
|
|
|
|
|
|
enqueue_worker_if_there_still_event
|
2022-02-04 07:17:40 -05:00
|
|
|
|
|
|
|
@results
|
2021-12-10 10:10:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_events
|
2022-02-04 07:17:40 -05:00
|
|
|
add_result(estimated_total_events: @sync_event_class.upper_bound_count)
|
|
|
|
|
2021-12-10 10:10:24 -05:00
|
|
|
events = @sync_event_class.preload_synced_relation.first(BATCH_SIZE)
|
|
|
|
|
2022-02-04 07:17:40 -05:00
|
|
|
add_result(consumable_events: events.size)
|
|
|
|
|
2021-12-10 10:10:24 -05:00
|
|
|
return if events.empty?
|
|
|
|
|
2022-01-11 07:14:06 -05:00
|
|
|
processed_events = []
|
2021-12-10 10:10:24 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
events.each do |event|
|
|
|
|
@sync_class.sync!(event)
|
|
|
|
|
2022-01-11 07:14:06 -05:00
|
|
|
processed_events << event
|
2021-12-10 10:10:24 -05:00
|
|
|
end
|
|
|
|
ensure
|
2022-02-04 07:17:40 -05:00
|
|
|
add_result(processed_events: processed_events.size)
|
2022-01-11 07:14:06 -05:00
|
|
|
@sync_event_class.id_in(processed_events).delete_all
|
2021-12-10 10:10:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_worker_if_there_still_event
|
|
|
|
@sync_event_class.enqueue_worker if @sync_event_class.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def lease_key
|
|
|
|
"#{super}::#{@sync_event_class}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def lease_timeout
|
|
|
|
1.minute
|
|
|
|
end
|
2022-02-04 07:17:40 -05:00
|
|
|
|
|
|
|
def add_result(result)
|
|
|
|
@results.merge!(result)
|
|
|
|
end
|
2021-12-10 10:10:24 -05:00
|
|
|
end
|
|
|
|
end
|