Tech Nerd
Tech Nerd

Reputation: 93

How can I generate dynamic cron in nodeJs from inputs date given

I have a usecase where unsure how to generate cron using which lib in nodejs.

Edit - Just a note here, I am using Agenda lib And using this lib, I am trying to schedule custom event trigger.

Edit-2 - I am using date picker with react to pick up the dates, and logic will handle on the backend for scheduling with Agenda lib.

I am using node express with react where I have requirement to schedule a recurring events.

enter image description here

Here's picture where I have save the data with cron, How can I achieve this? any suggestions or node lib?

Upvotes: 3

Views: 4836

Answers (1)

Arch
Arch

Reputation: 527

If cron-like functionality from within Node is what you want, you can try node-cron

const cron = require("node-cron");
const express = require("express");
const fs = require("fs");

app = express();

frequency="Yearly"
pattern=""

switch(frequency) {
  case "Daily":
    pattern = "0 0 * * *"
    break;
  case "Monthly":
    pattern = "0 0 1 * *"
    break;
  default:
    pattern = "* * * * *"
    frequency = "every minute"
}

// schedule tasks to be run on the server
cron.schedule(pattern, function () {
  console.log("running a task " + frequency);
});

app.listen(3128);

Example borrowed from here Node.js Cron Jobs By Examples

Added some extra switch case logic to demonstrate a possible workflow for your task at hand.

Upvotes: 3

Related Questions