1
1
import { Injectable , Logger } from '@nestjs/common'
2
2
import { Cron , CronExpression } from '@nestjs/schedule'
3
- import { DomainPhase , DomainState , WebsiteHosting } from '@prisma/client'
3
+ import {
4
+ BucketDomain ,
5
+ DomainPhase ,
6
+ DomainState ,
7
+ WebsiteHosting ,
8
+ } from '@prisma/client'
4
9
import { times } from 'lodash'
5
10
import { TASK_LOCK_INIT_TIME } from 'src/constants'
6
11
import { SystemDatabase } from 'src/database/system-database'
12
+ import { RegionService } from 'src/region/region.service'
13
+ import * as assert from 'node:assert'
14
+ import { ApisixService } from './apisix.service'
7
15
8
16
@Injectable ( )
9
17
export class WebsiteTaskService {
10
18
readonly lockTimeout = 30 // in second
11
19
readonly concurrency = 1 // concurrency count
12
20
private readonly logger = new Logger ( WebsiteTaskService . name )
13
21
22
+ constructor (
23
+ private readonly regionService : RegionService ,
24
+ private readonly apisixService : ApisixService ,
25
+ ) { }
26
+
14
27
@Cron ( CronExpression . EVERY_SECOND )
15
28
async tick ( ) {
16
- return
17
29
// Phase `Creating` -> `Created`
18
30
times ( this . concurrency , ( ) => this . handleCreatingPhase ( ) )
19
31
@@ -41,7 +53,7 @@ export class WebsiteTaskService {
41
53
async handleCreatingPhase ( ) {
42
54
const db = SystemDatabase . db
43
55
44
- const doc = await db
56
+ const res = await db
45
57
. collection < WebsiteHosting > ( 'WebsiteHosting' )
46
58
. findOneAndUpdate (
47
59
{
@@ -57,10 +69,50 @@ export class WebsiteTaskService {
57
69
} ,
58
70
)
59
71
60
- if ( doc . value ) {
61
- // TODO
62
- // create website route
63
- // update phase to `Created`
72
+ if ( ! res . value ) return
73
+
74
+ this . logger . debug ( res . value )
75
+ // get region by appid
76
+ const site = res . value
77
+ const region = await this . regionService . findByAppId ( site . appid )
78
+ assert ( region , 'region not found' )
79
+
80
+ // get bucket domain
81
+ const bucketDomain = await db
82
+ . collection < BucketDomain > ( 'BucketDomain' )
83
+ . findOne ( {
84
+ appid : site . appid ,
85
+ bucketName : site . bucketName ,
86
+ } )
87
+
88
+ assert ( bucketDomain , 'bucket domain not found' )
89
+
90
+ // create website route
91
+ const route = await this . apisixService . createWebsiteRoute (
92
+ region ,
93
+ site ,
94
+ bucketDomain . domain ,
95
+ )
96
+ this . logger . debug ( `create website route: ` , route )
97
+
98
+ // update phase to `Created`
99
+ const updated = await db
100
+ . collection < WebsiteHosting > ( 'WebsiteHosting' )
101
+ . updateOne (
102
+ {
103
+ _id : site . _id ,
104
+ phase : DomainPhase . Creating ,
105
+ } ,
106
+ {
107
+ $set : {
108
+ phase : DomainPhase . Created ,
109
+ lockedAt : TASK_LOCK_INIT_TIME ,
110
+ } ,
111
+ } ,
112
+ )
113
+
114
+ if ( updated . modifiedCount !== 1 ) {
115
+ this . logger . error ( `update website hosting phase failed: ${ site . _id } ` )
64
116
}
65
117
}
66
118
@@ -72,7 +124,7 @@ export class WebsiteTaskService {
72
124
async handleDeletingPhase ( ) {
73
125
const db = SystemDatabase . db
74
126
75
- const doc = await db
127
+ const res = await db
76
128
. collection < WebsiteHosting > ( 'WebsiteHosting' )
77
129
. findOneAndUpdate (
78
130
{
@@ -88,10 +140,36 @@ export class WebsiteTaskService {
88
140
} ,
89
141
)
90
142
91
- if ( doc . value ) {
92
- // TODO
93
- // delete website route
94
- // update phase to `Deleted`
143
+ if ( ! res . value ) return
144
+
145
+ // get region by appid
146
+ const site = res . value
147
+ const region = await this . regionService . findByAppId ( site . appid )
148
+ assert ( region , 'region not found' )
149
+
150
+ // delete website route
151
+ const route = await this . apisixService . deleteWebsiteRoute ( region , site )
152
+
153
+ this . logger . debug ( `delete website route: ` , route )
154
+
155
+ // update phase to `Deleted`
156
+ const updated = await db
157
+ . collection < WebsiteHosting > ( 'WebsiteHosting' )
158
+ . updateOne (
159
+ {
160
+ _id : site . _id ,
161
+ phase : DomainPhase . Deleting ,
162
+ } ,
163
+ {
164
+ $set : {
165
+ phase : DomainPhase . Deleted ,
166
+ lockedAt : TASK_LOCK_INIT_TIME ,
167
+ } ,
168
+ } ,
169
+ )
170
+
171
+ if ( updated . modifiedCount > 1 ) {
172
+ this . logger . error ( `update website hosting phase failed: ${ site . _id } ` )
95
173
}
96
174
}
97
175
0 commit comments