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

[FEATURE REQUEST] random/N for salt.states.cron #60567

Open
dseomn opened this issue Jul 19, 2021 · 5 comments
Open

[FEATURE REQUEST] random/N for salt.states.cron #60567

dseomn opened this issue Jul 19, 2021 · 5 comments
Assignees
Labels
Feature new functionality including changes to functionality and code refactors, etc. Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged

Comments

@dseomn
Copy link
Contributor

dseomn commented Jul 19, 2021

Describe the solution you'd like
salt.states.cron currently supports values like minute: '*/5' to run every 5 minutes. If I understand correctly, that runs on minutes 0, 5, 10, etc., right? There's also support for minute: 'random' to run on a random minute. It would be nice to have the combination of those two features so something like minute: 'random/5' would run every 5 minutes, starting on a random minute between 0 and 4.

Describe alternatives you've considered
I'm not aware of any other easy way to spread out cron jobs that run every N minutes/hours/etc. It is possible for a cron job to sleep for a random amount of time before doing anything else though.

@dseomn dseomn added Feature new functionality including changes to functionality and code refactors, etc. needs-triage labels Jul 19, 2021
@leeclemens
Copy link
Contributor

On Fedora 34 random/5 does not appear to be a valid format. I ran 0-6 for testing and they never printed anything, but */5 worked as expected.

Test

/etc/crontab

*/5 * * * *	root	echo "*/5: $(date)" >> /tmp/crontest
0/5 * * * *	root	echo "0/5: $(date)" >> /tmp/crontest
1/5 * * * *	root	echo "1/5: $(date)" >> /tmp/crontest
2/5 * * * *	root	echo "2/5: $(date)" >> /tmp/crontest
3/5 * * * *	root	echo "3/5: $(date)" >> /tmp/crontest
4/5 * * * *	root	echo "4/5: $(date)" >> /tmp/crontest
5/5 * * * *	root	echo "5/5: $(date)" >> /tmp/crontest
6/5 * * * *	root	echo "6/5: $(date)" >> /tmp/crontest

/tmp/crontest

*/5: Thu Jul 22 03:15:01 PM EDT 2021
*/5: Thu Jul 22 03:20:01 PM EDT 2021
*/5: Thu Jul 22 03:25:01 PM EDT 2021

crond[3392]: (CRON) bad minute (/etc/crontab)

@dwoz dwoz added Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged and removed needs-triage labels Jul 22, 2021
@dwoz
Copy link
Contributor

dwoz commented Jul 22, 2021

Seems like this could be a useful feature. I do wonder how far off of upstream cron implementations we'd be headed. I know some cron implementations have the ability to add random delay to jobs. Adding support for this might accomplish what you are after.

@dseomn
Copy link
Contributor Author

dseomn commented Jul 22, 2021

@leeclemens Thanks for looking into that. I think it might work to do 0-59/5, 1-59/5, etc. If that doesn't work, then I think the other way to implement it is 0,5,10,..., 1,6,11,..., etc.

@dwoz Makes sense, and thanks, I'll look at some other cron implementations.

@leeclemens
Copy link
Contributor

@dseomn having salt extrapolate those combinations would be my next thought based on what I've seen. May get a little cumbersome, so perhaps there's a more elegant solution.

I saw some notations like */5+2 as well, but I'm not sure what systems use it.

@dseomn
Copy link
Contributor Author

dseomn commented May 11, 2024

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#random-items-or-numbers has another idea for how to implement this. I haven't tested this yet, but I think using https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.mod_random.html#salt.modules.mod_random.rand_int could work something like:

minute: {{ salt['random.rand_int'](0, 4, seed=grains.id + ':some-id-for-this-specific-cron-job') }}}-59/5

dseomn added a commit to dseomn/system-configs that referenced this issue May 15, 2024
With the cron.present and cron.absent states, it's possible to remove
old/unexpected cron jobs, but it requires a bunch of manual checking for
them. Those checks use execution modules, which are really slow in
salt-ssh. This avoids that by accumulating lines in one file, so when a
cron job state is removed, the next update of that file won't have the
old line.

Also, this makes it relatively easy to implement
saltstack/salt#60567 while I'm at it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature new functionality including changes to functionality and code refactors, etc. Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged
Projects
None yet
Development

No branches or pull requests

3 participants