Skip to content

Commit

Permalink
Add cleanup job for stalled scans
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Jun 21, 2024
1 parent e908d64 commit 4a23292
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/models/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,11 @@ export async function finishScanWithError(storageId, error) {

return storage
}

// List all storages that are processing and have not sent a heartbeat in the last 10 minutes
export async function listStalledScans() {
return mongo.db.collection('storages').find({
'scan.status': 'processing',
'scan.heartbeat': {$lt: new Date(Date.now() - (10 * 60 * 1000))}
}).toArray()
}
17 changes: 15 additions & 2 deletions worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node
/* eslint-disable unicorn/prefer-top-level-await */
import 'dotenv/config.js'

import mongo from './lib/util/mongo.js'
import {startNextScan} from './lib/models/storage.js'
import {startNextScan, listStalledScans, finishScanWithError} from './lib/models/storage.js'
import {scan} from './lib/scan.js'

await mongo.connect()
Expand All @@ -17,4 +18,16 @@ async function scanLoop() {
setTimeout(scanLoop, 2000)
}

await scanLoop()
scanLoop()

async function cleanStalledScansLoop() {
const stalledScanStorages = await listStalledScans()

await Promise.all(stalledScanStorages.map(async storage => {
await finishScanWithError(storage._id, {message: 'Scan stalled'})
}))

setTimeout(cleanStalledScansLoop, 60_000)
}

cleanStalledScansLoop()

0 comments on commit 4a23292

Please sign in to comment.