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 21, 2024
1 parent 3c96204 commit 46eb038
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ const AllExtensions = [
type ExtensionArgs = [Zigbee, MQTT, State, PublishEntityState, EventBus,
(enable: boolean, name: string) => Promise<void>, () => void, (extension: Extension) => Promise<void>];

let sdNotify: any = null;

Check failure on line 42 in lib/controller.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type
try {
sdNotify = process.env.NOTIFY_SOCKET ? require('sd-notify') : null;
} catch {

Check failure on line 45 in lib/controller.ts

View workflow job for this annotation

GitHub Actions / ci

Empty block statement
}

export class Controller {
private eventBus: EventBus;
private zigbee: Zigbee;
Expand Down Expand Up @@ -162,6 +168,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 +198,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 +216,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 @@ -111,5 +111,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

0 comments on commit 46eb038

Please sign in to comment.