Skip to content

Commit 46b579d

Browse files
fix several Sentry issues (#1696)
* * fix several Sentry issues: IOBROKER-JS-CONTROLLER-215, IOBROKER-JS-CONTROLLER-27G, IOBROKER-JS-CONTROLLER-24K, IOBROKER-JS-CONTROLLER-279, IOBROKER-JS-CONTROLLER-270, IOBROKER-JS-CONTROLLER-263, IOBROKER-JS-CONTROLLER-251, IOBROKER-JS-CONTROLLER-26M, IOBROKER-JS-CONTROLLER-25Y, IOBROKER-JS-CONTROLLER-253, IOBROKER-JS-CONTROLLER-24R, IOBROKER-JS-CONTROLLER-252, and some more * * fix IOBROKER-JS-CONTROLLER-234 * * fix IOBROKER-JS-CONTROLLER-234 * Update packages/controller/main.js Co-authored-by: Max Hauser <[email protected]> Co-authored-by: Max Hauser <[email protected]>
1 parent df746a8 commit 46b579d

File tree

6 files changed

+127
-82
lines changed

6 files changed

+127
-82
lines changed

packages/common/lib/common/logger.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ const IoSeq =
8282

8383
// we add own properties
8484
ioInfo.props.Hostname = tools.getHostName();
85-
const msgParts = ioInfo.message.match(/^([^.]+\.[0-9]+) \(([^)]+)\) (.*)$/);
86-
if (msgParts) {
87-
ioInfo.props.Source = msgParts[1];
88-
ioInfo.props.Pid = msgParts[2];
89-
} else {
90-
ioInfo.props.Source = 'js-controller';
85+
if (ioInfo.message) {
86+
const msgParts = ioInfo.message.match(/^([^.]+\.[0-9]+) \(([^)]+)\) (.*)$/);
87+
if (msgParts) {
88+
ioInfo.props.Source = msgParts[1];
89+
ioInfo.props.Pid = msgParts[2];
90+
} else {
91+
ioInfo.props.Source = 'js-controller';
92+
}
9193
}
9294
super.log(ioInfo, callback);
9395
}

