Skip to content

random times for :hour and :day#596

Closed
philister wants to merge 5 commits intojavan:mainfrom
philister:master
Closed

random times for :hour and :day#596
philister wants to merge 5 commits intojavan:mainfrom
philister:master

Conversation

@philister
Copy link
Copy Markdown

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
Copy Markdown

+1

@benlangfeld benlangfeld added this to the 0.10.0 milestone Jun 12, 2016
Comment thread README.md

### ExceptionNotification

If you are using the exception_notification gem (<https://github.com/smartinez87/exception_notification>), enable it for your "runner" cronjobs with:
Copy link
Copy Markdown
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)

Comment thread lib/whenever/cron.rb
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
Copy Markdown
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?

Comment thread lib/whenever/cron.rb

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
Copy Markdown
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?

Comment thread lib/whenever/cron.rb
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
Copy Markdown
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.


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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

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

@lostapathy
Copy link
Copy Markdown
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
Copy Markdown
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