Skip to content

Commit feb2c01

Browse files
committed
Using notify service type instead of simple to allow handling startup errors by systemd
1 parent 37f2fd9 commit feb2c01

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

scripts/systemd/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Requires=wifibroadcast.service
44
ReloadPropagatedFrom=wifibroadcast.service
55

66
[Service]
7-
Type=simple
7+
Type=notify
88
# common environment
99
EnvironmentFile=/etc/default/wifibroadcast
1010
# per-profile environment

wfb_ng/protocols.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
from copy import deepcopy
2828
from twisted.python import log, failure
2929
from twisted.internet import reactor, defer, threads, task
30-
from twisted.internet.protocol import ProcessProtocol, Factory
30+
from twisted.internet.protocol import ProcessProtocol, Factory, ConnectedDatagramProtocol
3131
from twisted.protocols.basic import LineReceiver, Int32StringReceiver
3232

3333
from .conf import settings
34-
34+
from .common import fatal_error
3535

3636
class BadTelemetry(Exception):
3737
pass
@@ -667,3 +667,25 @@ def start(self):
667667
childFDs={0: "w", 1: "r", 2: "r"})
668668

669669
return df.addCallback(lambda _: self.df)
670+
671+
672+
class SDNotifier(ConnectedDatagramProtocol):
673+
def startProtocol(self):
674+
log.msg("Sending notification to systemd")
675+
self.transport.write(b'READY=1')
676+
self.transport.stopListening()
677+
678+
def connectionFailed(self, reason):
679+
log.msg("Connection to systemd failed: %s" % (reason.value,), isError=1)
680+
fatal_error()
681+
682+
683+
def notify_ready():
684+
sock = os.getenv('NOTIFY_SOCKET')
685+
if sock is None :
686+
return
687+
688+
if sock[0] == '@':
689+
sock = '\0' + sock[1:]
690+
691+
return reactor.connectUNIXDatagram(sock.encode(), SDNotifier())

wfb_ng/server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from . import _log_msg, ConsoleObserver, ErrorSafeLogFile, call_and_check_rc, ExecError, version_msg
3535
from .common import abort_on_crash, exit_status, df_sleep, search_attr
36-
from .protocols import AntStatsAndSelector, RFTempMeter, SSHClientProtocol, MsgPackAPIFactory, JSONAPIFactory
36+
from .protocols import AntStatsAndSelector, RFTempMeter, SSHClientProtocol, MsgPackAPIFactory, JSONAPIFactory, notify_ready
3737
from .services import parse_services, init_udp_direct_tx, init_udp_direct_rx, init_mavlink, init_tunnel, init_udp_proxy, hash_link_domain, bandwidth_map
3838
from .cluster import parse_cluster_services, gen_cluster_scripts
3939
from .conf import settings, cfg_files
@@ -271,6 +271,8 @@ def _cleanup(x):
271271
srv_cfg.udp_peers_auto if is_cluster else wlans,
272272
link_id, ant_sel_f, is_cluster, rx_only_wlan_ids))
273273

274+
275+
notify_ready()
274276
yield defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup).addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)
275277

276278

0 commit comments

Comments
 (0)