Skip to content

Commit

Permalink
Update win svc monitoring wrt breaking changes in XState 5 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Göran Sander committed Feb 1, 2024
1 parent 0d01b09 commit 4ca297c
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions src/lib/service_monitor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import later from '@breejs/later';
import { createMachine, interpret } from 'xstate';
import { createMachine, createActor } from 'xstate';

import { statusAll, status, details } from './winsvc.js';
import globals from '../globals.js';
Expand Down Expand Up @@ -215,13 +215,20 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {

// Update state machine
const smService = serviceStateMachine.find((winsvc) => winsvc.id === `${host.host}|${service.name}`);
const prevState = smService.stateSvc.state.value;

const snapshotPrev = smService.stateSvc.getSnapshot();
const prevState = snapshotPrev.value;
logger.verbose(`Service "${serviceDetails.displayName}" on host "${host.host}", previous state=${prevState}`);

const sendResult = smService.stateSvc.send('STOP');
logger.verbose(`Service "${serviceDetails.displayName}" on host "${host.host}", new state=${sendResult.value}`);
smService.stateSvc.send({ type: 'STOP' });

const snapshotNew = smService.stateSvc.getSnapshot();
const newState = snapshotNew.value;
logger.verbose(`Service "${serviceDetails.displayName}" on host "${host.host}", new state=${newState}`);

if (sendResult.changed) {
// Has state changed?
const stateChanged = prevState !== newState;
if (stateChanged) {
logger.warn(`Service "${serviceDetails.displayName}" on host "${host.host}" has stopped!`);

// Send message to enabled destinations
Expand All @@ -238,8 +245,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -257,8 +264,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -274,8 +281,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -293,8 +300,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -312,8 +319,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -331,8 +338,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}
}
Expand All @@ -349,18 +356,22 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
// First check?
if (isFirstCheck) {
// Set state to running as this is the first/startup check and we don't want to alert in this case
smService.stateSvc.send('START');
smService.stateSvc.send({ type: 'START' });
}
const prevState = smService.stateSvc.state.value;

logger.verbose(
`Service "${serviceDetails.displayName}" on host "${host.host}", previous state=${smService.stateSvc.state.value}`
);
const snapshotPrev = smService.stateSvc.getSnapshot();
const prevState = snapshotPrev.value;
logger.verbose(`Service "${serviceDetails.displayName}" on host "${host.host}", previous state=${prevState}`);

smService.stateSvc.send({ type: 'START' });

const sendResult = smService.stateSvc.send('START');
logger.verbose(`Service "${serviceDetails.displayName}" on host "${host.host}", new state=${sendResult.value}`);
const snapshotNew = smService.stateSvc.getSnapshot();
const newState = snapshotNew.value;
logger.verbose(`Service "${serviceDetails.displayName}" on host "${host.host}", new state=${newState}`);

if (sendResult.changed) {
// Has state changed?
const stateChanged = prevState !== newState;
if (stateChanged) {
logger.info(`Service "${serviceDetails.displayName}" on host "${host.host}" has started.`);
// Send message to enabled destinations

Expand All @@ -376,8 +387,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -395,8 +406,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -412,8 +423,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -431,8 +442,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -450,8 +461,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}

Expand All @@ -469,8 +480,8 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
serviceDetails,
host: host.host,
prevState: prevState.toUpperCase(),
currState: sendResult.value.toUpperCase(),
stateChanged: sendResult.changed,
currState: newState.toUpperCase(),
stateChanged,
});
}
}
Expand Down Expand Up @@ -548,7 +559,6 @@ async function setupServiceMonitorTimer(config, logger) {

// Windows service states: https://learn.microsoft.com/en-us/windows/win32/services/service-status-transitions
const windowsServiceMachine = createMachine({
predictableActionArguments: true,
id: 'windowsService',
initial: 'paused',
states: {
Expand All @@ -572,12 +582,14 @@ async function setupServiceMonitorTimer(config, logger) {
},
});

const svc = interpret(windowsServiceMachine).start();
const actor = createActor(windowsServiceMachine);
const svc = actor.start();

serviceStateMachine.push({
id: `${host.host}|${service.name}`,
host,
serviceName: service.name,
actor,
stateSvc: svc,
});
}
Expand Down

0 comments on commit 4ca297c

Please sign in to comment.