Skip to content

Commit

Permalink
fix(mqtt): More reboust startup code for MQTT & Win svc monitoring
Browse files Browse the repository at this point in the history
Fixes #897
  • Loading branch information
Göran Sander committed Dec 7, 2023
1 parent d716cfe commit 577dba3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
47 changes: 14 additions & 33 deletions src/butler.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,39 +117,20 @@ const start = async () => {

// ------------------------------------
// Create MQTT client object and connect to MQTT broker, if MQTT is enabled
try {
await mqttInitHandlers();

// if (
// globals.config.has('Butler.mqttConfig.enable') &&
// globals.config.has('Butler.mqttConfig.brokerHost') &&
// globals.config.has('Butler.mqttConfig.brokerPort') &&
// globals.config.get('Butler.mqttConfig.enable')
// ) {
// const mqttOptions = {
// host: globals.config.get('Butler.mqttConfig.brokerHost'),
// port: globals.config.get('Butler.mqttConfig.brokerPort'),
// };

// globals.mqttClient = mqtt.connect(mqttOptions);
// /*
// Following might be needed for conecting to older Mosquitto versions
// var mqttClient = mqtt.connect('mqtt://<IP of MQTT server>', {
// protocolId: 'MQIsdp',
// protocolVersion: 3
// });
// */
// if (!globals.mqttClient.connected) {
// globals.logger.verbose(
// `CONFIG: Created (but not yet connected) MQTT object for ${mqttOptions.host}:${mqttOptions.port}, protocol version ${mqttOptions.protocolVersion}`
// );
// }
// } else {
// globals.logger.info('CONFIG: MQTT disabled, not connecting to MQTT broker');
// }
} catch (err) {
globals.logger.error(`CONFIG: Could not set up MQTT: ${JSON.stringify(err, null, 2)}`);
}
mqttInitHandlers();

// Sleep 5 seconds to allow MQTT to connect
globals.logger.info('MAIN: Sleeping 5 seconds to allow MQTT to connect.');
globals.logger.info('5...');
await globals.sleep(1000);
globals.logger.info('4...');
await globals.sleep(1000);
globals.logger.info('3...');
await globals.sleep(1000);
globals.logger.info('2...');
await globals.sleep(1000);
globals.logger.info('1...');
await globals.sleep(1000);

// Set up service monitoring, if enabled in the config file
if (globals.config.has('Butler.serviceMonitor.enable') && globals.config.get('Butler.serviceMonitor.enable') === true) {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/mqtt_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { config, logger, execPath } = require('../globals');
const globals = require('../globals');
const qrsUtil = require('../qrs_util');

async function mqttInitHandlers() {
function mqttInitHandlers() {
try {
let mqttClient;
let mqttOptions;
Expand All @@ -24,6 +24,8 @@ async function mqttInitHandlers() {

mqttClient = mqtt.connect(mqttOptions);

globals.mqttClient = mqttClient;

/*
Following might be needed for conecting to older Mosquitto versions
var mqttClient = mqtt.connect('mqtt://<IP of MQTT server>', {
Expand Down Expand Up @@ -52,11 +54,11 @@ async function mqttInitHandlers() {
};

// Connect to MQTT broker
// mqttClient = mqtt.connect(mqttOptions);
mqttClient = mqtt.connect(
`mqtts://${config.get('Butler.mqttConfig.brokerHost')}:${config.get('Butler.mqttConfig.brokerPort')}`,
mqttOptions
);

globals.mqttClient = mqttClient;
} else {
logger.error('MQTT INIT HANDLERS: MQTT configuration error');
Expand Down
12 changes: 6 additions & 6 deletions src/udp/udp_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ const schedulerFailed = async (msg, legacyFlag) => {
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
'Butler.mqttConfig.taskFailureTopic'
)}`
);
}
Expand Down Expand Up @@ -730,7 +730,7 @@ const schedulerFailed = async (msg, legacyFlag) => {
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
'Butler.mqttConfig.taskFailureTopic'
)}`
);
}
Expand Down Expand Up @@ -931,8 +931,8 @@ module.exports.udpInitTaskErrorServer = () => {
globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'start');
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
`UDP SERVER INIT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskFailureServerStatusTopic'
)}`
);
}
Expand All @@ -952,8 +952,8 @@ module.exports.udpInitTaskErrorServer = () => {
globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
`UDP SERVER ERROR: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskFailureServerStatusTopic'
)}`
);
}
Expand Down

0 comments on commit 577dba3

Please sign in to comment.