Skip to content

Commit

Permalink
feat: lazy timer commands
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ericnorris committed Nov 8, 2024
1 parent edea576 commit 3d6c7c5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cookbooks/fb_timers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cookbooks/fb_timers/spec/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions cookbooks/fb_timers/spec/fixtures/default/lazy.service
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions cookbooks/fb_timers/spec/fixtures/default/lazy.timer
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion cookbooks/fb_timers/templates/default/service.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] %>
Expand Down

0 comments on commit 3d6c7c5

Please sign in to comment.