Skip to content
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

Merged
merged 2 commits into from
Jan 21, 2024
Merged
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
18 changes: 18 additions & 0 deletions lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand All @@ -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);
Expand All @@ -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> {
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,8 @@
},
"bin": {
"zigbee2mqtt": "cli.js"
},
"optionalDependencies": {
"sd-notify": "^2.8.0"
}
}
4 changes: 3 additions & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions test/controller.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
process.env.NOTIFY_SOCKET = "mocked";
const data = require('./stub/data');
const logger = require('./stub/logger');
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
Expand All @@ -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;
Expand Down