Skip to content

Commit

Permalink
fix: configure pgBoss from env
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Oct 11, 2023
1 parent 9007206 commit bfa4b99
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 115 deletions.
165 changes: 56 additions & 109 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"knex": "^2.4.2",
"md5-file": "^5.0.0",
"pg": "^8.10.0",
"pg-boss": "^8.1.1",
"pg-boss": "^9.0.3",
"pg-listen": "^1.7.0",
"pino": "^8.15.4",
"pino-logflare": "^0.4.2",
Expand Down
12 changes: 12 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type StorageConfigType = {
logflareSourceToken?: string
enableQueueEvents: boolean
pgQueueConnectionURL?: string
pgQueueDeleteAfterDays?: number
pgQueueArchiveCompletedAfterSeconds?: number
pgQueueRetentionDays?: number
webhookURL?: string
webhookApiKey?: string
webhookQueuePullInterval?: number
Expand Down Expand Up @@ -147,6 +150,15 @@ export function getConfig(): StorageConfigType {
logflareSourceToken: getOptionalConfigFromEnv('LOGFLARE_SOURCE_TOKEN'),
enableQueueEvents: getOptionalConfigFromEnv('ENABLE_QUEUE_EVENTS') === 'true',
pgQueueConnectionURL: getOptionalConfigFromEnv('PG_QUEUE_CONNECTION_URL'),
pgQueueDeleteAfterDays: parseInt(
getOptionalConfigFromEnv('PG_QUEUE_DELETE_AFTER_DAYS') || '2',
10
),
pgQueueArchiveCompletedAfterSeconds: parseInt(
getOptionalConfigFromEnv('PG_QUEUE_ARCHIVE_COMPLETED_AFTER_SECONDS') || '7200',
10
),
pgQueueRetentionDays: parseInt(getOptionalConfigFromEnv('PG_QUEUE_RETENTION_DAYS') || '2', 10),
webhookURL: getOptionalConfigFromEnv('WEBHOOK_URL'),
webhookApiKey: getOptionalConfigFromEnv('WEBHOOK_API_KEY'),
webhookQueuePullInterval: parseInt(
Expand Down
1 change: 1 addition & 0 deletions src/http/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const setErrorHandler = (app: FastifyInstance) => {
'remaining connection slots are reserved for non-replication superuser connections',
'no more connections allowed',
'sorry, too many clients already',
'server login has been failing, try again later',
].some((msg) => (error as DatabaseError).message.includes(msg))
) {
return reply.status(429).send({
Expand Down
16 changes: 11 additions & 5 deletions src/queue/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ export abstract class Queue {
return Queue.pgBoss
}

const { isMultitenant, multitenantDatabaseUrl, pgQueueConnectionURL } = getConfig()
const {
isMultitenant,
multitenantDatabaseUrl,
pgQueueConnectionURL,
pgQueueDeleteAfterDays,
pgQueueArchiveCompletedAfterSeconds,
pgQueueRetentionDays,
} = getConfig()

let url = pgQueueConnectionURL ?? process.env.DATABASE_URL

Expand All @@ -31,13 +38,12 @@ export abstract class Queue {
connectionString: url,
max: 4,
application_name: 'storage-pgboss',
deleteAfterDays: 7,
archiveCompletedAfterSeconds: 14_400,
retentionDays: 7,
deleteAfterDays: pgQueueDeleteAfterDays,
archiveCompletedAfterSeconds: pgQueueArchiveCompletedAfterSeconds,
retentionDays: pgQueueRetentionDays,
retryBackoff: true,
retryLimit: 20,
expireInHours: 48,
monitorStateIntervalSeconds: 30,
})

registerWorkers()
Expand Down

0 comments on commit bfa4b99

Please sign in to comment.