From 7a5ff6dd78c74177ea05fec303128380719d0c5b Mon Sep 17 00:00:00 2001 From: Eric Norris Date: Fri, 8 Nov 2024 15:55:21 -0500 Subject: [PATCH] feat: lazy timer commands Before this commit, it was not possible to lazily declare an fb_timers entry that utilized a node attribute inside the command. After this commit, specifying a Proc for `command` or entries in `commands` will cause fb_timers to evaluate the proc when rendering the service template. --- cookbooks/fb_timers/README.md | 3 ++- cookbooks/fb_timers/spec/default_spec.rb | 4 ++++ .../fb_timers/spec/fixtures/default/lazy.service | 12 ++++++++++++ .../fb_timers/spec/fixtures/default/lazy.timer | 15 +++++++++++++++ cookbooks/fb_timers/templates/default/service.erb | 2 +- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 cookbooks/fb_timers/spec/fixtures/default/lazy.service create mode 100644 cookbooks/fb_timers/spec/fixtures/default/lazy.timer diff --git a/cookbooks/fb_timers/README.md b/cookbooks/fb_timers/README.md index 5c363f962..6274a0b07 100644 --- a/cookbooks/fb_timers/README.md +++ b/cookbooks/fb_timers/README.md @@ -65,7 +65,8 @@ Required fields: when you want your job to run. Corresponds to the `OnCalendar` field of the systemd timer. See below for helpers to generate common calendar patterns. * `command`: The command to run. Corresponds to the `ExecStart` field of the - systemd service. + systemd service. Specify a Proc to lazily evaluate the command string, useful + for an attribute-driven command. * `commands`: The commands to run. Will generate several `ExecStart` lines. Useful if you want to run multiple commands in sequence, without forking to bash. Mixing `commands` and `command` will produce a warning, but the diff --git a/cookbooks/fb_timers/spec/default_spec.rb b/cookbooks/fb_timers/spec/default_spec.rb index a28f578cf..360745dae 100644 --- a/cookbooks/fb_timers/spec/default_spec.rb +++ b/cookbooks/fb_timers/spec/default_spec.rb @@ -156,6 +156,10 @@ 'command' => '/usr/local/bin/foobar.sh', 'timer_options' => { 'OnBootSec' => '1s' }, }, + 'lazy' => { + 'calendar' => '*:0/15:0', + 'command' => proc { '/usr/local/bin/foobar.sh' }, + }, } end end diff --git a/cookbooks/fb_timers/spec/fixtures/default/lazy.service b/cookbooks/fb_timers/spec/fixtures/default/lazy.service new file mode 100644 index 000000000..d5e8fd060 --- /dev/null +++ b/cookbooks/fb_timers/spec/fixtures/default/lazy.service @@ -0,0 +1,12 @@ +# This file managed by chef. +# Local changes to this file will be overwritten. + +[Unit] +Description=Run scheduled task lazy +After=network.target + +[Service] +Type=oneshot +Slice=system-timers-lazy.slice +ExecStart=/usr/local/bin/foobar.sh +TimeoutStopSec=90s diff --git a/cookbooks/fb_timers/spec/fixtures/default/lazy.timer b/cookbooks/fb_timers/spec/fixtures/default/lazy.timer new file mode 100644 index 000000000..46ddb6dbe --- /dev/null +++ b/cookbooks/fb_timers/spec/fixtures/default/lazy.timer @@ -0,0 +1,15 @@ +# This file managed by chef. +# Local changes to this file will be overwritten. + +[Unit] +Description=Run scheduled task lazy + +[Install] +WantedBy=timers.target + +[Timer] +OnCalendar=*:0/15:0 +AccuracySec=1s +Persistent=false +RandomizedDelaySec=0s +Unit=lazy.service diff --git a/cookbooks/fb_timers/templates/default/service.erb b/cookbooks/fb_timers/templates/default/service.erb index 717de165f..0e6c38bb0 100644 --- a/cookbooks/fb_timers/templates/default/service.erb +++ b/cookbooks/fb_timers/templates/default/service.erb @@ -26,7 +26,7 @@ EnvironmentFile=<%= @conf['envfile'] %> Slice=system-timers-<%= @conf['name'] %>.slice <% end %> <% @conf['commands'].each do |command| %> -ExecStart=<%= command %> +ExecStart=<%= if command.instance_of?(Proc) then command.call() else command end %> <% end %> <% if @conf['timeout'] %> TimeoutStartSec=<%= @conf['timeout'] %>