From 9d5d2e9177a3e8a627465140f08bca56ecaf6c7a Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 28 Jan 2021 15:57:33 +0100 Subject: [PATCH] Added method prune again --- .../item-image-queue/downloadImages.js | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index d99109738..3207a57ed 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -22,7 +22,20 @@ module.exports = Self => { const tempPath = path.join(container.client.root, container.name); const maxAttempts = 3; - prune(); + const images = await Self.find({ + where: {url: {neq: null}, attempts: {eq: maxAttempts}} + }); + + for (let image of images) { + const currentStamp = new Date().getTime(); + const updatedStamp = image.updated.getTime(); + const graceTime = Math.abs(currentStamp - updatedStamp); + const maxTTL = 3600 * 48 * 1000; // 48 hours in ms; + + if (graceTime >= maxTTL) + await Self.destroyById(image.itemFk); + } + download(); async function download() { @@ -60,7 +73,6 @@ module.exports = Self => { }); writeStream.on('close', async function() { - console.log('stream closed'); try { await models.Image.registerImage('catalog', filePath, fileName, image.itemFk); await image.destroy(); @@ -79,8 +91,7 @@ module.exports = Self => { try { const row = await Self.findById(rowId); - if (!row) - throw new Error(`Could not update due error ${error}`); + if (!row) return; if (row.attempts < maxAttempts) { await row.updateAttributes({ @@ -95,7 +106,7 @@ module.exports = Self => { download(); } catch (err) { - throw new Error(`ErrorHandler error: ${err}`); + throw new Error(`Image download failed: ${err}`); } } };