@@ -5,27 +5,39 @@ import { verifyDomainDNS, issueDomainCertificate, checkCertificateStatus, getVal
5
5
export async function domainVerification ( { data : { domainId } , boss } ) {
6
6
const models = createPrisma ( { connectionParams : { connection_limit : 1 } } )
7
7
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
22
33
}
23
34
}
24
35
25
36
async function verifyDomain ( domain , models ) {
26
37
// track verification
27
38
const data = { ...domain , lastVerifiedAt : new Date ( ) }
28
39
data . verification = data . verification || { dns : { } , ssl : { } }
40
+ data . failedAttempts = data . failedAttempts || 0
29
41
30
42
if ( data . verification ?. dns ?. state !== 'VERIFIED' ) {
31
43
await verifyDNS ( data )
@@ -43,7 +55,7 @@ async function verifyDomain (domain, models) {
43
55
data . failedAttempts += 1
44
56
data . updatedAt = new Date ( )
45
57
const oneDayAgo = new Date ( Date . now ( ) - 24 * 60 * 60 * 1000 )
46
- // todo: change this
58
+ // TODO: discussion
47
59
if ( data . failedAttempts > 10 && data . updatedAt < oneDayAgo ) {
48
60
data . status = 'HOLD'
49
61
}
@@ -52,6 +64,7 @@ async function verifyDomain (domain, models) {
52
64
if ( data . verification ?. dns ?. state === 'VERIFIED' && data . verification ?. ssl ?. state === 'VERIFIED' ) {
53
65
data . status = 'ACTIVE'
54
66
}
67
+
55
68
return await models . customDomain . update ( { where : { id : domain . id } , data } )
56
69
}
57
70
0 commit comments