Skip to content

Commit 95eee67

Browse files
authored
feat: lazy timer commands (#28)
Cherry-pick of facebook#242.
2 parents ec07a0e + 7a5ff6d commit 95eee67

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

cookbooks/fb_timers/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ Required fields:
6565
when you want your job to run. Corresponds to the `OnCalendar` field of the
6666
systemd timer. See below for helpers to generate common calendar patterns.
6767
* `command`: The command to run. Corresponds to the `ExecStart` field of the
68-
systemd service.
68+
systemd service. Specify a Proc to lazily evaluate the command string, useful
69+
for an attribute-driven command.
6970
* `commands`: The commands to run. Will generate several `ExecStart` lines.
7071
Useful if you want to run multiple commands in sequence, without forking to
7172
bash. Mixing `commands` and `command` will produce a warning, but the

cookbooks/fb_timers/spec/default_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@
156156
'command' => '/usr/local/bin/foobar.sh',
157157
'timer_options' => { 'OnBootSec' => '1s' },
158158
},
159+
'lazy' => {
160+
'calendar' => '*:0/15:0',
161+
'command' => proc { '/usr/local/bin/foobar.sh' },
162+
},
159163
}
160164
end
161165
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file managed by chef.
2+
# Local changes to this file will be overwritten.
3+
4+
[Unit]
5+
Description=Run scheduled task lazy
6+
After=network.target
7+
8+
[Service]
9+
Type=oneshot
10+
Slice=system-timers-lazy.slice
11+
ExecStart=/usr/local/bin/foobar.sh
12+
TimeoutStopSec=90s
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file managed by chef.
2+
# Local changes to this file will be overwritten.
3+
4+
[Unit]
5+
Description=Run scheduled task lazy
6+
7+
[Install]
8+
WantedBy=timers.target
9+
10+
[Timer]
11+
OnCalendar=*:0/15:0
12+
AccuracySec=1s
13+
Persistent=false
14+
RandomizedDelaySec=0s
15+
Unit=lazy.service

cookbooks/fb_timers/templates/default/service.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ EnvironmentFile=<%= @conf['envfile'] %>
2626
Slice=system-timers-<%= @conf['name'] %>.slice
2727
<% end %>
2828
<% @conf['commands'].each do |command| %>
29-
ExecStart=<%= command %>
29+
ExecStart=<%= if command.instance_of?(Proc) then command.call() else command end %>
3030
<% end %>
3131
<% if @conf['timeout'] %>
3232
TimeoutStartSec=<%= @conf['timeout'] %>

0 commit comments

Comments
 (0)