Skip to content

Commit

Permalink
feat: Notify systemd for start, stop, watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
chrthi committed Jan 1, 2024
1 parent 3d8f0fc commit dfff208
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ export class Controller {
(data) => utils.publishLastSeen(data, settings.get(), false, this.publishEntityState));

logger.info(`Zigbee2MQTT started!`);

if (process.env.NOTIFY_SOCKET) {
const notify = require('sd-notify');
const watchdogInterval = notify.watchdogInterval();
if (watchdogInterval > 0) {
const interval = Math.floor(watchdogInterval / 2);
notify.startWatchdogMode(interval);
}
notify.ready();
}
}

@bind async enableDisableExtension(enable: boolean, name: string): Promise<void> {
Expand All @@ -189,6 +199,10 @@ export class Controller {
}

async stop(restart = false): Promise<void> {
if (process.env.NOTIFY_SOCKET) {
require('sd-notify').stopping();
}

// Call extensions
await this.callExtensions('stop', this.extensions);
this.eventBus.removeListeners(this);
Expand All @@ -205,6 +219,10 @@ export class Controller {
logger.error('Failed to stop Zigbee2MQTT');
await this.exit(1, restart);
}

if (process.env.NOTIFY_SOCKET) {
require('sd-notify').stopWatchdogMode();
}
}

async exit(code: number, restart = false): Promise<void> {
Expand Down
22 changes: 19 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"mqtt": "^5.3.4",
"object-assign-deep": "^0.4.0",
"rimraf": "^5.0.5",
"sd-notify": "^2.8.0",
"semver": "^7.5.4",
"source-map-support": "^0.5.21",
"uri-js": "^4.4.1",
Expand Down
3 changes: 2 additions & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Description=zigbee2mqtt
After=network.target
[Service]
ExecStart=/usr/bin/npm start
Type=notify
ExecStart=/usr/bin/node index.js
WorkingDirectory=/opt/zigbee2mqtt
StandardOutput=inherit
StandardError=inherit
Expand Down

0 comments on commit dfff208

Please sign in to comment.