Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to disable queue workers #507

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type StorageConfigType = {
logflareApiKey?: string
logflareSourceToken?: string
pgQueueEnable: boolean
pgQueueEnableWorkers?: boolean
pgQueueConnectionURL?: string
pgQueueDeleteAfterDays?: number
pgQueueArchiveCompletedAfterSeconds?: number
Expand Down Expand Up @@ -303,6 +304,7 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {

// Queue
pgQueueEnable: getOptionalConfigFromEnv('PG_QUEUE_ENABLE', 'ENABLE_QUEUE_EVENTS') === 'true',
pgQueueEnableWorkers: getOptionalConfigFromEnv('PG_QUEUE_WORKERS_ENABLE') !== 'false',
pgQueueConnectionURL: getOptionalConfigFromEnv('PG_QUEUE_CONNECTION_URL'),
pgQueueDeleteAfterDays: parseInt(
getOptionalConfigFromEnv('PG_QUEUE_DELETE_AFTER_DAYS') || '2',
Expand Down
5 changes: 4 additions & 1 deletion src/queue/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export abstract class Queue {
pgQueueDeleteAfterDays,
pgQueueArchiveCompletedAfterSeconds,
pgQueueRetentionDays,
pgQueueEnableWorkers,
} = getConfig()

let url = pgQueueConnectionURL ?? databaseURL
Expand All @@ -52,7 +53,9 @@ export abstract class Queue {
expireInHours: 48,
})

registerWorkers()
if (pgQueueEnableWorkers) {
registerWorkers()
}

Queue.pgBoss.on('error', (error) => {
logSchema.error(logger, '[Queue] error', {
Expand Down
54 changes: 54 additions & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Queue } from './queue'
import { logger, logSchema } from './monitoring'
import adminApp from './admin-app'
import { getConfig } from './config'

export async function main() {
const { requestTraceHeader, adminPort, host } = getConfig()

logger.info('[Queue] Starting Queue Worker')
const queue = await Queue.init()

const server = adminApp({
logger,
disableRequestLogging: true,
requestIdHeader: requestTraceHeader,
})

process.on('SIGTERM', async () => {
logger.info('[Worker] Stopping')
await server.close()
await Queue.stop()
})

await server.listen({ port: adminPort, host })

return new Promise<void>((resolve, reject) => {
queue.on('error', (err) => {
logger.info('[Queue] Error', err)
reject(err)
})

queue.on('stopped', () => {
logger.info('[Queue] Stopping')
resolve()
})
})
}

process.on('uncaughtException', (e) => {
logSchema.error(logger, 'uncaught exception', {
type: 'uncaughtException',
error: e,
})
logger.flush()
process.exit(1)
})

main()
.then(() => {
logger.info('[Queue] Worker Exited Successfully')
})
.catch(() => {
process.exit(1)
})
Loading