Skip to content
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end

# Note: If you use the Symbol-Shortcut (:hour or :day at the moment) and leave the at-argument blank, the exact time of execution will be randomized
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end
Expand Down Expand Up @@ -220,6 +221,15 @@ $ whenever

This will simply show you your `schedule.rb` file converted to cron syntax. It does not read or write your crontab file. Run `whenever --help` for a complete list of options.

### ExceptionNotification

If you are using the exception_notification gem (<https://github.com/smartinez87/exception_notification>), enable it for your "runner" cronjobs with:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use a link in Markdown format?

the [exception_notification gem](https://github.com/smartinez87/exception_notification)


In your "schedule.rb" file:
`set :runner_exception_notification, true`

This will catch all commands defined as "runner", handle it with ExceptionNotification (and raise the exception nevertheless).

### Credit

Whenever was created for use at Inkling (<http://inklingmarkets.com>). Their take on it: <http://blog.inklingmarkets.com/2009/02/whenever-easy-way-to-do-cron-jobs-from.html>
Expand Down
18 changes: 18 additions & 0 deletions lib/whenever/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ def initialize(time = nil, task = nil, at = nil)
@at_given = at
@time = time
@task = task

at = randomize_at(at,time)

@at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)

end

def randomize_at(at, time)
return at unless at.nil?
unless time.is_a?(Symbol) && [:hour,:day].include?(time)
#puts 'Sorry, the randomizing-feature works only for symbols :hour and :day'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove debug-oriented code in the PR?

return at
end

case time
when :hour then return Random.rand(0...59)
when :day then return [23,0,1,2,3,4,5,6,7].sample.to_s + ':' + Random.rand(0...59).to_s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial: there are more than double spaces after the symbols?

end
return at
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case statement allows an else case, which would allow all of the "legs" of it to be the last expression of the method = to return the value from the method = to drop the return keyword.

end

def self.enumerate(item, detect_cron = true)
Expand Down
4 changes: 4 additions & 0 deletions lib/whenever/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def process_template(template, options)
before_and_after = [$`[-1..-1], $'[0..0]]
option = options[key.sub(':', '').to_sym] || key

if @options[:runner_exception_notification] && options[:job_type_name]==:runner && key == ':task'
option = 'begin; ' + option + '; rescue => e; ExceptionNotifier.notify_exception(e); raise e; end'
end

if before_and_after.all? { |c| c == "'" }
escape_single_quotes(option)
elsif before_and_after.all? { |c| c == '"' }
Expand Down
1 change: 1 addition & 0 deletions lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def job_type(name, template)
options[:output] = (options[:cron_log] || @cron_log) if defined?(@cron_log) || options.has_key?(:cron_log)
# :output is the newer, more flexible option.
options[:output] = @output if defined?(@output) && !options.has_key?(:output)
options[:job_type_name] = name

@jobs[@current_time_scope] ||= []
@jobs[@current_time_scope] << Whenever::Job.new(@options.merge(@set_variables).merge(options))
Expand Down
3 changes: 2 additions & 1 deletion test/functional/output_jobs_for_roles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class OutputJobsForRolesTest < Whenever::TestCase
file

assert_match two_hours + " /bin/bash -l -c 'role1_cmd'", output
assert_match "0 * * * * /bin/bash -l -c 'role2_cmd'", output
#assert_match "0 * * * * /bin/bash -l -c 'role2_cmd'", output
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is commented out?

assert_match /^[1-5]?[0-9]( \*){4} \/bin\/bash -l -c 'role2_cmd'$/ , output
end
end
4 changes: 2 additions & 2 deletions test/unit/cron_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ class CronParseShortcutsTest < Whenever::TestCase

should "convert time-based shortcuts to times" do
assert_equal '0 0 1 * *', parse_time(:month)
assert_equal '0 0 * * *', parse_time(:day)
assert_equal '0 * * * *', parse_time(:hour)
assert_match /^[1-5]?[0-9] ([1]?[0-9]|2[0-3])( \*){3}$/ , parse_time(:day)
assert_match /^[1-5]?[0-9]( \*){4}$/ , parse_time(:hour)
assert_equal '0 0 1 12 *', parse_time(:year)
assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
end
Expand Down