Skip to content

Commit

Permalink
2.0.44
Browse files Browse the repository at this point in the history
* optimize stopping behaviour and fix error in disconnect cases
* make sure compact groups terminate correctly
  • Loading branch information
Apollon77 committed Nov 14, 2019
1 parent bc49be4 commit d470fc1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 45 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.0.44 (2019-11-14) Release Bella Stable-RC5
* (Apollon77) optimize stopping and restarting behaviour (again)

## 2.0.43 (2019-11-12) Release Bella Stable-RC4
* (Apollon77) enhance backup error handling for invalid files - they now display errors and not break the backup process
* (Apollon77) make sure new enabled/moved instances are started directly
Expand All @@ -22,7 +25,7 @@
## 2.0.40 (2019-11-08) Release Bella Stable-RC1
* (Apollon77) make sure multiple instances of same adapter do not disturb each other in compact mode, finalize compact mode
* (AlCalzone) correctly handle git commits and branches as custom install sources
* (Apollon77) * make sure callbacks with setImmediate return the rela values from code execution and not pot. changed values from other actions (fixing file content being undefined when write/read happen in parallel)
* (Apollon77) make sure callbacks with setImmediate return the real values from code execution and not pot. changed values from other actions (fixing file content being undefined when write/read happen in parallel)
* (Apollon77) handle more cases for aliases automatically (On/Ein is converted to true case insensitive now)
* (bluefox) getForeignStates() return correct ids when used with aliases
* (Apollon77) optimize controller group stopping
Expand Down
14 changes: 13 additions & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"common": {
"name": "js-controller",
"version": "2.0.43",
"version": "2.0.44",
"platform": "Javascript/Node.js",
"controller": true,
"title": "JS controller",
Expand All @@ -18,6 +18,18 @@
"zh-cn": "JS控制器"
},
"news": {
"2.0.44": {
"en": "optimizations and bugfixes",
"de": "Optimierungen und Bugfixes",
"ru": "оптимизации и исправления",
"pt": "otimizações e correções de bugs",
"nl": "optimalisaties en bugfixes",
"fr": "optimisations et corrections de bugs",
"it": "ottimizzazioni e correzioni di bug",
"es": "optimizaciones y correcciones de errores",
"pl": "optymalizacje i poprawki błędów",
"zh-cn": "优化和错误修正"
},
"2.0.43": {
"en": "optimizations and bugfixes",
"de": "Optimierungen und Bugfixes",
Expand Down
107 changes: 66 additions & 41 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ let lastCalculationOfIps = null;
let lastDiskSizeCheck = 0;
let restartTimeout = null;
let connectTimeout = null;
let reportInterval = null;

const procs = {};
const hostAdapter = {};
Expand Down Expand Up @@ -244,27 +245,16 @@ function handleDisconnect() {

connected = false;
logger.warn(hostLogPrefix + ' Slave controller detected disconnection. Stop all instances.');
stopInstances(true, () => {
// if during stopping the DB has connection again
if (connected && !isStopping) {
logger.warn(hostLogPrefix + ' Slave controller has connection again ... restarting');
getInstances();
startAliveInterval();
initMessageQueue();
} else
if (!isStopping) {
if (compactGroupController) {
process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED);
}
else {
restartTimeout = setTimeout(() => {
processMessage({command: 'cmdExec', message: {data: '_restart'}});
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
}, 10000);
}
stop(true, () => {
if (compactGroupController) {
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
} else {
restartTimeout = setTimeout(() => {
processMessage({command: 'cmdExec', message: {data: '_restart'}});
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
}, 10000);
}
});

}

function createStates(onConnect) {
Expand Down Expand Up @@ -616,7 +606,7 @@ function startAliveInterval() {
val: config.system.compact || false
});
}
setInterval(reportStatus, config.system.statisticsInterval);
reportInterval = setInterval(reportStatus, config.system.statisticsInterval);
reportStatus();
tools.measureEventLoopLag(1000, lag => eventLoopLags.push(lag));
}
Expand Down Expand Up @@ -1012,6 +1002,7 @@ function collectDiagInfo(type, callback) {

// check if some IPv4 address found. If not try in 30 seconds one more time (max 10 times)
function setIPs(ipList) {
if (isStopping) return;
const _ipList = ipList || getIPs();

// check if IPs detected (because of DHCP delay)
Expand All @@ -1030,8 +1021,15 @@ function setIPs(ipList) {
objects.getObject('system.host.' + hostname, (err, oldObj) => {
const networkInterfaces = os.networkInterfaces();

if (JSON.stringify(oldObj.native.hardware.networkInterfaces) !== JSON.stringify(networkInterfaces) ||
JSON.stringify(oldObj.common.address) !== JSON.stringify(_ipList)) {
if (
!err &&
oldObj &&
oldObj.common &&
(
JSON.stringify(oldObj.native.hardware.networkInterfaces) !== JSON.stringify(networkInterfaces) ||
JSON.stringify(oldObj.common.address) !== JSON.stringify(_ipList)
)
) {
oldObj.common.address = _ipList;
oldObj.native.hardware.networkInterfaces = networkInterfaces;
oldObj.from = hostObjectPrefix;
Expand Down Expand Up @@ -2861,7 +2859,7 @@ function startInstance(id, wakeUp) {
for (const i in compactProcs) {
if (!compactProcs.hasOwnProperty(i)) continue;
if (compactProcs[i].process) {
logger.silly(hostLogPrefix + ' ' + compactProcs[i].config.common.name + ' still running (compact)');
logger.silly(hostLogPrefix + ' Compact group ' + i + ' still running');
return;
}
}
Expand Down Expand Up @@ -2942,11 +2940,14 @@ function startInstance(id, wakeUp) {
decache = decache || require('decache');
decache(fileNameFull);

procs[id].process = require(fileNameFull)({
logLevel,
compactInstance: _instance,
compact: true
});
procs[id].process = {
logic: require(fileNameFull)({
logLevel,
compactInstance: _instance,
compact: true
})
};
procs[id].process.logic.on('exit', exitHandler);

procs[id].startedInCompactMode = true;
} catch (e) {
Expand Down Expand Up @@ -3014,13 +3015,13 @@ function startInstance(id, wakeUp) {
delete compactProcs[currentCompactGroup].process;
}

function markCompactInstancesAsStopped(groupId, callback) {
if (!compactProcs[groupId].instances.length) {
function markCompactInstancesAsStopped(instances, callback) {
if (!instances.length) {
callback && callback();
return;
}

const id = compactProcs[groupId].instances.shift();
const id = instances.shift();
outputCount += 2;
states.setState(id + '.alive', {val: false, ack: true, from: hostObjectPrefix});
states.setState(id + '.connected', {val: false, ack: true, from: hostObjectPrefix});
Expand All @@ -3034,12 +3035,15 @@ function startInstance(id, wakeUp) {
if (procs[id] && procs[id].process) {
delete procs[id].process;
}
markCompactInstancesAsStopped(groupId, callback);

markCompactInstancesAsStopped(instances, callback);
});
}

// mark all instances that should be handled by this controller also as not running.
markCompactInstancesAsStopped(currentCompactGroup, () => {
const killedInstances = [];
compactProcs[currentCompactGroup].instances.forEach(el => killedInstances.push(el));
markCompactInstancesAsStopped(killedInstances, () => {
// show stored errors
cleanErrors(compactProcs[currentCompactGroup], null, true);

Expand All @@ -3055,7 +3059,7 @@ function startInstance(id, wakeUp) {
for (const i in compactProcs) {
if (!compactProcs.hasOwnProperty(i)) continue;
if (compactProcs[i].process) {
logger.silly(hostLogPrefix + ' ' + compactProcs[i].config.common.name + ' still running (compact)');
logger.silly(hostLogPrefix + ' Compact group ' + i + ' still running (compact)');
return;
}
}
Expand Down Expand Up @@ -3127,7 +3131,7 @@ function startInstance(id, wakeUp) {

storePids(); // Store all pids to make possible kill them all

!procs[id].startedAsCompactGroup && procs[id].process && procs[id].process.on('exit', exitHandler);
!procs[id].startedInCompactMode && !procs[id].startedAsCompactGroup && procs[id].process && procs[id].process.on('exit', exitHandler);

if (!wakeUp && procs[id] && procs[id].process && procs[id].config.common && procs[id].config.common.enabled && (!procs[id].config.common.webExtension || !procs[id].config.native.webInstance) && mode !== 'once') {
if (procs[id].startedInCompactMode) {
Expand Down Expand Up @@ -3215,7 +3219,7 @@ function stopInstance(id, force, callback) {
callback = force;
force = false;
}
logger.info(hostLogPrefix + ' stopInstance ' + id + ' (process=' + (procs[id].process ? 'true' : 'false') + ')');
logger.info(hostLogPrefix + ' stopInstance ' + id + ' (force=' + force + ', process=' + (procs[id].process ? 'true' : 'false') + ')');
if (!procs[id]) {
logger.warn(hostLogPrefix + ' unknown instance ' + id);
return typeof callback === 'function' && callback();
Expand Down Expand Up @@ -3502,6 +3506,9 @@ function stopInstances(forceStop, callback) {
if (!compactProcs.hasOwnProperty(id)) continue;
if (compactProcs[id].process) compactProcs[id].process.kill(); // TODO better?
}
if (forceStop) {
allInstancesStopped = true;
}
}

waitForInstances();
Expand All @@ -3520,7 +3527,10 @@ function stopInstances(forceStop, callback) {
}, stopTimeout);
}

function stop() {
function stop(force, callback) {
if (force === undefined) {
force = false
}
if (mhService) {
mhService.close();
mhService = null;
Expand All @@ -3531,12 +3541,22 @@ function stop() {
updateIPsTimer = null;
}

stopInstances(false, wasForced => {
if (reportInterval) {
clearInterval(reportInterval);
reportInterval = null;
}

stopInstances(force, wasForced => {
if (objects && objects.destroy) objects.destroy();

if (!states) {
if (!states || force) {
logger.info(hostLogPrefix + ' ' + (wasForced ? 'force terminating' : 'terminated') + '. Could not reset alive status for instances');
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
if (typeof callback === 'function') {
return void callback();
}
else {
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
}
return;
}
outputCount++;
Expand All @@ -3559,7 +3579,12 @@ function stop() {
}
}
states && states.destroy && states.destroy();
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
if (typeof callback === 'function') {
return void callback();
}
else {
setTimeout(() => process.exit(EXIT_CODES.JS_CONTROLLER_STOPPED), 1000);
}
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.js-controller",
"version": "2.0.43",
"version": "2.0.44",
"engines": {
"node": ">=8.0.0"
},
Expand All @@ -21,7 +21,7 @@
"debug": "^4.1.1",
"decache": "^4.5.1",
"event-stream": "^4.0.1",
"iobroker.objects-redis": "^1.2.3",
"iobroker.objects-redis": "^1.2.4",
"ioredis": "^4.14.1",
"jsonwebtoken": "^8.5.1",
"jszip": "^3.2.2",
Expand Down

0 comments on commit d470fc1

Please sign in to comment.