From 8444d804edd39bd0ac7fb09133f691f7e914f419 Mon Sep 17 00:00:00 2001 From: maslow Date: Fri, 31 Mar 2023 12:27:21 +0800 Subject: [PATCH] fix(server): add existing check in creating phase of tasks (#990) --- server/src/gateway/apisix.service.ts | 21 +++----- .../src/gateway/bucket-domain-task.service.ts | 24 ++++----- .../gateway/runtime-domain-task.service.ts | 49 +++++++------------ server/src/gateway/website-task.service.ts | 28 +++++------ 4 files changed, 52 insertions(+), 70 deletions(-) diff --git a/server/src/gateway/apisix.service.ts b/server/src/gateway/apisix.service.ts index 2d0cb705c5..19ef5a0455 100644 --- a/server/src/gateway/apisix.service.ts +++ b/server/src/gateway/apisix.service.ts @@ -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) { @@ -183,7 +178,7 @@ export class ApisixService { return null } this.logger.error(error, error.response?.data) - return error + throw error } } diff --git a/server/src/gateway/bucket-domain-task.service.ts b/server/src/gateway/bucket-domain-task.service.ts index 9d70a613a9..83e6259fc3 100644 --- a/server/src/gateway/bucket-domain-task.service.ts +++ b/server/src/gateway/bucket-domain-task.service.ts @@ -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').updateOne( + await db.collection('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) } /** diff --git a/server/src/gateway/runtime-domain-task.service.ts b/server/src/gateway/runtime-domain-task.service.ts index afcbd2a27f..08c21fd9ec 100644 --- a/server/src/gateway/runtime-domain-task.service.ts +++ b/server/src/gateway/runtime-domain-task.service.ts @@ -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 @@ -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') - .updateOne( - { - _id: doc._id, - phase: DomainPhase.Creating, - }, - { - $set: { - phase: DomainPhase.Created, - lockedAt: TASK_LOCK_INIT_TIME, - }, - }, - ) + await db.collection('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) } /** diff --git a/server/src/gateway/website-task.service.ts b/server/src/gateway/website-task.service.ts index f2ff5dc894..ea1a3328a6 100644 --- a/server/src/gateway/website-task.service.ts +++ b/server/src/gateway/website-task.service.ts @@ -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' @@ -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() @@ -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) { @@ -138,10 +141,7 @@ export class WebsiteTaskService { // update phase to `Created` await db.collection('WebsiteHosting').updateOne( - { - _id: site._id, - phase: DomainPhase.Creating, - }, + { _id: site._id, phase: DomainPhase.Creating }, { $set: { phase: DomainPhase.Created,