Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Dynamic Cron Job in Node with axios

I need to create a service in NodeJS that periodically executes a GET request to an API that return all the Jobs/Tasks. The service then needs to create a CronJob for each task returned while continuing to check for new tasks, and if there are new ones create new CronJobs. I made something similar by having a service that runs a GET and then does a forEach loop and creates new CronJobs. But this doesn't take in account new tasks that are created after the first initialization. How do I solve this? How do I make a service that is always looking for new tasks and dynamically creates them? EDIT1: the axios.post just post a log on a database, nothing special

const axios = require("axios");
const CronJob = require("cron").CronJob;
const cron = require("cron");

const startCron = async () => {
  const schedules = await axios
    .get("http://127.0.0.1:4000/")
    .then((res) => {
      return res.data;
    })
    .catch((err) => console.log(err));

  schedules.forEach((schedule) => {
    return new CronJob(`${schedule.timing} * * * * *`, () => {
      let d = new Date();
      console.log(schedule.message + " in data: " + d);
      axios.post(`http://127.0.0.1:4000/${schedule.id}`);
    }).start();
  });
};

startCron();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!