-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notify systemd for start, stop, watchdog #20482
Conversation
dfff208
to
a6216a7
Compare
lib/controller.ts
Outdated
@@ -39,6 +39,16 @@ const AllExtensions = [ | |||
type ExtensionArgs = [Zigbee, MQTT, State, PublishEntityState, EventBus, | |||
(enable: boolean, name: string) => Promise<void>, () => void, (extension: Extension) => Promise<void>]; | |||
|
|||
try { | |||
if (process.env.NOTIFY_SOCKET) { | |||
var sdNotify = require('sd-notify'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use import
/from
syntax, also I suggest to use let sdNotify = null
on line 42, then you can skip the other sdNotify = null
statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, import
is a static import, so we can't do it conditionally. I tried to avoid that because sd-notify
requires the libsystemd-dev
C library to build and would be a hassle to install for Mac or Windows users. I've now switched to optional-require
which seems like a good solution to me and also simplifies this code a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems optional-require
only do a try/catch
, in my opinion not worth adding a dependency for. I think we can easily add such function to utils.ts
Could you take a look at the failing test and also make a PR for the docs? |
15568c1
to
e619c56
Compare
The "failing tests" were in fact just a failing coverage check because the CI tests run without optional dependencies. I now mock the module in the controller tests so the code is covered. I haven't added an actual test case since the application logic is rather trivial and testing end-to-end by running under systemd is the only way to be sure it really works. |
This documents the feature added in Koenkk/zigbee2mqtt#20482.
Documentation PR added: Koenkk/zigbee2mqtt.io#2475 |
e619c56
to
57490c6
Compare
Hi, I'm glad to see the progress on this task! I just wanted to share some thoughts about the dependency: It should be possible to avoid the dependency at all by using The documentation is here Note, the There is more information here. I wonder why for the solution 2 the article don't mention the |
57490c6
to
73f3498
Compare
73f3498
to
46eb038
Compare
46eb038
to
27835a0
Compare
This documents the feature added in Koenkk/zigbee2mqtt#20482.
Hi! Now node is a bit of an in-between case; it can write to unix sockets directly just fine, but they dropped support for datagram sockets -.- https://groups.google.com/g/nodejs/c/iCzhcuxGP1I so the sd-notify module that uses the libsystemd C library for that seems to be the next-best solution. |
Removed the optional-require dependency and used the try/catch suggested on https://www.npmjs.com/package/optional-require instead. Unfortunately the |
Thanks! |
Great work! |
This documents the feature added in Koenkk/zigbee2mqtt#20482.
This doesn't work in the docker images published on docker.io and I think this is because the Dockerfile's |
I've created a PR to update the documentation In order for this to work, the user needs to install the sd-notify requirements |
I was referring to the fact the Docker images built by this repo's Dockerfile don't currently include libsystemd so the sd_notify stuff doesn't work. Sure I could build my own Docker image but I'd rather just get it into this repo if that is OK. Would any committers accept a PR to remove I should say that my use case here is that I'm trying to run this (and mosquitto which also doesn't have it enabled in their Docker images) in Podman via systemd and whenever I configured it wrong not having |
As already suggested in #20413, this PR implements
Type=notify
. This makes zigbee2mqtt send a notification to systemd when it has finished starting up and when it starts shutting down. My use case for example was to add anExecStartPost=
line to the service file that changes the ACL of the web interface socket. For this I needed a reliable way to know when this socket has actually been created.It is also possible to specify a watchdog timeout in the service file; in this case zigbee2mqtt will regularly notify systemd that it's main loop is still alive, and systemd will restart it if it fails to do so for too long.
One drawback is that requiring
sd-notify
adds a dependency on thelibsystemd-dev
deb package. So I've made thesd-notify
dependency optional in an attempt to not break builds on other platforms. This might need some refinement or updates to the install instructions. The deb package might also need to be added to the Docker images.