Skip to content

Commit

Permalink
Merge pull request #645 from evershopcommerce/dev
Browse files Browse the repository at this point in the history
feat: Scheduled job implementation
  • Loading branch information
treoden authored Oct 1, 2024
2 parents 0c99f5c + 0e06705 commit 6445046
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/evershop/bin/lib/cronjob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const cron = require('node-cron');
const isResolvable = require('is-resolvable');
const { getConfig } = require('@evershop/evershop/src/lib/util/getConfig');
const { error } = require('@evershop/evershop/src/lib/log/logger');

module.exports = exports;

function start() {
// Get the list of jobs from the configuration
const jobs = getConfig('system.jobs', []);

const goodJobs = [];
jobs.forEach((job) => {
if (!isResolvable(job.resolve)) {
error(
`Job ${job.name} is not resolvable. Please check again the 'resolve' property.`
);
} else if (!cron.validate(job.schedule)) {
error(
`Job ${job.name} has an invalid schedule. Please check again the 'schedule' property.`
);
} else if (job.enabled === true) {
goodJobs.push(job);
} else {
error(`Job ${job.name} is disabled.`);
}
});
// Schedule the jobs
goodJobs.forEach((job) => {
cron.schedule(job.schedule, async () => {
try {
// Load the module
const jobFunction = require(job.resolve);
// Make sure the module is a function or async function
if (typeof jobFunction !== 'function') {
throw new Error(
`Job ${job.name} is not a function. Make sure the module exports a function as default.`
);
}
// Execute the job
await jobFunction();
} catch (e) {
error(e);
}
});
});
}

start();
17 changes: 17 additions & 0 deletions packages/evershop/bin/lib/startUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ module.exports.start = async function start(cb) {
});

child.unref();

// Spawn the child process to manage scheduled jobs
const jobArgs = [path.resolve(__dirname, 'cronjob.js')];
if (isDevelopmentMode() || process.argv.includes('--debug')) {
jobArgs.push('--debug');
}
const jobChild = spawn('node', jobArgs, {
stdio: 'inherit',
env: {
...process.env,
ALLOW_CONFIG_MUTATIONS: true
}
});
jobChild.on('error', (err) => {
error(`Error spawning job processor: ${err}`);
});
jobChild.unref();
};

module.exports.updateApp = function updateApp(cb) {
Expand Down
1 change: 1 addition & 0 deletions packages/evershop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"luxon": "^2.0.2",
"mini-css-extract-plugin": "^2.6.1",
"multer": "^1.4.2",
"node-cron": "^3.0.3",
"npm": "^8.18.0",
"ora": "^5.4.1",
"pg": "^8.11.3",
Expand Down
21 changes: 21 additions & 0 deletions packages/evershop/src/modules/base/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ module.exports = async () => {
required: ['name', 'enabled', 'resolve']
}
},
jobs: {
type: 'array',
items: {
type: 'object',
properties: {
name: {
type: 'string'
},
resolve: {
type: 'string'
},
enabled: {
type: 'boolean'
},
schedule: {
type: 'string'
}
},
required: ['name', 'enabled', 'resolve', 'schedule']
}
},
theme: {
type: 'string'
},
Expand Down

0 comments on commit 6445046

Please sign in to comment.