From dfff208361a4868c951fce9f84d8cf3399ac851b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Thie=C3=9Fen?= Date: Mon, 1 Jan 2024 19:08:47 +0100 Subject: [PATCH] feat: Notify systemd for start, stop, watchdog --- lib/controller.ts | 18 ++++++++++++++++++ package-lock.json | 22 +++++++++++++++++++--- package.json | 1 + scripts/install.sh | 3 ++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/controller.ts b/lib/controller.ts index 69e1e3bdae..3bfb4d4512 100644 --- a/lib/controller.ts +++ b/lib/controller.ts @@ -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 { @@ -189,6 +199,10 @@ export class Controller { } async stop(restart = false): Promise { + if (process.env.NOTIFY_SOCKET) { + require('sd-notify').stopping(); + } + // Call extensions await this.callExtensions('stop', this.extensions); this.eventBus.removeListeners(this); @@ -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 { diff --git a/package-lock.json b/package-lock.json index 1ef2fb6286..f6f5355d58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,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", @@ -3878,7 +3879,6 @@ "version": "1.5.0", "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", - "optional": true, "dependencies": { "file-uri-to-path": "1.0.0" } @@ -5271,8 +5271,7 @@ "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", - "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", - "optional": true + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" }, "node_modules/fill-range": { "version": "7.0.1", @@ -8764,6 +8763,23 @@ "node": ">=10" } }, + "node_modules/sd-notify": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/sd-notify/-/sd-notify-2.8.0.tgz", + "integrity": "sha512-e+D1v0Y6UzmqXcPlaTkHk1QMdqk36mF/jIYv5gwry/N2Tb8/UNnpfG6ktGLpeBOR6TCC5hPKgqA+0hTl9sm2tA==", + "hasInstallScript": true, + "os": [ + "linux", + "darwin", + "win32" + ], + "dependencies": { + "bindings": "1.5.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/semver": { "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", diff --git a/package.json b/package.json index 3e066f5036..68499ad495 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/install.sh b/scripts/install.sh index 00f8b76d97..af943f9ed7 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -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