From 163eef58c2b8175ac147e5745a6054d2b25194d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Sun, 11 Apr 2021 13:14:35 +0100 Subject: [PATCH] Business errors differenciated from technical errors in the Lambda runner --- .dockerignore | 6 +++- bin/lambda.js | 34 ++++++++++++++++++---- lib/index.js | 4 +-- lib/screenshotHandler.js | 14 ++++----- lib/server/controllers/awsApiController.js | 3 +- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/.dockerignore b/.dockerignore index 40b878d..106f9d5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,5 @@ -node_modules/ \ No newline at end of file +node_modules/ +results/ +test/ +doc/ +front/ \ No newline at end of file diff --git a/bin/lambda.js b/bin/lambda.js index 20f58e2..ca51801 100644 --- a/bin/lambda.js +++ b/bin/lambda.js @@ -6,18 +6,42 @@ const ylt = require('..'); async function runner({id, url, options = {}}, context) { console.log(`Processing run #${id} on ${url}`); + // AWS S3 bucket and path const bucket = process.env.RESULT_BUCKET_NAME; const keyPrefix = `results/${id}`; + // Function that can save any file on S3 (JSON, screenshot,...) const saveFile = async (path, content) => s3.putObject({Bucket: bucket, Key: `${keyPrefix}/${path}`, Body: content}) .promise(); - const results = JSON.stringify(await ylt(url, {...options, saveFile})); - results.runId = id; + // Let's launch ylt + const result = await ylt(url, {...options, saveScreenshotFn: saveFile}) - await saveFile('results.json', results); + .then(async data => { + console.log(`Run succeeded`); + + data.runId = id; + await saveFile('results.json', JSON.stringify(data)); + + return { + status: 'processed', + id, + bucket, + keyPrefix + }; + }) - return {status: 'processed', id, bucket, keyPrefix}; + .fail(error => { + console.log(`Run failed with error: ${error}`); + return { + status: 'failed', + id, + bucket, + errorMessage: error + }; + }); + + return result; } -module.exports = {runner} \ No newline at end of file +module.exports = {runner}; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 06fcf9f..0d5bc77 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,7 +30,7 @@ var yellowLabTools = function(url, options) { .then(function(data) { // If a screenshot saveFunction was provided in the options - if (typeof options.saveFile === 'function') { + if (typeof options.saveScreenshotFn === 'function') { debug('Now optimizing screenshot...'); // Remove uneeded temp screenshot path @@ -42,7 +42,7 @@ var yellowLabTools = function(url, options) { .then(function(screenshotBuffer) { debug('Screenshot optimized, now saving...'); - return options.saveFile('screenshot.jpg', screenshotBuffer); + return options.saveScreenshotFn('screenshot.jpg', screenshotBuffer); }) .then(function(response) { diff --git a/lib/screenshotHandler.js b/lib/screenshotHandler.js index 8221145..e57ce3f 100644 --- a/lib/screenshotHandler.js +++ b/lib/screenshotHandler.js @@ -94,15 +94,15 @@ var screenshotHandler = function() { this.deleteTmpFile = function(tmpFilePath) { var deferred = Q.defer(); - fs.unlink(this.getTmpFileRelativePath(), function (err) { - if (err) { - debug('Screenshot temporary file not found, could not be deleted. But it is not a problem.'); - } else { - debug('Screenshot temporary file deleted.'); - } + //fs.unlink(this.getTmpFileRelativePath(), function (err) { + // if (err) { + // debug('Screenshot temporary file not found, could not be deleted. But it is not a problem.'); + // } else { + // debug('Screenshot temporary file deleted.'); + // } deferred.resolve(); - }); + //}); return deferred.promise; }; diff --git a/lib/server/controllers/awsApiController.js b/lib/server/controllers/awsApiController.js index ac82b83..28963a2 100644 --- a/lib/server/controllers/awsApiController.js +++ b/lib/server/controllers/awsApiController.js @@ -102,7 +102,8 @@ var ApiController = function(app) { if (response.StatusCode === 200 && response.Payload && response.Payload !== 'null') { const payload = JSON.parse(response.Payload); - if (payload.errorType) { + if (payload.status === 'failed') { + debug('Failed with error %s', payload.errorMessage); runsDatastore.markAsFailed(run.runId, payload.errorMessage); } else { debug('Success!');