service: add new RestartMode option

When this option is set to direct, the service restarts without entering a failed
state. Dependent units are not notified of transitory failure.

This is useful for the following use case:

We have a target with Requires=my-service, After=my-service.
my-service.service is a oneshot service and has Restart=on-failure in
its definition.

my-service.service can get stuck for various reasons and time out, in
which case it is restarted. Currently, when it fails the first time, the
target fails, even though my-service is restarted.

The behavior we're looking for is that until my-service is not restarted
anymore, the target stays pending waiting for my-service.service to
start successfully or fail without being restarted anymore.
This commit is contained in:
Richard Phibel
2023-07-06 14:33:52 +02:00
parent 5bc9ea070f
commit e568fea9fc
20 changed files with 129 additions and 1 deletions
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Description=Fail on restart
StartLimitIntervalSec=1m
StartLimitBurst=3
[Service]
Type=oneshot
ExecStart=false
Restart=on-failure
RestartMode=direct
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Requires=fails-on-restart-restartdirect.service
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Description=Fail on restart
StartLimitIntervalSec=1m
StartLimitBurst=3
[Service]
Type=oneshot
ExecStart=false
Restart=on-failure
RestartMode=normal
+3
View File
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Requires=fails-on-restart.service
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/tests/testdata/testsuite-03.units/succeeds-on-restart.sh
Restart=on-failure
RestartMode=direct
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Requires=succeeds-on-restart-restartdirect.service
+6
View File
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/tests/testdata/testsuite-03.units/succeeds-on-restart.sh
Restart=on-failure
RestartMode=normal
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
if [[ ! -f "/succeeds-on-restart.ko" ]]
then
touch "/succeeds-on-restart.ko"
exit 1
else
rm "/succeeds-on-restart.ko"
exit 0
fi
+3
View File
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Requires=succeeds-on-restart.service
+13
View File
@@ -153,4 +153,17 @@ systemctl restart propagatestopto-indirect.target
assert_rc 3 systemctl --quiet is-active propagatestopto-and-pullin.target
assert_rc 3 systemctl --quiet is-active sleep-infinity-simple.service
# Test restart mode direct
systemctl start succeeds-on-restart-restartdirect.target
assert_rc 0 systemctl --quiet is-active succeeds-on-restart-restartdirect.target
systemctl start fails-on-restart-restartdirect.target || :
assert_rc 3 systemctl --quiet is-active fails-on-restart-restartdirect.target
systemctl start succeeds-on-restart.target || :
assert_rc 3 systemctl --quiet is-active succeeds-on-restart.target
systemctl start fails-on-restart.target || :
assert_rc 3 systemctl --quiet is-active fails-on-restart.target
touch /testok