-
Notifications
You must be signed in to change notification settings - Fork 66
Add what's needed to make probes work with normal cron #482
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
Merged
ericvaandering
merged 1 commit into
master
from
481-make-an-option-for-cron-based-probes
Feb 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #! /usr/bin/env python3 | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import subprocess | ||
| import time | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog='DeterministicDelay', | ||
| description=('Delays running a command by a consistent amount of time. ' | ||
| 'This may be useful in spreading the load out from frequently running cronjobs. ' | ||
| 'When frequency is specified, delays longer than the frequency will exit without executing, ' | ||
| 'and the next execution will have a possibility. ' | ||
| 'i.e. -d 7d -f 1d means the script is run once per day, but the only 1/7 executions will really run.'), | ||
| epilog=None) | ||
|
|
||
| parser.add_argument('-d', required=False, dest='delay', | ||
| help=('The maximum amount of time to delay. 60 seconds is the default, ' | ||
| 'suffix with s (the default), m, h, or d for seconds, minutes, hours, or days.')) | ||
| parser.add_argument('-f', required=False, dest='frequency', | ||
| help=('Frequency of submission. ' | ||
| 'Suffix with s (the default), m, h, or d for seconds, minutes, hours, or days.')) | ||
| parser.add_argument('commands', nargs='+', help="The commands to run") | ||
| args = parser.parse_args() | ||
|
|
||
| command_hash = hashlib.sha256(bytes(str(args.commands), 'utf-8')) | ||
| checksum = int(command_hash.hexdigest(), 16) | ||
|
|
||
| frequency = None | ||
| if args.frequency: | ||
| if args.frequency[-1] in ['s', 'm', 'h', 'd']: | ||
| frequency_value, frequency_unit = args.frequency[:-1], args.frequency[-1] | ||
| if frequency_unit == 'm': | ||
| frequency = int(frequency_value) * 60 | ||
| elif frequency_unit == 'h': | ||
| frequency = int(frequency_value) * 60 * 60 | ||
| elif frequency_unit == 'd': | ||
| frequency = int(frequency_value) * 24 * 60 * 60 | ||
| else: | ||
| frequency = int(frequency_value) | ||
| else: | ||
| frequency = int(args.frequency) | ||
|
|
||
| if not args.delay: | ||
| max_delay = 60 | ||
| else: | ||
| if args.delay[-1] in ['s', 'm', 'h', 'd']: | ||
| delay_value, delay_unit = args.delay[:-1], args.delay[-1] | ||
| if delay_unit == 'm': | ||
| max_delay = int(delay_value) * 60 | ||
| elif delay_unit == 'h': | ||
| max_delay = int(delay_value) * 60 * 60 | ||
| elif delay_unit == 'd': | ||
| max_delay = int(delay_value) * 24 * 60 * 60 | ||
| else: | ||
| max_delay = int(delay_value) | ||
| else: | ||
| max_delay = int(args.delay) | ||
|
|
||
| delay = checksum % max_delay | ||
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if frequency and delay: | ||
| epoch = time.time() | ||
| if epoch % delay > frequency: | ||
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print(f'Exiting. time: {epoch % max_delay} max: {max_delay} f: {frequency}') | ||
| exit() | ||
| else: | ||
| print(f'Running. time: {epoch % max_delay} max: {max_delay} f: {frequency}') | ||
| delay = checksum % frequency | ||
|
|
||
| print(f"Sleeping {delay}s") | ||
| time.sleep(delay) | ||
|
|
||
| executed = subprocess.run(args.commands, capture_output=True) | ||
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| print("STDOUT") | ||
| print(executed.stdout) | ||
| print("STDERR") | ||
| print(executed.stderr) | ||
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
ericvaandering marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.