Skip to content

Commit

Permalink
fix(server): fix tasks state handler logic (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Mar 30, 2023
1 parent 206070a commit d36eab9
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 294 deletions.
55 changes: 12 additions & 43 deletions server/src/application/application-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export class ApplicationTaskService {

// State `Deleted`
this.handleDeletedState()

this.clearTimeoutLocks()
}

/**
Expand All @@ -78,20 +76,12 @@ export class ApplicationTaskService {
.findOneAndUpdate(
{
phase: ApplicationPhase.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
}
if (!res.value) return

const app = res.value
const appid = app.appid
Expand Down Expand Up @@ -145,20 +135,17 @@ export class ApplicationTaskService {
}

// update application phase to `Created`
const updated = await db.collection<Application>('Application').updateOne(
{
_id: app._id,
phase: ApplicationPhase.Creating,
},
await db.collection<Application>('Application').updateOne(
{ _id: app._id, phase: ApplicationPhase.Creating },
{
$set: {
phase: ApplicationPhase.Created,
lockedAt: TASK_LOCK_INIT_TIME,
},
},
)
if (updated.modifiedCount > 0)
this.logger.debug('app phase updated to `Created`:', app.appid)

this.logger.log('app phase updated to `Created`: ' + app.appid)
}

/**
Expand Down Expand Up @@ -275,20 +262,17 @@ export class ApplicationTaskService {
}

// update phase to `Deleted`
const updated = await db.collection<Application>('Application').updateOne(
{
_id: app._id,
phase: ApplicationPhase.Deleting,
},
await db.collection<Application>('Application').updateOne(
{ _id: app._id, phase: ApplicationPhase.Deleting },
{
$set: {
phase: ApplicationPhase.Deleted,
lockedAt: TASK_LOCK_INIT_TIME,
},
},
)
if (updated.modifiedCount > 0)
this.logger.debug('app phase updated to `Deleted`:', app.appid)

this.logger.log('app phase updated to `Deleted`: ' + app.appid)
}

/**
Expand Down Expand Up @@ -319,8 +303,7 @@ export class ApplicationTaskService {
)

await db.collection<Application>('Application').deleteMany({
// TODO: support reset app or not? keep this line now.
// state: ApplicationState.Deleted,
state: ApplicationState.Deleted,
phase: ApplicationPhase.Deleted,
})
}
Expand All @@ -334,18 +317,4 @@ export class ApplicationTaskService {
.collection<Application>('Application')
.updateOne({ appid: appid }, { $set: { lockedAt: TASK_LOCK_INIT_TIME } })
}

/**
* Clear timeout locks
*/
async clearTimeoutLocks() {
const db = SystemDatabase.db

await db
.collection<Application>('Application')
.updateMany(
{ lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) } },
{ $set: { lockedAt: TASK_LOCK_INIT_TIME } },
)
}
}
102 changes: 24 additions & 78 deletions server/src/gateway/bucket-domain-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { RegionService } from 'src/region/region.service'
import { ApisixService } from './apisix.service'
import * as assert from 'node:assert'
import { Cron, CronExpression } from '@nestjs/schedule'
import { times } from 'lodash'
import { ServerConfig, TASK_LOCK_INIT_TIME } from 'src/constants'
import { SystemDatabase } from 'src/database/system-database'

Expand All @@ -26,10 +25,10 @@ export class BucketDomainTaskService {
}

// 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 All @@ -39,9 +38,6 @@ export class BucketDomainTaskService {

// Phase `Deleting` -> `Deleted`
this.handleDeletedState()

// Clear timeout locks
this.clearTimeoutLocks()
}

/**
Expand All @@ -57,15 +53,9 @@ export class BucketDomainTaskService {
.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 @@ -85,15 +75,9 @@ export class BucketDomainTaskService {

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

Expand All @@ -114,15 +98,9 @@ export class BucketDomainTaskService {
.findOneAndUpdate(
{
phase: DomainPhase.Deleting,
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 @@ -131,25 +109,19 @@ export class BucketDomainTaskService {
const region = await this.regionService.findByAppId(doc.appid)
assert(region, 'region not found')

// delete route first
const route = await this.apisixService.deleteBucketRoute(
region,
doc.bucketName,
)

this.logger.debug('bucket route deleted:', route)
// delete route if exists
const id = `bucket-${doc.bucketName}`
const route = await this.apisixService.getRoute(region, id)
if (route) {
await this.apisixService.deleteBucketRoute(region, doc.bucketName)
this.logger.log('bucket route deleted: ' + doc.bucketName)
}

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

Expand All @@ -168,12 +140,10 @@ export class BucketDomainTaskService {
{
state: DomainState.Active,
phase: DomainPhase.Deleted,
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{
$set: {
phase: DomainPhase.Creating,
lockedAt: TASK_LOCK_INIT_TIME,
},
$set: { phase: DomainPhase.Creating, lockedAt: TASK_LOCK_INIT_TIME },
},
)
}
Expand All @@ -188,13 +158,11 @@ export class BucketDomainTaskService {
await db.collection<BucketDomain>('BucketDomain').updateMany(
{
state: DomainState.Inactive,
phase: DomainPhase.Created,
phase: { $in: [DomainPhase.Created, DomainPhase.Creating] },
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{
$set: {
phase: DomainPhase.Deleting,
lockedAt: TASK_LOCK_INIT_TIME,
},
$set: { phase: DomainPhase.Deleting, lockedAt: TASK_LOCK_INIT_TIME },
},
)
}
Expand All @@ -211,12 +179,10 @@ export class BucketDomainTaskService {
{
state: DomainState.Deleted,
phase: DomainPhase.Created,
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{
$set: {
phase: DomainPhase.Deleting,
lockedAt: TASK_LOCK_INIT_TIME,
},
$set: { phase: DomainPhase.Deleting, lockedAt: TASK_LOCK_INIT_TIME },
},
)

Expand All @@ -225,24 +191,4 @@ export class BucketDomainTaskService {
phase: DomainPhase.Deleted,
})
}

/**
* Clear timeout locks
*/
async clearTimeoutLocks() {
const db = SystemDatabase.db

await db.collection<BucketDomain>('BucketDomain').updateMany(
{
lockedAt: {
$lt: new Date(Date.now() - 1000 * this.lockTimeout),
},
},
{
$set: {
lockedAt: TASK_LOCK_INIT_TIME,
},
},
)
}
}
Loading

0 comments on commit d36eab9

Please sign in to comment.