File size: 683 Bytes
03b405b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const { listJobs, updateJob, event } = require("./jobs");

function startWatchdog(logger) {
  const interval = setInterval(() => {
    const stuckCutoff = Date.now() - 90 * 60 * 1000;
    for (const job of listJobs(200)) {
      if (!["downloading", "compressing", "uploading"].includes(job.status)) continue;
      if (Date.parse(job.updatedAt) < stuckCutoff) {
        event(job.id, "warn", "Watchdog moved a stale job back to retrying");
        updateJob(job.id, { status: "retrying", stage: "watchdog-retry" });
        logger.warn({ jobId: job.id }, "Watchdog recovered stale job");
      }
    }
  }, 5 * 60 * 1000);
  interval.unref();
}

module.exports = { startWatchdog };