Skip to content

Commit 937fa56

Browse files
committed
Prefer on_active_sec to on_startup_sec
Refs #4
1 parent 92cff36 commit 937fa56

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/sysdi/core.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,16 @@ class TimedUnit:
213213
# instead of waiting for ExecStart to exit. Use oneshot as the default instead.
214214
service_type: str = 'oneshot'
215215

216-
# How long after system startup or user login to start the unit.
217-
# TODO: should have a default here. Maybe 15s?
216+
# How long after systemd service startup should the timer activate? For a user unit, this
217+
# would be from systemd startup for the user. But if linger is enabled, its essentially from
218+
# system startup too.
219+
# NOTE: unless you really want a timer that only starts after system start, prefer
220+
# on_active_sec.
218221
on_startup_sec: str = None
219222

223+
# How long after the timer is started should the service unit activate?
224+
on_active_sec: str = None
225+
220226
# Uses the last activation time of this timer's UNIT (not the timer). If a unit is activated
221227
# by another means (e.g. systemctl start ...), the timer's next run time will be affected.
222228
# The unit's runtime DOES NOT affect the timer's next run time unless it's still running.
@@ -280,7 +286,7 @@ def __post_init__(self):
280286
self.fixed_random_delay = True
281287

282288
if self.start_delay:
283-
self.on_startup_sec = self.start_delay
289+
self.on_active_sec = self.start_delay
284290

285291
if self.run_daily:
286292
assert not any((self.run_every, self.run_delay))
@@ -304,11 +310,12 @@ def __post_init__(self):
304310
if self.run_delay:
305311
self.on_unit_inactive_sec = self.run_delay
306312

307-
if (self.on_unit_active_sec or self.on_unit_inactive_sec) and not self.on_startup_sec:
313+
if (self.on_unit_active_sec or self.on_unit_inactive_sec) and not self.on_active_sec:
308314
# A timer that depends on a unit's last run will never fire the first time on it's own.
309-
# OnStartupSec is applied retroactively effectively causing the timer to start
310-
# immediately upon it's creation.
311-
self.on_startup_sec = '1'
315+
# Configure the timer to kick off the unit immediately upon timer activation so the
316+
# service unit runs once and then the other configurations will take care of how
317+
# often it is ran.
318+
self.on_active_sec = '1'
312319

313320
@property
314321
def exec_start(self):
@@ -340,6 +347,7 @@ def timer(self):
340347
self.option(lines, 'AccuracySec')
341348
self.option(lines, 'OnCalendar')
342349
self.option(lines, 'Persistent')
350+
self.option(lines, 'OnActiveSec')
343351
self.option(lines, 'OnStartupSec')
344352
self.option(lines, 'OnUnitActiveSec')
345353
self.option(lines, 'OnUnitInactiveSec')

src/sysdi_tests/systemd-units/aliases.timer

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description=Harvest Sync
33

44
[Timer]
55
AccuracySec=60s
6-
OnStartupSec=5s
6+
OnActiveSec=5s
77
OnUnitActiveSec=5m
88
OnUnitInactiveSec=30s
99

src/sysdi_tests/systemd-units/defaults.timer

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description=check defaults
33

44
[Timer]
55
AccuracySec=60s
6-
OnStartupSec=15m
6+
OnActiveSec=15m
77
OnUnitActiveSec=2h
88
RandomizedDelaySec=5m
99
FixedRandomDelay=true

src/sysdi_tests/test_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_defaults(self):
4949
tu = TimedUnit(
5050
'check defaults',
5151
'foo bar',
52-
on_startup_sec='15m',
52+
on_active_sec='15m',
5353
on_unit_active_sec='2h',
5454
randomized_delay_sec='5m',
5555
)
@@ -60,7 +60,7 @@ def test_defaults(self):
6060
def test_defaults_inherited(self):
6161
@dataclass
6262
class CustomTU(TimedUnit):
63-
on_startup_sec: str = '15m'
63+
on_active_sec: str = '15m'
6464
randomized_delay_sec: str = '5m'
6565

6666
tu = CustomTU(
@@ -175,10 +175,10 @@ def test_extra_config(self):
175175

176176
def test_startup_sec_default(self):
177177
tu = TimedUnit('foo', run_every='15m')
178-
assert tu.on_startup_sec == '1'
178+
assert tu.on_active_sec == '1'
179179

180180
tu = TimedUnit('foo', run_delay='15m')
181-
assert tu.on_startup_sec == '1'
181+
assert tu.on_active_sec == '1'
182182

183183

184184
class TestManager:

0 commit comments

Comments
 (0)