Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Scheduled job implementation #645

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading