Skip to content

Commit

Permalink
fix(winservice): Better handling of services that don't exist
Browse files Browse the repository at this point in the history
or cannot be reached. Fixers #864
  • Loading branch information
Göran Sander committed Dec 6, 2023
1 parent 6299a2c commit 56a18de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/lib/service_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ const serviceMonitorMqttSend2 = (config, logger, svc) => {
};

const verifyServicesExist = async (config, logger) => {
logger.info('Verifying that all Windows services specified in config file exist and can be reached.');

// Return false if one or more services do not exist or cannot be reached.
// Return true if all services are reachable.
let result = false;
let result = true;

const hostsToCheck = config.get('Butler.serviceMonitor.monitor');

Expand All @@ -132,13 +134,25 @@ const verifyServicesExist = async (config, logger) => {

// eslint-disable-next-line no-restricted-syntax
for (const service of servicesToCheck) {
// eslint-disable-next-line no-await-in-loop
const serviceExists = await svcTools.exists(service.name, host.host);
if (serviceExists) {
result = true;
let serviceExists;
try {
// eslint-disable-next-line no-await-in-loop
serviceExists = await svcTools.exists(service.name, host.host);
} catch (err) {
logger.error(`Error verifying existence and reachability of service ${service.name} on host ${host.host}: ${err}`);
result = false;
}

logger.verbose(`Windows service ${service.name} (="${service.friendlyName}") on host ${host.host} exists: ${serviceExists}`);
if (serviceExists) {
logger.verbose(
`Windows service ${service.name} (="${service.friendlyName}") on host ${host.host} exists: ${serviceExists}`
);
} else {
logger.error(
`Windows service ${service.name} (="${service.friendlyName}") on host ${host.host} does not exist or cannot be reached.`
);
result = false;
}
}
}

Expand Down Expand Up @@ -539,7 +553,7 @@ async function setupServiceMonitorTimer(config, logger) {
}
} else {
logger.error(
'At least one Windows service does not exist or could not be reached. Will not check any Windows services from here on.'
'At least one Windows service does not exist or could not be reached. Monitoring of Windows services is disabled.'
);
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/winsvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function all(host = null) {
// Create promise
return new Promise((resolve, reject) => {
let command = '';

if (host === null) {
// Run command for get states of all services on local machine
command = 'sc.exe query state= all';
Expand All @@ -21,6 +22,9 @@ function all(host = null) {
exec(command, (err, stdout) => {
// On error, reject and exit
if (err) {
if (stdout) {
reject(stdout);
}
reject(err);
return;
}
Expand Down Expand Up @@ -55,6 +59,8 @@ function exists(serviceName, host = null) {
return;
}

// Is host reachable?

// Get all services
all(host).then(
// On success, check
Expand Down

0 comments on commit 56a18de

Please sign in to comment.