Implement infinite long running service in NodeJS - node.js

I have a list of URLs in DB. I want to periodically check if those URLs are alive or not.
So I create a long running service which run infinite loop, each iteration:
Query database to get list of urls
For each URL, make request to check if it is alive or not
Please guide me how to implement that service.
I looked at Bull and Kue, but they seem not support infinite loop service?

You can use something very simple like setInterval() to have your task repeat x amount of times.
var testUrls = function(){
//do your magic of connecting to the DB and checking urls.
}
setInterval(testUrls, 60000);
The above code snippet will call your function testUrls every minute.
Or if you need more control over the scheduling you can use a npm package like cron.

You can use node-schedule
Its very simple
var schedule = require('node-schedule');
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});

Related

How to complete a process in Node JS after executing all the operations

I am very new to NodeJS and trying to develop an application which acts as a scheduler that tries to fetch data from ELK and sends the processed data to another ELK. I am able to achieve the expected behaviour but after completing all the processes, scheduler job does not exists and wait for another scheduler job to come up.
Note: This scheduler runs every 3 minutes.
job.js
const self = module.exports = {
async schedule() {
if (process.env.SCHEDULER == "MinuteFrequency") {
var timenow = moment().seconds(0).milliseconds(0).valueOf();
var endtime = timenow - 60000;
var starttime = endtime - 60000 * 3;
//sendData is an async method
reports.sendData(starttime, endtime, "SCHEDULER");
}
}
}
I tried various solutions such Promise.allSettled(....., Promise.resolve(true), etc, but not able to fix this.
As per my requirement, I want the scheduler to complete and process and exit so that I can save some resources as I am planning to deploy the application using Kubernetes cronjobs.
When all your work is done, you can call process.exit() to cause your application to exit.
In this particular code, you may need to know when reports.sendData() is actually done before exiting. We would have to know what that code is and/or see the code to know how to know when it is done. Just because it's an async function doesn't mean it's written properly to return a promise that resolves when it's done. If you want further help, show us the code for sendData() and any code that it calls too.

how to run a line of code after a delay in a paralel thread node js?

I need to schedule one of the tasks in a web site while being able to use the rest of the website features
I'm reading about async and sync execution but not sure if there is a simpler way to do it.
This task is sending something to the Windows cmd to run a program, so I thought about two options:
Adding a command in the cmd sentence to delay the execution of the line
Defining this line of code as a thread which includes a "wait" action with the specificed delay
I'm trying this code:
var schedule = require('node-schedule');
schedule.scheduleJob('6 * * * *', function(){
//console.log('The answer to life, the universe, and everything!');
command += "--test project://" + projectName + "/" + tc.name + " ";
});
This runs the command in minute 6.
I'm a bit newbie in the language, so I'm not sure if this is the best way to do it.
What do you guys think? Any node js function I could use?

node-schedule undoing cancellation

I've been working with node for the first time in a while again and stumbled upon node-schedule, which for the most part has been a breeze, however, I've found resuming a scheduled task after canceling it via job.cancel() pretty difficult.
For the record, I'm using schedule to perform specific actions at a specific date (non-recurring) and under some circumstances cancel the task at a specific date but would later like to resume it.
I tried using job.cancel(true) after cancelling it via plain job.cancel() first as the documentation states that that would reschedule the task, but this has not worked for me. Using job.reschedule() after having cancelled job first yields the same result.
I could probably come up with an unelegant solution, but I thought I'd ask if anyone knows of an elegant one first.
It took me a while to understand node-schedule documentation ^^
To un-cancel a job, You have to give to reschedule some options.
If you don't pass anything to reschedule, this function returns false (Error occured)
For exemple, you can declare options, and pass this variable like this :
const schedule = require('node-schedule');
let options = {rule: '*/1 * * * * *'}; // Declare schedule rules
let job = schedule.scheduleJob(options, () => {
console.log('Job processing !');
});
job.cancel(); // Cancel Job
job.reschedule(options); // Reschedule Job
Hope it helps.

can we use node js timer object in angular 4?

In my Angular application I need to make a http call in regular intervals of time (say every y mins) as long as the page is active and render the response on the screen.
Is it good to use NodeJS.Timer object to implement setTimeout()
or use Observable.timer().
Observable.timer() is a lot more useful for Angular
for example:
const tick3$ = Observable.timer(100, 60000);
If you just want to execute a regular function every 10 minutes, you can just use plain Javascript for that:
var timer = setInterval(function() {
// put your function code here
}, 10 * 60 * 60 * 1000);
Or, if the function is already separately defined:
var timer = setInterval(myFunction, 10 * 60 * 60 * 1000);
Is it good to use node js timer object to implement setTimeout()
It's really not clear what you mean by this question. A nodejs timer object would be running things on the nodejs server, not on the client. Since you said you want to run something in the browser web page, I presume you don't want a timer on nodejs to do that, but rather a timer in the web page as my example above shows.
Angular also has a wrapper around setInterval() called $interval() and it adds a few more features including a count and some angular specific functionality related to updating the view if the data changes. If you want those angular specific features, you can use $interval(). If not, then you can use either.

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

Resources