NodeJs Job scheduler based on selection from calendar - node.js

I am building nodejs app. It is first time I will use job scheduler in my code.
I want a user to select dates and times from calendar (such as bootstrap date time picker) to run a specific script at server at those dates and times. Users chosen schedule will be saved on server and execute the script on server according to schedule.
I searched the net and found some libraries for this such as agenda, later, node-cron, node-schedule etc. And then there a libraries Bull, Bee and Kue.
I am rather lost where to begin. I would very much appreciate if some one experienced in job scheduling in nodejs can guide me concurring to my usage. It would save me lot of time.

You can try date based job scheduling of "node-schedule".
Refer:
https://www.npmjs.com/package/node-schedule#date-based-scheduling
var Schedular = require('node-schedule');
var date = new Date(2018, 10, 27, 12, 25, 0);
let nodeScheduler = await Schedular.scheduleJob(date, async function () {
console.log("Job started.");
console.log('** do something **');
console.log(`Job completed`);
});
Where in new Date: 2018- year, 10 - month(0- Jan, 11- Dec), 27 - date, 12 -Hr, 25 - min

Related

Vanilla NodeJS Cron Job?

I want to do something in node at, say, midnight each day.
I see a lot of stuff pointing me to node-cron, and I see this article configuring a docker container to execute a script per a crontab
I want to 1. not use any external packages and 2. keep the script being executed inside the server code itself (i.e. I couldn't have the docker container execute some other file on a schedule)
The use case is I want to update a cache on the server every day around midnight, and then, at more frequent intervals, use that cache for various things.
You can use setInterval to run the code every hour and check if it's around midnight
setInterval(() => {
if (new Date().getHours() === 0) {
// do stuff
}
}, 1000 * 60 * 60 * 60)

How to run a node script automatically at specified intervals

I'm making a dating app. Every day at midnight I want 'new matches' to be randomly found and displayed to a user.
If anyone could give a give high level overview of how this would work I'd be really grateful?
If want to execute a Node task every midnight then Cron jobs is a powerful, yet simple tool that can help us get there. You can user Cron Package to achieve this in node.
You can add cron dependency using
npm install cron
You can modify following script
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 01 * * *', function() {
console.log('You see this message every day 01 AM America/Los_Angeles');
}, null, true, 'America/Los_Angeles');
Where you can replace console.log with the logic you want to implement the code that will find match for each users of app and replace timezone with yours.

Looking for a way to retrieve timezones in which the current time is x

I am looking for a way to obtain a list of timezones in which the current time is a variable (it has to be DST dependant as well).
For example I need to push messages to users for which the local time is 18:45. I can fetch from our database a list of users by timezone. So I plan a scheduled event that would wake up at every 15 minutes, it will need to check in which timezones it is currently 18:45 in order to be able to retrieve the right users.
Our environment is node js.
The way I am thinking about is preferably a web service or some utility that does it all for me including the updates from iana timezones database. But I can handle less convenient ways if there's no such alternative.
Thanks in advance.
Here is one simple way to do it with moment-timezone (I believe it handles things like daylight savings time etc., not 100% sure -- but certainly will do it better than one could do starting from scratch alone.. see their github repo for issues -- its a complex topic):
var moment = require('moment-timezone');
var tm = moment();
var toFind = tm.format('h:mm a');
var userZones = ['America/Los_Angeles', 'America/New_York'];
for (var i=0; i<userZones.length; i++) {
var fmt = tm.tz(userZones[i]).format('h:mm a');
if (fmt === toFind) console.log('matched zone: ' + userZones[i]);
}

Setup Heroku Scheduler job to email all users (Meteor/MongoDB)

Does anyone know if it's possible to make a Heroku Scheduler job that would send an email to all of my users once per day? I'm using Meteor and MongoDB.
I can see that the Heroku Scheduler can run a command such as "node somefile.js" but I can't seem to figure out how to make a connection to the mongodb in a file like this. Can I somehow tap into the DB without involving Meteor in this?
Any help would be appreciated!
I eventually found a package to do so: synced-cron. Basically, you need to setup a method in which use the package to fire a recurring job.
The package website also has a sample code:
SyncedCron.add({
name: 'Crunch some important numbers for the marketing department',
schedule: function(parser) {
// parser is a later.parse object
return parser.text('every 2 hours');
},
job: function() {
var numbersCrunched = CrushSomeNumbers();
return numbersCrunched;
}
});
Here you just need to replace the code in the job function to send out the email.
The job supports schedules like "every 5 minutes", "at 5:00pm", etc. The package relies on the text parser in Later.js to parse the schedule. You can refer to the Later.js doc.
Two different options.
The first is to use Heroku's scheduler,
In which you create a text file in your bin directory:
#! /app/.heroku/node/bin/node
var test = require('./jobToDo') //put your job in this file (jobToDo.js)
Now you don't have to put the job in another .js file, but it makes it easier to work with, rather than coding in a plain text file. (put again that is up to you)
The first line #! /app/.heroku/node/bin/node may be different for you depending on how your configuration is set up, depending on your OS and node/npm set up.
The second option is a cron style library. This will allow you to decide when you want your code to run.
This is pretty easy, and for me the preferred method.
var CronJob = require('cron').CronJob;
var fn = function(){
// Do Something
}
var job = new CronJob({
cronTime: "00 00 02 * * 1-5",
onTick: fn,
start: true,
timeZone: 'America/Los_Angeles'
});
You can look at documentation on github

Schedule publishing of a post in a rails 3 blog

I'm developing a website (with Rails 3.1) where limited set of 'writers' are able to write post. 'Moderators' should accept (or decline) the post and schedule the publishing. Until this moment is the development process pretty basic.
There are two publish moments each day. Accepted posts will be placed in some kind of queue. Each day at 10:00am and 4:00pm the oldest accepted post must be published. However, I need also to be able to ** manually set** a date and time when the post going live.
What's the best way to achieve the result? Cron? Background Jobs?
So...
1) have an accepted_at field, which you can also set manually; it's the 'time to go live'.
2)
class Post
scope :ready_to_be_published, lambda{ where(['accepted_at<? and not published', Time.zone.now]).order('accepted_at ASC') }
def accept!(time_to_go_live = nil)
update_attributes!(:accepted_at => time_to_go_live || Time.zone.now)
end
end
3) have a whenever job at 10am and 4pm to run a rake task
task :publish_a_post => :environment do
Post.ready_to_be_published.first.update_attributes!(:published => true)
end

Resources