From 6f3fb83cd75b121f703e979888b0eb05068fabbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 27 Aug 2020 13:53:11 +0200 Subject: [PATCH 1/5] Changes --- back/models/image.js | 4 +++- .../back/methods/item-image-queue/downloadImages.js | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/back/models/image.js b/back/models/image.js index 340b2e5a6..313e4492b 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -90,7 +90,9 @@ module.exports = Self => { ); } - await fs.unlink(srcFilePath); + if (fs.existsSync(filePath)) + await fs.unlink(srcFilePath); + await tx.commit(); return newImage; } catch (e) { diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index c49cb0702..d3fd739cc 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -19,7 +19,6 @@ module.exports = Self => { Self.downloadImages = async() => { const models = Self.app.models; - let image; try { const tempPath = path.join('/tmp/salix-image'); @@ -27,7 +26,7 @@ module.exports = Self => { await fs.mkdir(tempPath, {recursive: true}); const timer = setInterval(async() => { - image = await Self.findOne({where: {error: null}}); + const image = await Self.findOne({where: {error: null}}); // Exit loop if (!image) return clearInterval(timer); @@ -62,9 +61,9 @@ module.exports = Self => { await errorHandler(image.itemFk, error, filePath); } }); - }, 1500); + }, 2500); } catch (error) { - await errorHandler(image.itemFk, error); + throw new Error('Try-catch error: ', error); } async function errorHandler(rowId, error, filePath) { @@ -76,10 +75,10 @@ module.exports = Self => { await row.updateAttribute('error', error); - if (filePath) + if (filePath && fs.existsSync(filePath)) await fs.unlink(filePath); } catch (error) { - throw error; + throw new Error('ErrorHandler error: ', error); } } }; From 0c4939830bd27897ed44217b0628c1900900141f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 27 Aug 2020 14:00:40 +0200 Subject: [PATCH 2/5] Fixed error --- back/models/image.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/models/image.js b/back/models/image.js index 313e4492b..113bc7084 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -90,7 +90,7 @@ module.exports = Self => { ); } - if (fs.existsSync(filePath)) + if (fs.existsSync(srcFilePath)) await fs.unlink(srcFilePath); await tx.commit(); From fd67a85fe8bdcae4c14bb849b61d91cfd9398858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 27 Aug 2020 14:09:21 +0200 Subject: [PATCH 3/5] Changed to 1 second --- modules/item/back/methods/item-image-queue/downloadImages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index d3fd739cc..febd0289d 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -61,7 +61,7 @@ module.exports = Self => { await errorHandler(image.itemFk, error, filePath); } }); - }, 2500); + }, 1000); } catch (error) { throw new Error('Try-catch error: ', error); } From 55d047c0dd40d61b3cdb6f188cc9c1f7940c5cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 27 Aug 2020 14:11:22 +0200 Subject: [PATCH 4/5] Renamed error var --- modules/item/back/methods/item-image-queue/downloadImages.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index febd0289d..71af07265 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -77,8 +77,8 @@ module.exports = Self => { if (filePath && fs.existsSync(filePath)) await fs.unlink(filePath); - } catch (error) { - throw new Error('ErrorHandler error: ', error); + } catch (err) { + throw new Error('ErrorHandler error: ', err); } } }; From a75938b6d07196126cc2f5eefd74862114df5006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Thu, 27 Aug 2020 14:23:40 +0200 Subject: [PATCH 5/5] Updated error handler --- modules/item/back/methods/item-image-queue/downloadImages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index 71af07265..420b357a5 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -78,7 +78,7 @@ module.exports = Self => { if (filePath && fs.existsSync(filePath)) await fs.unlink(filePath); } catch (err) { - throw new Error('ErrorHandler error: ', err); + throw new Error(`ErrorHandler error: ${err}`); } } };