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

random times for :hour and :day #596

Closed
wants to merge 5 commits into from
Closed

Conversation

philister
Copy link

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.
This can reduce the load, when multiple jobs run hourly, for example.

@cazzerson
Copy link

+1

@benlangfeld benlangfeld added this to the 0.10.0 milestone Jun 12, 2016
@@ -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)

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?


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?

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
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.

@@ -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?

@lostapathy
Copy link
Contributor

This would be really useful functionality. @benlangfeld or @javan is this something you're interested in merging if I clean up the PR?

@benlangfeld
Copy link
Collaborator

I like the idea. If it can be cleaned up, I’ll take a look more closely.

@lostapathy
Copy link
Contributor

thanks @benlangfeld. I started working on this over at this branch. I have this patch mostly cleaned up but have found a couple other issues with it. I will check back in with a PR when I either get it sorted or am ready to beg for help. thanks!

Base automatically changed from master to main January 20, 2021 18:17
@philister
Copy link
Author

#818

@philister philister closed this Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants