From 3d8c0b3e2005ad4cb4280e980e6f7f257becc442 Mon Sep 17 00:00:00 2001 From: alexpozzi Date: Thu, 27 May 2021 11:28:36 +0200 Subject: [PATCH] Add job timeout configuration --- .env.dist | 1 + src/configuration.js | 3 +++ src/configuration.test.js | 13 +++++++++++++ src/manager/http-server/index.js | 4 ++-- src/manager/http-server/middlewares/render.js | 5 +++-- src/queue.js | 1 - src/worker/renderers/chrome/index.js | 1 + 7 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.env.dist b/.env.dist index 035217c2..a0cb7e0a 100644 --- a/.env.dist +++ b/.env.dist @@ -3,6 +3,7 @@ LOG_LEVEL=DEBUG # Queue configurations QUEUE_REDIS_DSN=redis://redis:6379 +QUEUE_JOB_TIMEOUT=30000 # Manager configurations MANAGER_ENABLED=1 diff --git a/src/configuration.js b/src/configuration.js index 86769968..9d359a13 100644 --- a/src/configuration.js +++ b/src/configuration.js @@ -76,6 +76,9 @@ const generate = () => ({ }, queue: { redis_dsn: process.env.QUEUE_REDIS_DSN, + job: { + timeout: Number(process.env.QUEUE_JOB_TIMEOUT ?? 30000), + }, }, manager: { enabled: 1 === Number(process.env.MANAGER_ENABLED ?? 1), diff --git a/src/configuration.test.js b/src/configuration.test.js index 96fb2efe..40c72ab1 100644 --- a/src/configuration.test.js +++ b/src/configuration.test.js @@ -28,6 +28,9 @@ describe('configuration :: createConfiguration', () => { }, queue: { redis_dsn: 'redis://redis:6379', + job: { + timeout: 30000, + }, }, worker: { enabled: true, @@ -61,6 +64,9 @@ describe('configuration :: createConfiguration', () => { }, queue: { redis_dsn: 'redis://redis:6379', + job: { + timeout: 30000, + }, }, worker: { enabled: true, @@ -94,6 +100,9 @@ describe('configuration :: createConfiguration', () => { }, queue: { redis_dsn: 'redis://redis:6379', + job: { + timeout: 30000, + }, }, worker: { enabled: false, @@ -113,6 +122,7 @@ describe('configuration :: createConfiguration', () => { it(`creates a configuration`, () => { process.env.LOG_LEVEL = 'ERROR' process.env.QUEUE_REDIS_DSN = 'redis://redis:6379' + process.env.QUEUE_JOB_TIMEOUT = 5000 process.env.MANAGER_ENABLED = 1 process.env.MANAGER_HTTP_SERVER_PORT = 8081 process.env.MANAGER_HTTP_SERVER_HOST = '127.0.0.1' @@ -134,6 +144,9 @@ describe('configuration :: createConfiguration', () => { }, queue: { redis_dsn: 'redis://redis:6379', + job: { + timeout: 5000, + }, }, worker: { enabled: true, diff --git a/src/manager/http-server/index.js b/src/manager/http-server/index.js index 8e718cb8..e89f6ac8 100644 --- a/src/manager/http-server/index.js +++ b/src/manager/http-server/index.js @@ -2,9 +2,9 @@ import attachRenderMiddleware from './middlewares/render' import express from 'express' import { pipe } from 'ramda' -// createHttpServer => (Configuration, Logger, Queue) -> HttpServer +// createHttpServer => (Configuration, Logger, Queue, RequestRegistry) -> HttpServer export default (configuration, logger, queue, requestRegistry) => pipe( - attachRenderMiddleware(logger, queue, requestRegistry), + attachRenderMiddleware(configuration, logger, queue, requestRegistry), app => app.listen( configuration.manager.http_server.port, configuration.manager.http_server.host, diff --git a/src/manager/http-server/middlewares/render.js b/src/manager/http-server/middlewares/render.js index e2e2a07d..1de89c50 100644 --- a/src/manager/http-server/middlewares/render.js +++ b/src/manager/http-server/middlewares/render.js @@ -1,8 +1,8 @@ import { call, complement, compose, ifElse, isNil, path, pipe } from 'ramda' import { DEFAULT_JOB_OPTIONS } from '../../../queue' -// default :: (Logger, Queue, RequestRegistry) -> Express.app -> Express.app -export default (logger, queue, requestRegistry) => app => +// default :: (Configuration, Logger, Queue, RequestRegistry) -> Express.app -> Express.app +export default (configuration, logger, queue, requestRegistry) => app => app.get('/render', (req, res, next) => call(pipe( () => logger.info(`Render request for url "${req.query.url}" started.`), ifElse( @@ -13,6 +13,7 @@ export default (logger, queue, requestRegistry) => app => url: req.query.url, }, { ...DEFAULT_JOB_OPTIONS, + timeout: configuration.queue.job.timeout, jobId, }), ), diff --git a/src/queue.js b/src/queue.js index 80cedb51..0a30f05a 100644 --- a/src/queue.js +++ b/src/queue.js @@ -8,7 +8,6 @@ export const DEFAULT_QUEUE_OPTIONS = {} export const DEFAULT_JOB_OPTIONS = { attempts: 1, - timeout: 5000, removeOnComplete: true, removeOnFail: true, } diff --git a/src/worker/renderers/chrome/index.js b/src/worker/renderers/chrome/index.js index f2c9308b..748f3825 100644 --- a/src/worker/renderers/chrome/index.js +++ b/src/worker/renderers/chrome/index.js @@ -17,6 +17,7 @@ const renderPageContent = async (configuration, logger, browserInstance, url) => await page.goto(url, { waitUntil: 'networkidle0', + timeout: configuration.queue.job.timeout, }) return await page.content()