GithubHelp home page GithubHelp logo

farhan711 / task-scheduling Goto Github PK

View Code? Open in Web Editor NEW
24.0 1.0 1.0 13 KB

NodeJs server using express, Job scheduling (Recurring, Periodic, Time-specific.)

JavaScript 100.00%
agendajs agenda nodejs job-scheduler recurring-events mongodb

task-scheduling's Introduction

task-scheduling

This has nodejs application used to schedule task(s) [Here I have used to send email at given time]. Using Agenda as scheduler for sending email at given time. This could be set recurring, one time, or periodic execution of tasks. (Note: If you're using Agenda then, must follow file strcuture and Agenda decalration for turning it on, or it will create an issue of not resuming jobs when server restarts.)

How to run this server?

  • Clone repository and go to the project path
  • Install dependencies npm install
  • start the server npm start

Also add .env file in project main(root) folder, having below values of your own.

NODE_ENV=development
PORT=3001
DATABASE=mongodb://localhost:27017/database #your_mongo_database_here
[email protected] #email_address_from_where_email_would_be_sending
EMAIL_PASSWORD=****** #Password_of_the_above_email

task-scheduling's People

Contributors

farhan711 avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

debugger22m

task-scheduling's Issues

How to get dynamically created jobs

Hi Farhan,

Thanks for referring me to your repo, I find it easier to understand. However, I have a quick question about getting jobs list.

I'm creating jobs dynamically and saving them in the database. How do I retrieve those jobs and process them here.

Referring to your comments.

// if you are adding the job types dynamically and saving them in the database you will get it here
let jobTypes = ["emailSchedule"];

Thanks!

Ajenda jobs getting scheduled twice.

I followed the Agenda JS setup from this repository but made a few changes of my own. Everything seemed to work fine until I discovered a bug. If I restart the server two times in a row then the same job would get scheduled twice instead of once. I don't want a recurring job, I just want to schedule a one time job by passing a specific date.

Defining Jobs

const agenda = new Agenda({
  db: {
    address: uri,
    collection: 'jobs',
    options: {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    },
  },
});

// Define membership job
agenda.define('endMembership', (job) => {
 // A job
});

// Define notifications job
agenda.define('sendNotification', async (job) => {
  // Another job
});

agenda.on('ready', async () => await agenda.start());

const graceful = () => {
  agenda.stop(() => process.exit(0));
};

process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);

module.exports = agenda;

Scheduling Jobs

controllers.js

const {
      membership_date,
      customer_id,
      customer_onesignal_id,
      name,
    } = req.body;
    try {
      (async () => {
        // Schedule membership end event
        const end_date = addMinutes(parseISO(membership_date), 5);
        await agenda.schedule(end_date, 'endMembership', {//Some data});

        const seventh_day = addMinutes(parseISO(membership_date), 5);
        await agenda.schedule(seventh_day, 'sendNotification', { //Some data});

        const fourteenth_day = addMinutes(parseISO(membership_date), 4);
        await agenda.schedule(fourteenth_day, 'sendNotification', {//Some data});
      })();

      res.status(200);
    } catch (err) {
      res.status(400);
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.