forked from brandur/rocket-rides-atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enqueuer.rb
58 lines (46 loc) · 1.23 KB
/
enqueuer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require_relative "./api"
class Enqueuer
def run
loop do
num_enqueued = run_once
# Sleep for a while if we didn't find anything to enqueue on the last
# run.
if num_enqueued == 0
$stdout.puts "Sleeping for #{SLEEP_DURATION}"
sleep(SLEEP_DURATION)
end
end
end
def run_once
num_enqueued = 0
# Need at least repeatable read isolation level so that our DELETE after
# enqueueing will see the same jobs as the original SELECT.
DB.transaction(isolation_level: :repeatable_read) do
jobs = StagedJob.order(:id).limit(BATCH_SIZE)
unless jobs.empty?
jobs.each do |job|
$stdout.puts "Enqueued job: #{job.job_name}"
num_enqueued += 1
end
StagedJob.where(Sequel.lit("id <= ?", jobs.last.id)).delete
end
end
num_enqueued
end
# Number of jobs to try to enqueue on each batch.
BATCH_SIZE = 1000
private_constant :BATCH_SIZE
# Sleep duration in seconds to sleep in case we ran but didn't find anything
# to enqueue.
SLEEP_DURATION = 5
private_constant :SLEEP_DURATION
end
#
# run
#
if __FILE__ == $0
# so output appears in Forego
$stderr.sync = true
$stdout.sync = true
Enqueuer.new.run
end