I'm created a cron job with Cron module. Currently, I declared that cron job in crawler.js file and its cron rules in settings.js file just like below:
File settings.js:
const cronCrawlItemsRule = '0 */10 * * * *'
module.exports = { cronCrawlItemsRule }
File crawler.js:
const connectDB = require('./config/db');
connectDB();
const { cronCrawlItemsRule } = require './settings.js';
const crawlItems = new CronJob(cronCrawlItemsRule, async function() {
// do stuff here and save to DB that connected above.
})
Problem: I want to save cron rules to DB, then whenever I start the server, get those cron rules and the crons will run follow that rules. Remind that those rules will be got once the server is started.
Is it possible to do this? I've tried like below:
const connectDB = require('./config/db');
connectDB();
const { cronCrawlItemsRule } = require './settings.js';
(async() => {
const rule = (await Config.findOne({...}))?._doc.value || cronCrawlItemsRule; // <- here
const crawlItems = new CronJob(rule, async function() { // <- and here
// do stuff here and save to DB that connected above.
})
})()
but I have to get those rules from DB again whenever I need them in other files.
Does anyone know how to do this? Thanks in advance.