Skip to content

Commit 72fc96d

Browse files
authored
fix(api): query ready runners only on backup checks (#2336)
Signed-off-by: Toma Puljak <[email protected]>
1 parent 649df71 commit 72fc96d

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

apps/api/src/sandbox/managers/backup.manager.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class BackupManager {
113113
}
114114
}
115115

116-
@Cron(CronExpression.EVERY_10_SECONDS, { name: 'check-backup-states' }) // Run every 10 seconds
116+
@Cron(CronExpression.EVERY_10_SECONDS, { name: 'check-backup-states' })
117117
async checkBackupStates(): Promise<void> {
118118
// lock the sync to only run one instance at a time
119119
const lockKey = 'check-backup-states'
@@ -122,19 +122,22 @@ export class BackupManager {
122122
return
123123
}
124124

125-
const sandboxes = await this.sandboxRepository.find({
126-
where: {
127-
state: In([SandboxState.ARCHIVING, SandboxState.STARTED, SandboxState.STOPPED]),
128-
backupState: In([BackupState.PENDING, BackupState.IN_PROGRESS]),
129-
},
130-
order: {
131-
lastBackupAt: 'ASC',
132-
},
133-
take: 100,
134-
})
135-
136125
try {
137-
await Promise.all(
126+
const sandboxes = await this.sandboxRepository
127+
.createQueryBuilder('sandbox')
128+
.innerJoin('runner', 'r', 'r.id = sandbox.runnerId')
129+
.where('sandbox.state IN (:...states)', {
130+
states: [SandboxState.ARCHIVING, SandboxState.STARTED, SandboxState.STOPPED],
131+
})
132+
.andWhere('sandbox.backupState IN (:...backupStates)', {
133+
backupStates: [BackupState.PENDING, BackupState.IN_PROGRESS],
134+
})
135+
.andWhere('r.state = :ready', { ready: RunnerState.READY })
136+
.orderBy('sandbox.lastBackupAt', 'ASC')
137+
.take(100)
138+
.getMany()
139+
140+
await Promise.allSettled(
138141
sandboxes.map(async (s) => {
139142
const lockKey = `sandbox-backup-${s.id}`
140143
const hasLock = await this.redisLockProvider.lock(lockKey, 60)
@@ -143,11 +146,6 @@ export class BackupManager {
143146
}
144147

145148
try {
146-
const runner = await this.runnerService.findOne(s.runnerId)
147-
if (runner.state !== RunnerState.READY) {
148-
return
149-
}
150-
151149
// get the latest sandbox state
152150
const sandbox = await this.sandboxRepository.findOneByOrFail({
153151
id: s.id,
@@ -191,25 +189,25 @@ export class BackupManager {
191189
}
192190
}
193191

194-
@Cron(CronExpression.EVERY_30_SECONDS, { name: 'sync-stop-state-create-backups' }) // Run every 30 seconds
192+
@Cron(CronExpression.EVERY_10_SECONDS, { name: 'sync-stop-state-create-backups' })
195193
async syncStopStateCreateBackups(): Promise<void> {
196194
const lockKey = 'sync-stop-state-create-backups'
197-
const hasLock = await this.redisLockProvider.lock(lockKey, 30)
195+
const hasLock = await this.redisLockProvider.lock(lockKey, 10)
198196
if (!hasLock) {
199197
return
200198
}
201199

202200
try {
203-
const sandboxes = await this.sandboxRepository.find({
204-
where: {
205-
state: In([SandboxState.STOPPED, SandboxState.ARCHIVING]),
206-
backupState: In([BackupState.NONE]),
207-
},
208-
// todo: increase this number when auto-stop is stable
209-
take: 100,
210-
})
211-
212-
await Promise.all(
201+
const sandboxes = await this.sandboxRepository
202+
.createQueryBuilder('sandbox')
203+
.innerJoin('runner', 'r', 'r.id = sandbox.runnerId')
204+
.where('sandbox.state IN (:...states)', { states: [SandboxState.ARCHIVING, SandboxState.STOPPED] })
205+
.andWhere('sandbox.backupState = :none', { none: BackupState.NONE })
206+
.andWhere('r.state = :ready', { ready: RunnerState.READY })
207+
.take(100)
208+
.getMany()
209+
210+
await Promise.allSettled(
213211
sandboxes
214212
.filter((sandbox) => sandbox.runnerId !== null)
215213
.map(async (sandbox) => {
@@ -220,11 +218,6 @@ export class BackupManager {
220218
}
221219

222220
try {
223-
const runner = await this.runnerService.findOne(sandbox.runnerId)
224-
if (runner.state !== RunnerState.READY) {
225-
return
226-
}
227-
228221
await this.setBackupPending(sandbox.id)
229222
} catch (error) {
230223
this.logger.error(`Error processing backup for sandbox ${sandbox.id}:`, error)

0 commit comments

Comments
 (0)