()
| 19 | |
| 20 | class JobModule { |
| 21 | constructor() { |
| 22 | this.jobsDir = configModule.getJobsPath(); |
| 23 | this.jobsFilePath = path.join(this.jobsDir, 'jobs.json'); |
| 24 | this.jobsFilePathOld = path.join(this.jobsDir, 'jobs.json.old'); |
| 25 | this.isSaving = false; // Locking mechanism to prevent multiple saves at the same time |
| 26 | this.jobs = {}; // Initialize this.jobs as an empty object |
| 27 | |
| 28 | if (!fs.existsSync(this.jobsDir)) { |
| 29 | fs.mkdirSync(this.jobsDir, { recursive: true }); |
| 30 | } |
| 31 | |
| 32 | // If there is a jobs.json file, load it and migrate to the DB |
| 33 | if (fs.existsSync(this.jobsFilePath)) { |
| 34 | const fileContent = fs.readFileSync(this.jobsFilePath); |
| 35 | this.jobs = JSON.parse(fileContent); |
| 36 | |
| 37 | this.migrateJobsFromFile().then(() => { |
| 38 | // Save the jobs.json file to jobs.json.old just in case we need it later |
| 39 | fs.renameSync(this.jobsFilePath, this.jobsFilePathOld); |
| 40 | |
| 41 | // Reload from the DB |
| 42 | this.loadJobsFromDB().then(() => { |
| 43 | this.terminateInProgressJobs(); |
| 44 | this.saveJobsAndStartNext(); |
| 45 | }); |
| 46 | }); |
| 47 | } else { |
| 48 | // If there is no jobs.json file, load the jobs from the DB |
| 49 | this.loadJobsFromDB().then(() => { |
| 50 | this.terminateInProgressJobs(); |
| 51 | this.saveJobsAndStartNext(); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | // Schedule a daily backfill from complete.list and run an initial backfill |
| 56 | this.scheduleDailyBackfill(); |
| 57 | |
| 58 | const disableInitialBackfill = process.env.JOBMODULE_DISABLE_INITIAL_BACKFILL === 'true'; |
| 59 | if (!disableInitialBackfill) { |
| 60 | setTimeout(() => { |
| 61 | this.backfillFromCompleteList().catch((err) => { |
| 62 | logger.error({ err }, 'Initial backfill failed'); |
| 63 | }); |
| 64 | }, 0); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Recover completed videos from JobVideoDownload tracking table |
nothing calls this directly
no test coverage detected