forked from brandur/rocket-rides-atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaper.rb
48 lines (38 loc) · 945 Bytes
/
reaper.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
require "uri"
require_relative "./api"
class Reaper
def run
loop do
num_reaped = run_once
# Sleep for a while if we didn't find anything to reap on the last run.
if num_reaped == 0
$stdout.puts "Sleeping for #{SLEEP_DURATION}"
sleep(SLEEP_DURATION)
end
end
end
def run_once
num_reaped = 0
IdempotencyKey.where(Sequel.lit(
"id IN (SELECT id FROM idempotency_keys WHERE created_at < ? LIMIT ?)",
Time.now - IDEMPOTENCY_KEY_REAP_TIMEOUT,
BATCH_SIZE
)).delete
end
# Number of idempotency keys to try to reap 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 reap.
SLEEP_DURATION = 5
private_constant :SLEEP_DURATION
end
#
# run
#
if __FILE__ == $0
# so output appears in Forego
$stderr.sync = true
$stdout.sync = true
Reaper.new.run
end