From f0f94d660094aa2cf980a01510fd56ec2324087d Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Thu, 5 Jan 2023 13:32:12 +0100 Subject: [PATCH 1/2] Prevent crash in bad timing cases Resolves error cases like ``` ENOENT: no such file or directory, unlink '/opt/iobroker/log/iobroker.2023-01-04.log' Error: ENOENT: no such file or directory, unlink '/opt/iobroker/log/iobroker.2023-01-04.log' at Object.unlinkSync (node:fs:1767:3) at WriteStream. (/opt/iobroker/node_modules/winston-daily-rotate-file/daily-rotate-file.js:140:28) at WriteStream.emit (node:events:525:35) at WriteStream.emit (node:domain:489:12) at finish (node:internal/streams/writable:756:10) at finishMaybe (node:internal/streams/writable:741:9) at afterWrite (node:internal/streams/writable:506:3) at onwrite (node:internal/streams/writable:479:7) at node:internal/fs/streams:416:5 at FSReqCallback.wrapper [as oncomplete] (node:fs:816:5) ``` --- daily-rotate-file.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/daily-rotate-file.js b/daily-rotate-file.js index 19adc62..036a7cf 100644 --- a/daily-rotate-file.js +++ b/daily-rotate-file.js @@ -136,8 +136,12 @@ var DailyRotateFile = function (options) { var inp = fs.createReadStream(oldFile); var out = fs.createWriteStream(oldFile + '.gz'); inp.pipe(gzip).pipe(out).on('finish', function () { - if (fs.existsSync(oldFile)) { - fs.unlinkSync(oldFile); + try { + if (fs.existsSync(oldFile)) { + fs.unlinkSync(oldFile); + } + } catch (err) { + // ignore becauuse should only happen in bad timing cases } self.emit('archive', oldFile + '.gz'); }); From bc6e7b63fbba10a1d5c89fe5dbae132c0edc655c Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Thu, 5 Jan 2023 14:24:16 +0100 Subject: [PATCH 2/2] if we ignore the error we also do not need the exists check --- daily-rotate-file.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/daily-rotate-file.js b/daily-rotate-file.js index 036a7cf..06ec76f 100644 --- a/daily-rotate-file.js +++ b/daily-rotate-file.js @@ -137,9 +137,7 @@ var DailyRotateFile = function (options) { var out = fs.createWriteStream(oldFile + '.gz'); inp.pipe(gzip).pipe(out).on('finish', function () { try { - if (fs.existsSync(oldFile)) { - fs.unlinkSync(oldFile); - } + fs.unlinkSync(oldFile); } catch (err) { // ignore becauuse should only happen in bad timing cases }