I try to develop a little browser game based on NodeJS and Angular 4.
I have an API server running on NodeJS which is connected to a MongoDB and a second server running Angular 4.
I want to execute recurring standard functions (like every 15 minutes) in the background.
Do I need a third server which runs that functions? Or can I run that functions independently on my API server - no matter which route is open?
You might want to have a look to this library node-cron. You can set it up to work with your services. You will need to initialise the job right after your sever is initialised. An example:
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: '00 30 11 * * 1-5',
onTick: function() {
/*
* Runs every weekday (Monday through Friday)
* at 11:30:00 AM. It does not run on Saturday
* or Sunday.
*/
},
start: false,
timeZone: 'America/Los_Angeles'
});
job.start();
You can use setTimeout() and setInterval() in Node just like in the browser:
setInterval(() => {
// this runs every 15 minutes
}, 15 * 60 * 1000);
Related
i have a event web app on react.js | when logged-in user set an event on event page - assume four days later from today and there is another dropdown input filed of setReminder with values 4 hours ago / 3 hours ago and so on and on submit i'm calling or hiting an
route/api/endpoint/postRequest post->api->userSchema->mongoDB->req.body -
json
{ setReminderTime: currentDateTime - req.body.data.setReminderValue } etc. etc.
and saving other more data and so now i want to my code to run a function in there i write some code i want that code to exicute on that event date/time - {minus} that reminder date/time (4 hours or 3 or 2 hours ago ) so in reminder i send a notification or a smg or want to do other things more and i don't want to hit my databse each second and i also don't want to do use setTimeout staf beacuse my server refresh again again due to puch->updates
I'm not sure if i had understand your problem well, but you can try to use a reminder on the server (backend side) and you have to run the reminder every hour by using cron service like that :
const schedulerFactory = function() {
return {
start: function() {
// this action will be executed every day at 12:00 u can choose what you wan't
new CronJob('00 00 12 * * *', function() {
//CronJob('0 */1 * * * *') // this for execute each one minute
console.log('Running Send Notifications Worker for ' + moment().format());
notificationsWorker.run(); // this should be the function to call and it will search for all instance where some condition are true and than execute them
}, null, true, '');
},
};
};
on the database you can stock some informations to know if you have to execute this action or no (for example last connection or something like that to know when to send notification)
Good luck Bro.
I have written on cron job to update users table using Nodejs. I have used cron node package. I need to run cron 4 times a day. I have written 2 crons. The first cron will run 4 times a day with particular time duration and will start the second cron. The second cron will run in every 5 minutes till all users updated. after updating of users the second cron will stop.
sample code :
//This cron will run at every 5 minutes
var job = new CronJob({
cronTime: '*/5 * * * *',
onTick: function() {
//cron runs in every 5 min;
/* API logic */
// when all user updated this cron will stopped
job.stop();
},start: false
});
//runs on 7est,8est,9est,10est,11est
var mainjob = new CronJob('0 0 7-11 * * *', function() {
job.start();
}, function () {},true
);
First time It's running well but the second time not started. Can you please advice on this?
I'm trying to run a Node task/function at a specific time each day (7am Eastern Time) regardless of Daylight Savings. I've tried cron-based packages, but cron doesn't seem to account for it. The server the app is running on is on GMT/UTC, so that that needs to be taken into consideration as well. Here is my current code:
const schedule = require('node-schedule');
...
const j = schedule.scheduleJob('0 12 * * *', function(){
bot.channels.get(getDefaultChannel().id).send("Hello! Your daily fact for today is", { embed: generateEmbed(getRandomFact()) });
});
This works fine, but since we just moved ahead an hour, the message appears at 8am instead of 7am.
This is a tricky one, I've played about with a couple of ways of doing this, I think the Cron library works the best (https://www.npmjs.com/package/cron). You can schedule a job to run at 7 am in the US/Eastern timezone.
"use strict";
var cron = require('cron');
var job1 = new cron.CronJob({
cronTime: '0 7 * * *',
onTick: function() {
bot.channels.get(getDefaultChannel().id).send("Hello! Your daily fact for today is", { embed: generateEmbed(getRandomFact()) });
},
start: true,
timeZone: 'US/Eastern'
});
I am using node-schedule to schedule my tasks. Now I need to schedule a job everyday at 12am.
Below given is the code I am using,
var rule3 = schedule.scheduleJob('00 00 00 * * *', function(){
console.log('my test job!');
});
This is not working for me.
Any help appreciated. Thanks in advance.
You can use node-cron module simply.
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 0-6', function() {
/*
* Runs every day
* at 12:00:00 AM.
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);
Read docs for more pattern.
For anyone who is stuck on this also check what time your app is operating in. Your app by default might be in Greenwich Mean Time which would definitely make you think your schedule is not working. Toss a console.log(Date.now()) in a convenient location and check. You might just have to adjust the time on your schedule by a couple hours.
I'm running CronJob Every 30 mins.
But it is not running at the time of start.
How can i make it run at the time of start as well as every 30 min?
var CronJob = require('cron').CronJob;
console.log('started ' + new Date());
var job = new CronJob('0 */30 * * * *', function() {
myJob()
}, function () {
},
true,
'Indian/Mauritius'
);
job.start();
function myJob()
{
console.log('in Job');
console.log(new Date());
}
Output
started Mon Sep 21 2015 18:44:29 GMT+0530 (IST)
You could run myJob() and then start your cronjob.
or ...
you could change the way you use cron so that it runs at particular times, say now and every half hour like this:
var job = new CronJob('* 16,46 * * * *', // etc
Say the time is now 15:45, the above command will run in a minute (i.e. on the 16th and 46th minute of every hour). With a bit more code, you could generate the string "16,46" to be one minute from now and 30 minutes after that.