Add sleep in cache cron jobs

... so that users can avoid executing the same cron command at the same
time in multiple nodes.

Technically this can be done without this change by setting a different
cron job schedule in each node but it requires complicated parameter
setting.

Change-Id: I596879d5d46f23e54fe344d5bf93a57837a6d287
This commit is contained in:
Takashi Kajinami 2022-06-20 20:12:13 +09:00
parent daa8abd50a
commit 21c95657ef
5 changed files with 43 additions and 9 deletions

View File

@ -20,9 +20,15 @@
# (optional) Defaults to '*'.
#
# [*command_options*]
# command options to add to the cronjob
# (optional) command options to add to the cronjob
# (eg. point to config file, or redirect output)
# (optional) Defaults to ''.
# Defaults to ''.
#
# [*maxdelay*]
# (optional) In Seconds. Should be a positive integer.
# Induces a random delay before running the cronjob to avoid running
# all cron jobs at the same time on all hosts this job is configured.
# Defaults to 0.
#
class glance::cache::cleaner(
$minute = 1,
@ -31,13 +37,20 @@ class glance::cache::cleaner(
$month = '*',
$weekday = '*',
$command_options = '',
$maxdelay = 0
) {
include glance::deps
include glance::params
if $maxdelay == 0 {
$sleep = ''
} else {
$sleep = "sleep `expr \${RANDOM} \\% ${maxdelay}`; "
}
cron { 'glance-cache-cleaner':
command => "${glance::params::cache_cleaner_command} ${command_options}",
command => "${sleep}${glance::params::cache_cleaner_command} ${command_options}",
environment => 'PATH=/bin:/usr/bin:/usr/sbin',
user => $::glance::params::user,
minute => $minute,

View File

@ -20,9 +20,15 @@
# (optional) Defaults to '*'.
#
# [*command_options*]
# command options to add to the cronjob
# (optional) command options to add to the cronjob
# (eg. point to config file, or redirect output)
# (optional) Defaults to ''.
# Defaults to ''.
#
# [*maxdelay*]
# (optional) In Seconds. Should be a positive integer.
# Induces a random delay before running the cronjob to avoid running
# all cron jobs at the same time on all hosts this job is configured.
# Defaults to 0.
#
class glance::cache::pruner(
$minute = '*/30',
@ -31,13 +37,20 @@ class glance::cache::pruner(
$month = '*',
$weekday = '*',
$command_options = '',
$maxdelay = 0
) {
include glance::deps
include glance::params
if $maxdelay == 0 {
$sleep = ''
} else {
$sleep = "sleep `expr \${RANDOM} \\% ${maxdelay}`; "
}
cron { 'glance-cache-pruner':
command => "${glance::params::cache_pruner_command} ${command_options}",
command => "${sleep}${glance::params::cache_pruner_command} ${command_options}",
environment => 'PATH=/bin:/usr/bin:/usr/sbin',
user => $::glance::params::user,
minute => $minute,
@ -46,6 +59,5 @@ class glance::cache::pruner(
month => $month,
weekday => $weekday,
require => Anchor['glance::config::end'],
}
}

View File

@ -0,0 +1,7 @@
---
features:
- |
The ``glance::cache::cleaner`` class and the ``glance::cache::pruner``
class now support the ``maxdelay`` parameter. This parameter would be
useful in a multi-node deployment, to avoid launching the same cron command
at the same time in multiple nodes.

View File

@ -33,11 +33,12 @@ describe 'glance::cache::cleaner' do
:month => '2',
:weekday => '3',
:command_options => '--config-dir /etc/glance/',
:maxdelay => 3600,
}
end
it 'configures a cron' do
is_expected.to contain_cron('glance-cache-cleaner').with(
:command => 'glance-cache-cleaner --config-dir /etc/glance/',
:command => 'sleep `expr ${RANDOM} \\% 3600`; glance-cache-cleaner --config-dir /etc/glance/',
:environment => 'PATH=/bin:/usr/bin:/usr/sbin',
:user => 'glance',
:minute => 59,

View File

@ -32,11 +32,12 @@ describe 'glance::cache::pruner' do
:month => '2',
:weekday => '3',
:command_options => '--config-dir /etc/glance/',
:maxdelay => 3600,
}
end
it 'configures a cron' do
is_expected.to contain_cron('glance-cache-pruner').with(
:command => 'glance-cache-pruner --config-dir /etc/glance/',
:command => 'sleep `expr ${RANDOM} \\% 3600`; glance-cache-pruner --config-dir /etc/glance/',
:environment => 'PATH=/bin:/usr/bin:/usr/sbin',
:user => 'glance',
:minute => 59,