I wanted to make a function which will send the list of users a notification (Email) and want this to continue after every 6 hrs regularly.
Something like this
function(){
if(currentTime==scheduledTime){
//Triger notification sender
sendNotification();
scheduledTime = (scheduledTime + 6 hrs);
}
}
How can I implement this Feature. Also, where can I deploy this website with this kind of functionality for free.
You can use node-cron instead.You can setup the interval using cron syntax and it will run in a scheduled manner.
npm install --save node-cron
Then :
var cron = require('node-cron');
cron.schedule('* */6 * * *', () => {
console.log('running a task every 6 hours');
sendNotification();
});
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 am building a hosting server with node.JS and mongoDB.
The server will be used for people to save their files remotely. But I do not want to keep unattended files on the server because it will be a waste of memory on the server.
So there are two things I wish to do :
Delete Files if not attended for more than 10 days
Leave the Files otherwise
Is there a way this can be done easily?
You can use cron job for this type of scheduled task. NodeJS has a module named cron to perform a scheduled task.
Approach:
Every day at midnight you will check for unattended files. If you find any unattended file then you can delete it.
Example code:
const CronJob = require('cron').CronJob;
new CronJob('0 0 * * *', async () => {
// Find files
// Delete files
}, null, true, 'America/Vancouver', null, false);
You can schedule a cron and it will check for your condition and do the desired action.
https://www.npmjs.com/package/cron
example:
var CronJob = require('cron').CronJob;
var job = new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
job.start();
I would like to do some cronjob using the Node.js package "cron" every 10 minutes.
This cronjob takes between 5 to 15 minutes, and I don't want that in a case that one instance is still running - another will be joining it in parallel. Instead, it will skip the additional running and wait until the next period.
Is it possible to implement it using this package?
Here is the code of the implementation using the cron package :
const CronJob = require("cron").CronJob;
const job = new CronJob(
'0 */10 * * * *',
()=>SomeCronJob(),
null,
true,
'America/Los_Angeles',
);
I thought of implementing it using a combination of simple setInterval() and clearInterval() instead of the package, not sure how though.
I'd appreciate any help!
I would use a flag to check if the job is running. Example:
let isJobRunning = false;
function SomeCronJob() {
if (isJobRunning) {
// Skip
return;
}
isJobRunning = true;
// run stuff
// Once is finished
isJobRunning = false;
}
I have node application in which I want to run tasks on daily basis. So I want to use node-cron to schedule tasks but not like this:
var cron = require('node-cron');
cron.schedule('* * * * *', function(){
console.log('running a task every minute');
});
I need some generic solution so that at one place I have just empty body for cron job that takes different functions from a different location. That means in total we have two files, one which has just cron job function handler and other have a list of cron jobs. From where we want to run these jobs we just need require and some basic calling.
Can anyone suggest me this solution?
I got my solution. Using node-cron-job module I have achieved what I wanted. Let me explain in detail:-
First i created new file which contains jobs, lets name it jobs.js :-
// jobs.js
exports.first_job = {
on:"* * * * * " //runs every minute
},
job: function () {
console.log("first_job");
},
spawn: true
}
exports.second_job = {
on: "*/2 * * * * *", //runs every 2 second
job: function () {
console.log("second_job");
},
spawn: false // If false, the job will not run in a separate process.
}
Here, we define two jobs name first_job and second_job. We can define as many required.
Finally, we need to call these jobs from any location by these simple steps:-
// main.js
var cronjob = require('node-cron-job');
cronjob.setJobsPath(__dirname + '/jobs.js'); // Absolute path to the jobs module.
cronjob.startJob('first_job');
cronjob.startJob('second_job');
We can call all jobs in a single call like this:-
cronjob.startAllJobs();
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);