@@ -113,7 +113,7 @@ export class BackupManager {
113
113
}
114
114
}
115
115
116
- @Cron ( CronExpression . EVERY_10_SECONDS , { name : 'check-backup-states' } ) // Run every 10 seconds
116
+ @Cron ( CronExpression . EVERY_10_SECONDS , { name : 'check-backup-states' } )
117
117
async checkBackupStates ( ) : Promise < void > {
118
118
// lock the sync to only run one instance at a time
119
119
const lockKey = 'check-backup-states'
@@ -122,19 +122,22 @@ export class BackupManager {
122
122
return
123
123
}
124
124
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
-
136
125
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 (
138
141
sandboxes . map ( async ( s ) => {
139
142
const lockKey = `sandbox-backup-${ s . id } `
140
143
const hasLock = await this . redisLockProvider . lock ( lockKey , 60 )
@@ -143,11 +146,6 @@ export class BackupManager {
143
146
}
144
147
145
148
try {
146
- const runner = await this . runnerService . findOne ( s . runnerId )
147
- if ( runner . state !== RunnerState . READY ) {
148
- return
149
- }
150
-
151
149
// get the latest sandbox state
152
150
const sandbox = await this . sandboxRepository . findOneByOrFail ( {
153
151
id : s . id ,
@@ -191,25 +189,25 @@ export class BackupManager {
191
189
}
192
190
}
193
191
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' } )
195
193
async syncStopStateCreateBackups ( ) : Promise < void > {
196
194
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 )
198
196
if ( ! hasLock ) {
199
197
return
200
198
}
201
199
202
200
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 (
213
211
sandboxes
214
212
. filter ( ( sandbox ) => sandbox . runnerId !== null )
215
213
. map ( async ( sandbox ) => {
@@ -220,11 +218,6 @@ export class BackupManager {
220
218
}
221
219
222
220
try {
223
- const runner = await this . runnerService . findOne ( sandbox . runnerId )
224
- if ( runner . state !== RunnerState . READY ) {
225
- return
226
- }
227
-
228
221
await this . setBackupPending ( sandbox . id )
229
222
} catch ( error ) {
230
223
this . logger . error ( `Error processing backup for sandbox ${ sandbox . id } :` , error )
0 commit comments