Skip to content

Commit

Permalink
Prevent job scheduler collision
Browse files Browse the repository at this point in the history
  • Loading branch information
kanwarujjaval committed Jan 16, 2025
1 parent b6a5421 commit 46c77aa
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions api/parts/jobs/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@

const log = require('../../utils/log.js')('jobs:scanner'),
manager = require('../../../plugins/pluginManager.js'),
fs = require('fs');
fs = require('fs'),
{Job, IPCJob, IPCFaçadeJob, TransientJob} = require('./job.js');

/**
* Validates if a job class has the required methods
* @param {Function} JobClass - The job class to validate
* @returns {boolean} - True if valid, throws error if invalid
*/
const validateJobClass = (JobClass) => {
// Check if it's a class/constructor
if (typeof JobClass !== 'function') {
throw new Error('Job must be a class constructor');
}

// Check if it inherits from one of the valid base classes
if (!(JobClass.prototype instanceof Job ||
JobClass.prototype instanceof IPCJob ||
JobClass.prototype instanceof IPCFaçadeJob ||
JobClass.prototype instanceof TransientJob)) {
throw new Error('Job class must extend Job, IPCJob, IPCFaçadeJob, or TransientJob');
}

return true;
};

module.exports = (db, filesObj, classesObj) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -52,9 +75,12 @@ module.exports = (db, filesObj, classesObj) => {
(arr || []).forEach(job => {
try {
let name = job.category + ':' + job.name;
filesObj[name] = job.file;
classesObj[name] = require(job.file);
log.d('Found job %j at %j', name, job.file);
const JobClass = require(job.file);
if (validateJobClass(JobClass)) {
filesObj[name] = job.file;
classesObj[name] = JobClass;
log.d('Found valid job %j at %j', name, job.file);
}
}
catch (e) {
log.e('Error when loading job %s: %j ', job.file, e, e.stack);
Expand Down

0 comments on commit 46c77aa

Please sign in to comment.