Skip to content

Instantly share code, notes, and snippets.

@claytron
Created March 14, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claytron/957398b593d9d1f0909683afb1412aff to your computer and use it in GitHub Desktop.
Save claytron/957398b593d9d1f0909683afb1412aff to your computer and use it in GitHub Desktop.
Show how long a DelayedJob will take to finish out its attempts
def human_time(t)
mm, ss = t.divmod(60)
hh, mm = mm.divmod(60)
dd, hh = hh.divmod(24)
"#{dd}d #{hh}h #{mm}m #{ss}s"
end
# Default attempts is 25
max_attempts = ARGV[0] ? ARGV[0].to_i : 25
cumulative_time = 0
# Delayed job waits 5 seconds + N**4, where N is the number of attempts
(1..max_attempts).each do |i|
next_wait = 5 + (i**4)
cumulative_time += next_wait
puts "#{i}: #{human_time(next_wait)} -> #{human_time(cumulative_time)}"
end
$ ruby ./delayed_job_error_escalate.rb
1: 0d 0h 0m 6s -> 0d 0h 0m 6s
2: 0d 0h 0m 21s -> 0d 0h 0m 27s
3: 0d 0h 1m 26s -> 0d 0h 1m 53s
4: 0d 0h 4m 21s -> 0d 0h 6m 14s
5: 0d 0h 10m 30s -> 0d 0h 16m 44s
6: 0d 0h 21m 41s -> 0d 0h 38m 25s
7: 0d 0h 40m 6s -> 0d 1h 18m 31s
8: 0d 1h 8m 21s -> 0d 2h 26m 52s
9: 0d 1h 49m 26s -> 0d 4h 16m 18s
10: 0d 2h 46m 45s -> 0d 7h 3m 3s
11: 0d 4h 4m 6s -> 0d 11h 7m 9s
12: 0d 5h 45m 41s -> 0d 16h 52m 50s
13: 0d 7h 56m 6s -> 1d 0h 48m 56s
14: 0d 10h 40m 21s -> 1d 11h 29m 17s
15: 0d 14h 3m 50s -> 2d 1h 33m 7s
16: 0d 18h 12m 21s -> 2d 19h 45m 28s
17: 0d 23h 12m 6s -> 3d 18h 57m 34s
18: 1d 5h 9m 41s -> 5d 0h 7m 15s
19: 1d 12h 12m 6s -> 6d 12h 19m 21s
20: 1d 20h 26m 45s -> 8d 8h 46m 6s
21: 2d 6h 1m 26s -> 10d 14h 47m 32s
22: 2d 17h 4m 21s -> 13d 7h 51m 53s
23: 3d 5h 44m 6s -> 16d 13h 35m 59s
24: 3d 20h 9m 41s -> 20d 9h 45m 40s
25: 4d 12h 30m 30s -> 24d 22h 16m 10s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment