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 7, 2024
1 parent 3d8f0fc commit e619c56
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import utils from './util/utils';
import stringify from 'json-stable-stringify-without-jsonify';
import assert from 'assert';
import bind from 'bind-decorator';
import {optionalRequire} from 'optional-require';

// Extensions
import ExtensionFrontend from './extension/frontend';
Expand Down Expand Up @@ -39,6 +40,8 @@ const AllExtensions = [
type ExtensionArgs = [Zigbee, MQTT, State, PublishEntityState, EventBus,
(enable: boolean, name: string) => Promise<void>, () => void, (extension: Extension) => Promise<void>];

const sdNotify = process.env.NOTIFY_SOCKET ? optionalRequire('sd-notify') : null;

export class Controller {
private eventBus: EventBus;
private zigbee: Zigbee;
Expand Down Expand Up @@ -165,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 @@ -189,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 @@ -205,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
41 changes: 41 additions & 0 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"moment": "^2.30.1",
"mqtt": "^5.3.4",
"object-assign-deep": "^0.4.0",
"optional-require": "^1.1.8",
"rimraf": "^5.0.5",
"semver": "^7.5.4",
"source-map-support": "^0.5.21",
Expand Down Expand Up @@ -111,5 +112,8 @@
},
"bin": {
"zigbee2mqtt": "cli.js"
},
"optionalDependencies": {
"sd-notify": "^2.8.0"
}
}
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
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 e619c56

Please sign in to comment.