Skip to content

Commit

Permalink
fix(server): add existing check in creating phase of tasks (#990)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Mar 31, 2023
1 parent b7f75cf commit 8444d80
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 70 deletions.
21 changes: 8 additions & 13 deletions server/src/gateway/apisix.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,13 @@ export class ApisixService {
const conf = region.gatewayConf
const api_url = `${conf.apiUrl}/routes/${id}`

try {
const res = await this.httpService.axiosRef.put(api_url, data, {
headers: {
'X-API-KEY': conf.apiKey,
'Content-Type': 'application/json',
},
})
return res.data
} catch (error) {
this.logger.error(error, error.response.data)
return null
}
const res = await this.httpService.axiosRef.put(api_url, data, {
headers: {
'X-API-KEY': conf.apiKey,
'Content-Type': 'application/json',
},
})
return res.data
}

async getRoute(region: Region, id: string) {
Expand All @@ -183,7 +178,7 @@ export class ApisixService {
return null
}
this.logger.error(error, error.response?.data)
return error
throw error
}
}

Expand Down
24 changes: 13 additions & 11 deletions server/src/gateway/bucket-domain-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,27 @@ export class BucketDomainTaskService {
const region = await this.regionService.findByAppId(doc.appid)
assert(region, 'region not found')

// create route first
const route = await this.apisixService.createBucketRoute(
region,
doc.bucketName,
doc.domain,
)

this.logger.debug('bucket route created:', route)
// create route if not exists
const id = `bucket-${doc.bucketName}`
const route = await this.apisixService.getRoute(region, id)
if (!route) {
await await this.apisixService.createBucketRoute(
region,
doc.bucketName,
doc.domain,
)
this.logger.log('bucket route created:' + doc.domain)
}

// update phase to `Created`
const updated = await db.collection<BucketDomain>('BucketDomain').updateOne(
await db.collection<BucketDomain>('BucketDomain').updateOne(
{ _id: doc._id, phase: DomainPhase.Creating },
{
$set: { phase: DomainPhase.Created, lockedAt: TASK_LOCK_INIT_TIME },
},
)

if (updated.modifiedCount > 0)
this.logger.debug('bucket domain phase updated to Created', doc)
this.logger.log('bucket domain phase updated to Created: ' + doc.domain)
}

/**
Expand Down
49 changes: 17 additions & 32 deletions server/src/gateway/runtime-domain-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,9 @@ export class RuntimeDomainTaskService {
.findOneAndUpdate(
{
phase: DomainPhase.Creating,
lockedAt: {
$lt: new Date(Date.now() - 1000 * this.lockTimeout),
},
},
{
$set: {
lockedAt: new Date(),
},
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{ $set: { lockedAt: new Date() } },
)

if (!res.value) return
Expand All @@ -72,33 +66,24 @@ export class RuntimeDomainTaskService {
const region = await this.regionService.findByAppId(doc.appid)
assert(region, 'region not found')

// create route first
const route = await this.apisixService.createAppRoute(
region,
doc.appid,
doc.domain,
)

this.logger.debug('app route created:', route)
// create route if not exists
const id = `app-${doc.appid}`
const route = await this.apisixService.getRoute(region, id)
if (!route) {
await this.apisixService.createAppRoute(region, doc.appid, doc.domain)
this.logger.log('app route created: ' + doc.appid)
this.logger.debug(route)
}

// update phase to `Created`
const updated = await db
.collection<RuntimeDomain>('RuntimeDomain')
.updateOne(
{
_id: doc._id,
phase: DomainPhase.Creating,
},
{
$set: {
phase: DomainPhase.Created,
lockedAt: TASK_LOCK_INIT_TIME,
},
},
)
await db.collection<RuntimeDomain>('RuntimeDomain').updateOne(
{ _id: doc._id, phase: DomainPhase.Creating },
{
$set: { phase: DomainPhase.Created, lockedAt: TASK_LOCK_INIT_TIME },
},
)

if (updated.modifiedCount > 0)
this.logger.debug('app domain phase updated to Created ' + doc.domain)
this.logger.log('app domain phase updated to Created ' + doc.domain)
}

/**
Expand Down
28 changes: 14 additions & 14 deletions server/src/gateway/website-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
DomainState,
WebsiteHosting,
} from '@prisma/client'
import { times } from 'lodash'
import { ServerConfig, TASK_LOCK_INIT_TIME } from 'src/constants'
import { SystemDatabase } from 'src/database/system-database'
import { RegionService } from 'src/region/region.service'
Expand Down Expand Up @@ -35,10 +34,10 @@ export class WebsiteTaskService {
}

// Phase `Creating` -> `Created`
times(this.concurrency, () => this.handleCreatingPhase())
this.handleCreatingPhase()

// Phase `Deleting` -> `Deleted`
times(this.concurrency, () => this.handleDeletingPhase())
this.handleDeletingPhase()

// Phase `Created` -> `Deleting`
this.handleInactiveState()
Expand Down Expand Up @@ -89,13 +88,17 @@ export class WebsiteTaskService {

assert(bucketDomain, 'bucket domain not found')

// create website route
const route = await this.apisixService.createWebsiteRoute(
region,
site,
bucketDomain.domain,
)
this.logger.log(`create website route: ${route?.node?.key}`)
// create website route if not exists
const route = await this.apisixService.getRoute(region, site._id.toString())
if (!route) {
const res = await this.apisixService.createWebsiteRoute(
region,
site,
bucketDomain.domain,
)
this.logger.log(`create website route: ${site._id}`)
this.logger.debug(res)
}

// create website custom certificate if custom domain is set
if (site.isCustom) {
Expand Down Expand Up @@ -138,10 +141,7 @@ export class WebsiteTaskService {

// update phase to `Created`
await db.collection<WebsiteHosting>('WebsiteHosting').updateOne(
{
_id: site._id,
phase: DomainPhase.Creating,
},
{ _id: site._id, phase: DomainPhase.Creating },
{
$set: {
phase: DomainPhase.Created,
Expand Down

0 comments on commit 8444d80

Please sign in to comment.