How to run a function after a given time in node.JS? - node.js

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();

Related

Avoiding more than one cronjob in parallel in node.js

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;
}

Node.js: Get time till next cron job

I have a server that has a cron job running at certain times in the day. I want to get the time till the next job will execute. I looked at the cron and node-cron packages in npm, but they don't seem to have this functionality.
How can I implement this functionality by myself for these packages?
The cron package allows you to get the next runs of the cron job, so you can use this to determine the time until the next run.
This is exposed in the CronJob.nextDates() function.
const CronJob = require("cron").CronJob;
const cronJob = new CronJob(
"*/30 * * * * *",
() => {
console.log("Timestamp: ", new Date());
},
null,
true,
"UTC"
);
setInterval(() => {
let timeUntilNextRunSeconds = cronJob.nextDates(1)[0].unix() - new Date().getTime()/1000;
console.log("Time until next run (s): ", Math.round(timeUntilNextRunSeconds));
}, 1000);

can cron schedule a variable change in one of your scripts (node.js)

is it possible to get cron to go into one of your scripts on the server and change a variable at a certain time?
cron simply fires a program/script at a given time. You would need an intermediate script/program to change the variable. But you could schedule a script to run that changes your variable.
You can install cron npm module to do it:
$ yarn add cron
And then in your code:
const CronJob = require('cron').CronJob;
let i = 0;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
i++;
console.log(`Value of i = ${i}`);
}, null, true, 'America/Los_Angeles');

How to use Generic cron jobs in node application?

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();

How can I trigger a function when server starts using keystonejs?

I am using Keystonejs to make a small app. I want to print out a message every second since the server starts, so I use node-cron to handle this task.
function(){
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
}
However, I don't know where I should put this code to make sure that when server starts, this function will be triggered.
Can you help me with this problem? Thank you in advance.
you can alter your lib/core/start.js file to also execute that callback

Resources