Skip to content

Commit

Permalink
fix(server): some pending billing tasks not processed on time
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed May 31, 2023
1 parent 87d482a commit ce225d3
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions server/src/billing/billing-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class BillingTaskService {
constructor(private readonly billing: BillingService) {}

@Cron(CronExpression.EVERY_5_MINUTES)
async tick() {
async createBillingScheduler() {
const db = SystemDatabase.db
if (ServerConfig.DISABLED_BILLING_TASK) {
return
Expand All @@ -42,22 +42,42 @@ export class BillingTaskService {
},
})

if (total === 0) {
return
}

const concurrency = total > 30 ? 30 : total
if (total > 30) {
setTimeout(() => {
this.tick()
this.createBillingScheduler()
}, 3000)
}

times(concurrency, () => {
this.handleApplicationBillingCreating().catch((err) => {
this.logger.error('handleApplicationBillingCreating error', err)
})
})
}

@Cron(CronExpression.EVERY_SECOND)
async payBillingScheduler() {
const db = SystemDatabase.db
if (ServerConfig.DISABLED_BILLING_TASK) {
return
}

const total = await db
.collection<ApplicationBilling>('ApplicationBilling')
.countDocuments({
state: ApplicationBillingState.Pending,
lockedAt: { $lt: new Date(Date.now() - 1000 * 60) },
})

const concurrency = total > 30 ? 30 : total
if (total > 30) {
setTimeout(() => {
this.payBillingScheduler()
}, 3000)
}

times(concurrency, () => {
this.handlePendingApplicationBilling().catch((err) => {
this.logger.error('handlePendingApplicationBilling error', err)
})
Expand All @@ -73,7 +93,7 @@ export class BillingTaskService {
{
state: ApplicationBillingState.Pending,
lockedAt: {
$lt: new Date(Date.now() - 1000 * this.lockTimeout),
$lt: new Date(Date.now() - 1000 * 60),
},
},
{ $set: { lockedAt: new Date() } },
Expand Down

0 comments on commit ce225d3

Please sign in to comment.