Skip to content
Open
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
56 changes: 56 additions & 0 deletions probes/DeterministicDelay
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#! /usr/bin/env python3

"""
Delays running a command by a consistent amount of time based on the hash of the command string.
This may be useful in spreading the load out from frequently running cronjobs.
"""

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.'),
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('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)

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

print(f"Sleeping {delay}s")
time.sleep(delay)

executed = subprocess.run(args.commands, capture_output=True)

print("STDOUT")
print(executed.stdout)
print("STDERR")
print(executed.stderr)
Comment on lines +53 to +56
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be useful to specify in the prints (maybe we should use a logger instead?) that these are the stdout and err of this probe in particular

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, can we still change that?

3 changes: 2 additions & 1 deletion probes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ADD oic.rpm /tmp
RUN dnf install -y epel-release.noarch && \
dnf upgrade -y && \
dnf install -y \
cronie-noanacron \
git \
gcc \
libnsl \
Expand Down Expand Up @@ -43,7 +44,7 @@ WORKDIR /
RUN git clone https://github.com/rucio/probes.git

ADD rucio.config.default.cfg /tmp/

ADD DeterministicDelay /
ADD run-probes.sh /

ENTRYPOINT ["/run-probes.sh"]
20 changes: 13 additions & 7 deletions probes/run-probes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ if [ ! -z "$RUCIO_PRINT_CFG" ]; then
echo ""
fi

cp /etc/jobber-config/dot-jobber.yaml /root/.jobber
if [ ! -z "$RUCIO_USING_CRON" ]; then
echo "Setting up and starting cron"
cp /etc/cron.rucio/probes-crontab /etc/cron.d/
crond -n -s
else
cp /etc/jobber-config/dot-jobber.yaml /root/.jobber

echo "Starting Jobber"
/usr/local/libexec/jobbermaster &
echo "Not using cron. Starting Jobber"
/usr/local/libexec/jobbermaster &

sleep 5
sleep 5

echo
echo "============= Jobber log file ============="
echo
echo "============= Jobber log file ============="

tail -f /var/log/jobber-runs
tail -f /var/log/jobber-runs
fi