fix: Notify systemd for start, stop, watchdog (#20482)

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
chrthi
2024-01-21 21:49:17 +01:00
committed by GitHub
co-authored by Koen Kanters
parent 032557b1db
commit 97eac16aaf
5 changed files with 56 additions and 1 deletions
+18
View File
@@ -39,6 +39,14 @@ const AllExtensions = [
type ExtensionArgs = [Zigbee, MQTT, State, PublishEntityState, EventBus,
(enable: boolean, name: string) => Promise<void>, () => void, (extension: Extension) => Promise<void>];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let sdNotify: any = null;
try {
sdNotify = process.env.NOTIFY_SOCKET ? require('sd-notify') : null;
} catch {
// sd-notify is optional
}
export class Controller {
private eventBus: EventBus;
private zigbee: Zigbee;
@@ -162,6 +170,12 @@ export class Controller {
(data) => utils.publishLastSeen(data, settings.get(), false, this.publishEntityState));
logger.info(`Zigbee2MQTT started!`);
const watchdogInterval = sdNotify?.watchdogInterval() || 0;
if (watchdogInterval > 0) {
sdNotify.startWatchdogMode(Math.floor(watchdogInterval / 2));
}
sdNotify?.ready();
}
@bind async enableDisableExtension(enable: boolean, name: string): Promise<void> {
@@ -186,6 +200,8 @@ export class Controller {
}
async stop(restart = false): Promise<void> {
sdNotify?.stopping();
// Call extensions
await this.callExtensions('stop', this.extensions);
this.eventBus.removeListeners(this);
@@ -202,6 +218,8 @@ export class Controller {
logger.error('Failed to stop Zigbee2MQTT');
await this.exit(1, restart);
}
sdNotify?.stopWatchdogMode();
}
async exit(code: number, restart = false): Promise<void> {
+21
View File
@@ -67,6 +67,9 @@
},
"engines": {
"node": "^18 || ^20 || ^21"
},
"optionalDependencies": {
"sd-notify": "^2.8.0"
}
},
"node_modules/@aashutoshrathi/word-wrap": {
@@ -8763,6 +8766,24 @@
"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,
"optional": true,
"os": [
"linux",
"darwin",
"win32"
],
"dependencies": {
"bindings": "1.5.0"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
+3
View File
@@ -109,5 +109,8 @@
},
"bin": {
"zigbee2mqtt": "cli.js"
},
"optionalDependencies": {
"sd-notify": "^2.8.0"
}
}
+3 -1
View File
@@ -36,10 +36,12 @@ 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
WatchdogSec=10s
Restart=always
User=pi
+11
View File
@@ -1,3 +1,4 @@
process.env.NOTIFY_SOCKET = "mocked";
const data = require('./stub/data');
const logger = require('./stub/logger');
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
@@ -16,6 +17,16 @@ const mocksClear = [
const fs = require('fs');
jest.mock('sd-notify', () => {
return {
watchdogInterval: () => {return 3000;},
startWatchdogMode: (interval) => {},
stopWatchdogMode: () => {},
ready: () => {},
stopping: () => {},
};
}, {virtual: true});
describe('Controller', () => {
let controller;
let mockExit;