Skip to content

Commit

Permalink
Merge pull request #31 from KnpLabs/fix/add-job-timeout-configuration
Browse files Browse the repository at this point in the history
Add job timeout configuration
  • Loading branch information
hlegay authored May 27, 2021
2 parents 1b1ee8f + 3d8c0b3 commit 8ded449
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ LOG_LEVEL=DEBUG

# Queue configurations
QUEUE_REDIS_DSN=redis://redis:6379
QUEUE_JOB_TIMEOUT=30000

# Manager configurations
MANAGER_ENABLED=1
Expand Down
3 changes: 3 additions & 0 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions src/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ describe('configuration :: createConfiguration', () => {
},
queue: {
redis_dsn: 'redis://redis:6379',
job: {
timeout: 30000,
},
},
worker: {
enabled: true,
Expand Down Expand Up @@ -61,6 +64,9 @@ describe('configuration :: createConfiguration', () => {
},
queue: {
redis_dsn: 'redis://redis:6379',
job: {
timeout: 30000,
},
},
worker: {
enabled: true,
Expand Down Expand Up @@ -94,6 +100,9 @@ describe('configuration :: createConfiguration', () => {
},
queue: {
redis_dsn: 'redis://redis:6379',
job: {
timeout: 30000,
},
},
worker: {
enabled: false,
Expand All @@ -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'
Expand All @@ -134,6 +144,9 @@ describe('configuration :: createConfiguration', () => {
},
queue: {
redis_dsn: 'redis://redis:6379',
job: {
timeout: 5000,
},
},
worker: {
enabled: true,
Expand Down
4 changes: 2 additions & 2 deletions src/manager/http-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/manager/http-server/middlewares/render.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -13,6 +13,7 @@ export default (logger, queue, requestRegistry) => app =>
url: req.query.url,
}, {
...DEFAULT_JOB_OPTIONS,
timeout: configuration.queue.job.timeout,
jobId,
}),
),
Expand Down
1 change: 0 additions & 1 deletion src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const DEFAULT_QUEUE_OPTIONS = {}

export const DEFAULT_JOB_OPTIONS = {
attempts: 1,
timeout: 5000,
removeOnComplete: true,
removeOnFail: true,
}
Expand Down
1 change: 1 addition & 0 deletions src/worker/renderers/chrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8ded449

Please sign in to comment.