Skip to content

Commit 8788e10

Browse files
committed
use pgboss exponential backoff for domainVerification jobs
1 parent 5dfefa1 commit 8788e10

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

api/resolvers/domain.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ export default {
7878
}
7979
})
8080

81-
// schedule domain verification in 5 seconds, worker will do the rest
82-
await models.$executeRaw`INSERT INTO pgboss.job (name, data)
83-
VALUES ('domainVerification', jsonb_build_object('domainId', ${updatedDomain.id}::INTEGER))`
81+
// schedule domain verification in 5 seconds, apply exponential backoff and keep it for 2 days
82+
await models.$executeRaw`INSERT INTO pgboss.job (name, data, retrylimit, retrybackoff, retrydelay, startafter, keepuntil)
83+
VALUES ('domainVerification',
84+
jsonb_build_object('domainId', ${updatedDomain.id}::INTEGER),
85+
21,
86+
true,
87+
'30', -- 30 seconds of delay between retries
88+
now() + interval '5 seconds',
89+
now() + interval '2 days')`
90+
8491
return updatedDomain
8592
} else {
8693
try {

worker/domainVerification.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,39 @@ import { verifyDomainDNS, issueDomainCertificate, checkCertificateStatus, getVal
55
export async function domainVerification ({ data: { domainId }, boss }) {
66
const models = createPrisma({ connectionParams: { connection_limit: 1 } })
77
console.log('domainVerification', domainId)
8-
const domain = await models.customDomain.findUnique({ where: { id: domainId } })
9-
console.log('domain', domain)
10-
const result = await verifyDomain(domain, models)
11-
12-
let startAfter = null
13-
if (result?.failedAttempts < 5) {
14-
// every 30 seconds
15-
startAfter = new Date(Date.now() + 30 * 1000)
16-
} else {
17-
// every 10 minutes
18-
startAfter = new Date(Date.now() + 10 * 60 * 1000)
19-
}
20-
if (result?.status === 'PENDING') {
21-
await boss.send('domainVerification', { domainId }, { startAfter })
8+
try {
9+
const domain = await models.customDomain.findUnique({ where: { id: domainId } })
10+
11+
if (!domain) {
12+
throw new Error(`domain with ID ${domainId} not found`)
13+
}
14+
15+
const result = await verifyDomain(domain, models)
16+
17+
if (result?.status === 'ACTIVE') {
18+
console.log(`domain ${domain.domain} verified`)
19+
return
20+
}
21+
22+
if (result?.status === 'HOLD') {
23+
console.log(`domain ${domain.domain} is on hold after too many failed attempts`)
24+
return
25+
}
26+
27+
if (result?.status === 'PENDING') {
28+
throw new Error(`domain ${domain.domain} is still pending verification, will retry`)
29+
}
30+
} catch (error) {
31+
console.error(`couldn't verify domain with ID ${domainId}: ${error.message}`)
32+
throw error
2233
}
2334
}
2435

2536
async function verifyDomain (domain, models) {
2637
// track verification
2738
const data = { ...domain, lastVerifiedAt: new Date() }
2839
data.verification = data.verification || { dns: {}, ssl: {} }
40+
data.failedAttempts = data.failedAttempts || 0
2941

3042
if (data.verification?.dns?.state !== 'VERIFIED') {
3143
await verifyDNS(data)
@@ -43,7 +55,7 @@ async function verifyDomain (domain, models) {
4355
data.failedAttempts += 1
4456
data.updatedAt = new Date()
4557
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000)
46-
// todo: change this
58+
// TODO: discussion
4759
if (data.failedAttempts > 10 && data.updatedAt < oneDayAgo) {
4860
data.status = 'HOLD'
4961
}
@@ -52,6 +64,7 @@ async function verifyDomain (domain, models) {
5264
if (data.verification?.dns?.state === 'VERIFIED' && data.verification?.ssl?.state === 'VERIFIED') {
5365
data.status = 'ACTIVE'
5466
}
67+
5568
return await models.customDomain.update({ where: { id: domain.id }, data })
5669
}
5770

0 commit comments

Comments
 (0)