diff --git a/manifests/cache/cleaner.pp b/manifests/cache/cleaner.pp index 64a70d08..7a676b2f 100644 --- a/manifests/cache/cleaner.pp +++ b/manifests/cache/cleaner.pp @@ -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', minute => $minute, diff --git a/manifests/cache/pruner.pp b/manifests/cache/pruner.pp index 7766c707..28edada5 100644 --- a/manifests/cache/pruner.pp +++ b/manifests/cache/pruner.pp @@ -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', minute => $minute, @@ -46,6 +59,5 @@ class glance::cache::pruner( month => $month, weekday => $weekday, require => Anchor['glance::config::end'], - } } diff --git a/releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml b/releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml new file mode 100644 index 00000000..76ec8cc6 --- /dev/null +++ b/releasenotes/notes/cache-cron-maxdelay-bfd1585eaa15aeda.yaml @@ -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. diff --git a/spec/classes/glance_cache_cleaner_spec.rb b/spec/classes/glance_cache_cleaner_spec.rb index 04c8d0f1..47604b4f 100644 --- a/spec/classes/glance_cache_cleaner_spec.rb +++ b/spec/classes/glance_cache_cleaner_spec.rb @@ -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, diff --git a/spec/classes/glance_cache_pruner_spec.rb b/spec/classes/glance_cache_pruner_spec.rb index 350e0f7f..3bcd5985 100644 --- a/spec/classes/glance_cache_pruner_spec.rb +++ b/spec/classes/glance_cache_pruner_spec.rb @@ -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,