Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pseudo-Randomize "every hour" and "every day" #818

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Run `whenever --help` for a complete list of options for selecting the schedule
### Example schedule.rb file

```ruby
# randomize all :hour- and :day-jobs, when no specific at-time is given.
# This avoids hourly jobs starting at the same time.
# set :randomize, true

every 3.hours do # 1.minute 1.day 1.week 1.month 1.year is also supported
# the following tasks are run in parallel (not in sequence)
runner "MyModel.some_process"
Expand All @@ -71,6 +75,10 @@ every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end

every :hour, randomize: true do # each hour, but at random minute
runner "SomeModel.ladeeda"
end

every :sunday, at: '12pm' do # Use any day of the week or :weekend, :weekday
runner "Task.do_something_great"
end
Expand Down
27 changes: 27 additions & 0 deletions lib/whenever/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Cron
MONTHS = %w(jan feb mar apr may jun jul aug sep oct nov dec)
KEYWORDS = [:reboot, :yearly, :annually, :monthly, :weekly, :daily, :midnight, :hourly]
REGEX = /^(@(#{KEYWORDS.join '|'})|((\*?[\d\/,\-]*)\s){3}(\*?([\d\/,\-]|(#{MONTHS.join '|'}))*\s)(\*?([\d\/,\-]|(#{DAYS.join '|'}))*))$/i
RANGE_FOR_RANDOM_BY_TIME_UNIT = {
minute: (0...60).to_a,
hour: %w[22 23 00 01 02 03 04 05 06 07]
}.freeze

attr_accessor :time, :task

Expand All @@ -16,6 +20,7 @@ def initialize(time = nil, task = nil, at = nil, options = {})
@at_given = at
@time = time
@task = task
at = Cron.randomize_at_by_task(at, time, options[:task]) if options[:randomize]
@at = at.is_a?(String) ? (Chronic.parse(at, chronic_options) || 0) : (at || 0)
end

Expand All @@ -42,6 +47,28 @@ def self.output(times, job, options = {})
end
end

# pseudo-random: same at for same task, useful for debugging across several deploys
def self.randomize_at_by_task(at, time, task)
return at unless at.nil?

# atm, the randomizing-feature works only for symbols :hour and :day
# return at unless time.is_a?(Symbol) && [:hour, :day].include?(time)
case time
when :hour
random_by_task(task, :minute)
when :day
random_by_task(task, :hour) + ':' + random_by_task(task, :minute).to_s
else
at
end

end

def self.random_by_task(task, time_unit)
array = RANGE_FOR_RANDOM_BY_TIME_UNIT[time_unit]
array[task.sum % array.length]
end

def output
[time_in_cron_syntax, task].compact.join(' ').strip
end
Expand Down
2 changes: 1 addition & 1 deletion lib/whenever/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Whenever
class Job
attr_reader :at, :roles, :mailto
attr_reader :at, :roles, :mailto, :options

def initialize(options = {})
@options = options
Expand Down
2 changes: 1 addition & 1 deletion lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def cron_jobs_of_time(time, jobs)
next unless roles.empty? || roles.any? do |r|
job.has_role?(r)
end
Whenever::Output::Cron.output(time, job, :chronic_options => @chronic_options) do |cron|
Whenever::Output::Cron.output(time, job, job.options.merge(:chronic_options => @chronic_options)) do |cron|
cron << "\n\n"

if cron[0,1] == "@"
Expand Down
53 changes: 53 additions & 0 deletions test/functional/output_random_at_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'test_helper'

class OutputRandomAtTest < Whenever::TestCase
test "pseudo random at for hour and day" do
output = Whenever.cron \
<<-file
set :job_template, nil
set :randomize, true
every :day do
command "blahblah"
end
file
assert_match '34 2 * * * blahblah', output

output = Whenever.cron \
<<-file
set :job_template, nil
set :randomize, true
every :day do
runner "blahblah"
end
file
assert_match(/\A34 2 * * */, output)

output = Whenever.cron \
<<-file
set :job_template, nil
every :day, randomize: true do
command "blah"
end
file
assert_match '47 5 * * * blah', output

output = Whenever.cron \
<<-file
set :job_template, nil
set :randomize, true
every :hour do
command "blahblah"
end
file
assert_match '34 * * * * blahblah', output

output = Whenever.cron \
<<-file
set :job_template, nil
every :hour, randomize: true do
command "blah"
end
file
assert_match '47 * * * * blah', output
end
end