Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified setup.py
100644 → 100755
Empty file.
11 changes: 9 additions & 2 deletions systemctl_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ async def _dbus_signal_loop_unit( # pylint: disable=too-many-arguments
unit_name=unit_name
)
((_, last_active_state),) = await unit_proxy.Get(property_name="ActiveState")
await mqtt_client.publish(topic=active_state_topic, payload=last_active_state)
await mqtt_client.publish(topic=active_state_topic, payload=last_active_state, retain=True)
with dbus_router.filter(unit_properties_changed_match_rule) as queue:
while True:
await queue.get()
Expand All @@ -397,7 +397,7 @@ async def _dbus_signal_loop_unit( # pylint: disable=too-many-arguments
)
if current_active_state != last_active_state:
await mqtt_client.publish(
topic=active_state_topic, payload=current_active_state
topic=active_state_topic, payload=current_active_state, retain=True
)
last_active_state = current_active_state
queue.task_done()
Expand Down Expand Up @@ -509,6 +509,13 @@ async def _run( # pylint: disable=too-many-arguments
payload=_MQTT_PAYLOAD_NOT_AVAILABLE,
retain=True,
)

for monitored_system_unit in state.monitored_system_unit_names:
await mqtt_client.publish(
topic=state.get_system_unit_active_state_mqtt_topic(unit_name=monitored_system_unit),
payload="unknown",
retain=True,
)
Comment on lines +512 to +518
Copy link
Owner

@fphammerle fphammerle Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for monitored_system_unit in state.monitored_system_unit_names:
await mqtt_client.publish(
topic=state.get_system_unit_active_state_mqtt_topic(unit_name=monitored_system_unit),
payload="unknown",
retain=True,
)

I am not sure whether we should report "unknown" on the shutdown of systemctl-mqtt, as consumers should always subscribe to the birth/last-will topic systemctl/[hostname]/status (c3533ee) to be notified when systemctl-mqtt becomes available or unavailable (so "unknown" seems redundant to me and somehow misleadingly suggests that systemd reports "unknown").
What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was really aiming for was to clear the topic of stale services. As it stands, if you decided to change what services are monitored the retained topic stays there "forever" - this was an easy way to at least mark out topics that haven't changed since shutdown. Do you think we need to clear out stale topics or leave that as an exercise of due diligence for the user?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed your reply. Yes, I would leave clearing the topic to the user/subscriber as we can't guarantee that "unknown" will be sent (e.g. on sigkill).



def _main() -> None:
Expand Down
Loading