packages/common/lib/common/tools.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,15 +3373,15 @@ async function getInstancesOrderedByStartPrio(objects, logger, logPrefix = '') {
33733373
endkey: 'system.adapter.\u9999'
33743374
});
33753375
} catch (e) {
3376-
if (e.message.startsWith('Cannot find ')) {
3377-
logger.error(`${logPrefix}_design/system missing - call node ${module.exports.appName}.js setup`);
3376+
if (e.message && e.message.startsWith('Cannot find ')) {
3377+
logger.error(`${logPrefix} _design/system missing - call node ${module.exports.appName}.js setup`);
33783378
} else {
3379-
logger.error(`${logPrefix}Can not get instances: ${e.message}`);
3379+
logger.error(`${logPrefix} Can not get instances: ${e.message}`);
33803380
}
33813381
}
33823382

33833383
if (!doc.rows || doc.rows.length === 0) {
3384-
logger.info(`${logPrefix}no instances found`);
3384+
logger.info(`${logPrefix} no instances found`);
33853385
} else {
33863386
for (const row of doc.rows) {
33873387
if (row && row.value) {
@@ -3594,7 +3594,11 @@ function compressFileGZip(inputFilename, outputFilename, options = {}) {
35943594
});
35953595
output.on('close', () => {
35963596
if (deleteInput) {
3597-
fs.unlinkSync(inputFilename);
3597+
try {
3598+
fs.unlinkSync(inputFilename);
3599+
} catch {
3600+
// Ignore
3601+
}
35983602
}
35993603
resolve();
36003604
});

packages/controller/lib/www/js/visUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function getUsedObjectIDs(views, isByViews) {
235235
_views[view] = [];
236236
}
237237

238-
if (!tools.isObject(views[view].widgets)) {
238+
if (!tools.isObject(views[view]) || !tools.isObject(views[view].widgets)) {
239239
continue;
240240
}
241241
for (id of Object.keys(views[view].widgets)) {

packages/controller/main.js

Lines changed: 105 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ function createObjects(onConnect) {
685685
) {
686686
procs[id].restartExpected = true;
687687
stopInstance(id, async () => {
688+
if (!procs[id]) {
689+
return;
690+
}
688691
const _ipArr = tools.findIPs();
689692

690693
if (checkAndAddInstance(procs[id].config, _ipArr)) {
@@ -900,27 +903,43 @@ function reportStatus() {
900903
// elapsed: 6650000, // ms since the start of the process
901904
// timestamp: 864000000 // ms since epoch
902905
// }
903-
pidUsage(process.pid, (err, stats) => {
904-
// controller.s might be stopped, but this is still running
905-
if (!err && states && states.setState && stats) {
906-
states.setState(id + '.cpu', { ack: true, from: id, val: Math.round(100 * parseFloat(stats.cpu)) / 100 });
907-
states.setState(id + '.cputime', { ack: true, from: id, val: stats.ctime / 1000 });
908-
outputCount += 2;
909-
}
910-
});
906+
try {
907+
pidUsage(process.pid, (err, stats) => {
908+
// controller.s might be stopped, but this is still running
909+
if (!err && states && states.setState && stats) {
910+
states.setState(id + '.cpu', {
911+
ack: true,
912+
from: id,
913+
val: Math.round(100 * parseFloat(stats.cpu)) / 100
914+
});
915+
states.setState(id + '.cputime', { ack: true, from: id, val: stats.ctime / 1000 });
916+
outputCount += 2;
917+
}
918+
});
919+
} catch (e) {
920+
logger.error(`${hostLogPrefix} Cannot read pidUsage data : ${e.message}`);
921+
}
911922

912-
const mem = process.memoryUsage();
913-
states.setState(`${id}.memRss`, { val: Math.round(mem.rss / 10485.76 /* 1MB / 100 */) / 100, ack: true, from: id });
914-
states.setState(`${id}.memHeapTotal`, {
915-
val: Math.round(mem.heapTotal / 10485.76 /* 1MB / 100 */) / 100,
916-
ack: true,
917-
from: id
918-
});
919-
states.setState(id + '.memHeapUsed', {
920-
val: Math.round(mem.heapUsed / 10485.76 /* 1MB / 100 */) / 100,
921-
ack: true,
922-
from: id
923-
});
923+
try {
924+
const mem = process.memoryUsage();
925+
states.setState(`${id}.memRss`, {
926+
val: Math.round(mem.rss / 10485.76 /* 1MB / 100 */) / 100,
927+
ack: true,
928+
from: id
929+
});
930+
states.setState(`${id}.memHeapTotal`, {
931+
val: Math.round(mem.heapTotal / 10485.76 /* 1MB / 100 */) / 100,
932+
ack: true,
933+
from: id
934+
});
935+
states.setState(id + '.memHeapUsed', {
936+
val: Math.round(mem.heapUsed / 10485.76 /* 1MB / 100 */) / 100,
937+
ack: true,
938+
from: id
939+
});
940+
} catch (e) {
941+
logger.error(`${hostLogPrefix} Cannot read memoryUsage data: ${e.message}`);
942+
}
924943

925944
// provide machine infos
926945

@@ -941,8 +960,8 @@ function reportStatus() {
941960
});
942961
outputCount++;
943962
}
944-
} catch (err) {
945-
logger.error(`${hostLogPrefix} Cannot read /proc/meminfo: ${err}`);
963+
} catch (e) {
964+
logger.error(`${hostLogPrefix} Cannot read /proc/meminfo: ${e.message}`);
946965
}
947966
}
948967

@@ -967,7 +986,7 @@ function reportStatus() {
967986
outputCount += 2;
968987
}
969988
} catch (e) {
970-
logger.error(`${hostLogPrefix} Cannot read disk information: ${e}`);
989+
logger.error(`${hostLogPrefix} Cannot read disk information: ${e.message}`);
971990
}
972991
});
973992
}
@@ -1082,7 +1101,7 @@ function cleanAutoSubscribes(instance, callback) {
10821101
if (res && res.rows) {
10831102
for (let c = res.rows.length - 1; c >= 0; c--) {
10841103
// remove this instance from autoSubscribe
1085-
if (res.rows[c].value && res.rows[c].value.common.subscribable) {
1104+
if (res.rows[c].value && res.rows[c].value.common && res.rows[c].value.common.subscribable) {
10861105
count++;
10871106
cleanAutoSubscribe(instance, res.rows[c].id, () => !--count && callback && callback());
10881107
}
@@ -2230,31 +2249,36 @@ async function processMessage(msg) {
22302249
}
22312250
logger.info(`${hostLogPrefix} ${tools.appName} ${args.slice(1).join(' ')}`);
22322251

2233-
const child = spawn('node', args, { windowsHide: true });
2234-
if (child.stdout) {
2235-
child.stdout.on('data', data => {
2236-
data = data.toString().replace(/\n/g, '');
2237-
logger.info(hostLogPrefix + ' ' + tools.appName + ' ' + data);
2238-
msg.from && sendTo(msg.from, 'cmdStdout', { id: msg.message.id, data: data });
2239-
});
2240-
}
2252+
try {
2253+
const child = spawn('node', args, { windowsHide: true });
2254+
if (child.stdout) {
2255+
child.stdout.on('data', data => {
2256+
data = data.toString().replace(/\n/g, '');
2257+
logger.info(`${hostLogPrefix} ${tools.appName} ${data}`);
2258+
msg.from && sendTo(msg.from, 'cmdStdout', { id: msg.message.id, data: data });
2259+
});
2260+
}
22412261

2242-
if (child.stderr) {
2243-
child.stderr.on('data', data => {
2244-
data = data.toString().replace(/\n/g, '');
2245-
logger.error(`${hostLogPrefix} ${tools.appName} ${data}`);
2246-
msg.from && sendTo(msg.from, 'cmdStderr', { id: msg.message.id, data: data });
2262+
if (child.stderr) {
2263+
child.stderr.on('data', data => {
2264+
data = data.toString().replace(/\n/g, '');
2265+
logger.error(`${hostLogPrefix} ${tools.appName} ${data}`);
2266+
msg.from && sendTo(msg.from, 'cmdStderr', { id: msg.message.id, data: data });
2267+
});
2268+
}
2269+
2270+
child.on('exit', exitCode => {
2271+
logger.info(`${hostLogPrefix} ${tools.appName} exit ${exitCode}`);
2272+
if (msg.from) {
2273+
sendTo(msg.from, 'cmdExit', { id: msg.message.id, data: exitCode });
2274+
// Sometimes finished command is lost, recent it
2275+
setTimeout(() => sendTo(msg.from, 'cmdExit', { id: msg.message.id, data: exitCode }), 1000);
2276+
}
22472277
});
2278+
} catch (e) {
2279+
logger.error(`${hostLogPrefix} ${tools.appName} ${e.message}`);
2280+
msg.from && sendTo(msg.from, 'cmdStderr', { id: msg.message.id, data: e.message });
22482281
}
2249-
2250-
child.on('exit', exitCode => {
2251-
logger.info(`${hostLogPrefix} ${tools.appName} exit ${exitCode}`);
2252-
if (msg.from) {
2253-
sendTo(msg.from, 'cmdExit', { id: msg.message.id, data: exitCode });
2254-
// Sometimes finished command is lost, recent it
2255-
setTimeout(() => sendTo(msg.from, 'cmdExit', { id: msg.message.id, data: exitCode }), 1000);
2256-
}
2257-
});
22582282
}
22592283

22602284
break;
@@ -2639,30 +2663,34 @@ async function processMessage(msg) {
26392663
} while (parts.length);
26402664
}
26412665

2642-
if (fs.existsSync(filename)) {
2643-
const files = fs.readdirSync(filename);
2644-
2645-
for (const file of files) {
2646-
try {
2647-
if (!file.endsWith('-audit.json')) {
2648-
const stat = fs.lstatSync(path.join(filename, file));
2649-
if (!stat.isDirectory()) {
2650-
result.list.push({
2651-
fileName: `log/${hostname}/${transport}/${file}`,
2652-
size: stat.size
2653-
});
2666+
try {
2667+
if (fs.existsSync(filename)) {
2668+
const files = fs.readdirSync(filename);
2669+
2670+
for (const file of files) {
2671+
try {
2672+
if (!file.endsWith('-audit.json')) {
2673+
const stat = fs.lstatSync(path.join(filename, file));
2674+
if (!stat.isDirectory()) {
2675+
result.list.push({
2676+
fileName: `log/${hostname}/${transport}/${file}`,
2677+
size: stat.size
2678+
});
2679+
}
26542680
}
2681+
} catch (e) {
2682+
// push unchecked
2683+
// result.list.push('log/' + transport + '/' + files[f]);
2684+
logger.error(
2685+
`${hostLogPrefix} cannot check file: ${path.join(filename, file)} - ${
2686+
e.message
2687+
}`
2688+
);
26552689
}
2656-
} catch (e) {
2657-
// push unchecked
2658-
// result.list.push('log/' + transport + '/' + files[f]);
2659-
logger.error(
2660-
`${hostLogPrefix} cannot check file: ${path.join(filename, file)} - ${
2661-
e.message
2662-
}`
2663-
);
26642690
}
26652691
}
2692+
} catch (e) {
2693+
logger.error(`${hostLogPrefix} cannot check files: ${filename} - ${e.message}`);
26662694
}
26672695
}
26682696
}
@@ -2730,8 +2758,8 @@ async function processMessage(msg) {
27302758
os: process.platform,
27312759
Architecture: os.arch(),
27322760
CPUs: cpus.length,
2733-
Speed: cpus[0].speed,
2734-
Model: cpus[0].model,
2761+
Speed: tools.isObject(cpus[0]) ? cpus[0].speed : undefined,
2762+
Model: tools.isObject(cpus[0]) ? cpus[0].model : undefined,
27352763
RAM: os.totalmem(),
27362764
'System uptime': Math.round(os.uptime()),
27372765
'Node.js': process.version,
@@ -2842,7 +2870,7 @@ async function processMessage(msg) {
28422870
objects,
28432871
msg.message.id,
28442872
msg.message.adapter,
2845-
Buffer.from(msg.message.data, 'base64'),
2873+
Buffer.from(msg.message.data || '', 'base64'),
28462874
msg.message.options,
28472875
error => msg.callback && msg.from && sendTo(msg.from, msg.command, { error }, msg.callback)
28482876
);
@@ -2931,7 +2959,11 @@ async function processMessage(msg) {
29312959
}
29322960

29332961
case 'rebuildAdapter':
2934-
if (!installQueue.some(entry => entry.id === msg.message.id)) {
2962+
if (!msg.message.id) {
2963+
if (msg.callback && msg.from) {
2964+
sendTo(msg.from, msg.command, { error: 'Adapter to rebuild not provided.' }, msg.callback);
2965+
}
2966+
} else if (!installQueue.some(entry => entry.id === msg.message.id)) {
29352967
logger.info(`${hostLogPrefix} ${msg.message.id} will be rebuilt`);
29362968
const installObj = { id: msg.message.id, rebuild: true };
29372969
if (msg.message.rebuildArgs) {
@@ -4473,6 +4505,9 @@ async function startInstance(id, wakeUp) {
44734505
});
44744506
} else {
44754507
// a group controller for this group is not yet started, execute one
4508+
compactProcs[instance.common.compactGroup] = compactProcs[instance.common.compactGroup] || {
4509+
instances: []
4510+
};
44764511
if (!compactProcs[instance.common.compactGroup].process) {
44774512
const compactControllerArgs = [instance.common.compactGroup];
44784513

packages/db-objects-jsonl/lib/objects/objectsInMemJsonlDB.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class ObjectsInMemoryJsonlDB extends ObjectsInMemoryFileDB {
206206
}
207207

208208
async destroy() {
209+
await super.destroy();
210+
209211
if (this._db) {
210212
await this._db.close();
211213
}

packages/db-states-jsonl/lib/states/statesInMemJsonlDB.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ class StatesInMemoryJsonlDB extends StatesInMemoryFileDB {
229229
}
230230

231231
async destroy() {
232+
await super.destroy();
233+
232234
if (this._db) {
233235
await this._db.close();
234236
}

0 commit comments

Comments
 (0)