Reputation: 7083
I want to execute task based on time so i am using node-schedule
to run every 1 minute for now but i dont see anything in console, Any idea what is implemented wrong ?
cron.js
var cron = require('node-schedule');
cron.scheduleJob('*/1 * * * * *', function() {
console.log('This runs at the 30th mintue of every hour. FROM NODE SCHEDULE');
async.eachSeries(directories, function(dir, cb1) {
var dir = __dirname + dir;
console.log('reading', dir);
// get files for the directory
fs.readdir(dir, function(err, files) {
if (err) return cb1(err);
// loop through each file
async.eachSeries(files, function(file, cb2) {
var filePath = path.resolve(dir + '/' + file);
// get info for the file
fs.stat(filePath, function(err, stats) {
if (err) return cb2(err);
var fileInfo = {
fileDate: stats.birthtime,
filename: file
};
console.log('fileInfo', fileInfo);
compareDates(fileInfo, filePath);
cb2(null, fileInfo);
});
}, cb1);
});
});
});
Upvotes: 0
Views: 2175
Reputation: 4170
There are a few answers here, but it seems that your question was still not fully answered. I just created a simple project, ran the code, and it worked for me. :)
Perhaps you have skipped over one of these steps:
1) Copy the code into a local javascript file. I named mine "index.js", and in it I put this simplified version of your original code snippet:
var cron = require('node-schedule');
cron.scheduleJob('*/1 * * * * *', function() {
console.log('This runs every second FROM NODE SCHEDULE');
});
2) Create a package.json file for managing your dependency libraries.
One easy way to create this file is to run npm init
and follow the interactive questions in your command prompt.
3) Install the node-schedule and save it to package.json
You can do this by running this command:
npm install node-schedule --save
4) Run your javascript file.
I ran this command:
node index.js
Also, here is a video of me running the file and having it log to the console:
Note that for illustration and testing purposes I have the cron scheduled to run every second. To have it run every minute the cron expression would be this:
'0 * * * * *'
EDIT
If you want to call this file from within another javascript file, just wrap this node-schedule code in a function that is visible to your other javascript files. Here is an example in Es5 syntax:
index.js
var cron = require('node-schedule');
module.exports = function beginCron() {
cron.scheduleJob('*/1 * * * * *', function () {
console.log('This runs every second FROM NODE SCHEDULE');
});
};
app.js
var beginCron = require('./index.js');
beginCron();
We can then run app.js and see it beginning the cron execution like this: node app.js
.
Upvotes: 0
Reputation: 1794
I have modified your code to run every 1 minute from now.
var cron = require('node-schedule');
cron.scheduleJob({ rule: '*/59 * * * * *' }, function () {
console.log('This runs at the 30th mintue of every hour. FROM NODE SCHEDULE');
/*
async.eachSeries(directories, function (dir, cb1) {
var dir = __dirname + dir;
console.log('reading', dir);
// get files for the directory
fs.readdir(dir, function (err, files) {
if (err) return cb1(err);
// loop through each file
async.eachSeries(files, function (file, cb2) {
var filePath = path.resolve(dir + '/' + file);
// get info for the file
fs.stat(filePath, function (err, stats) {
if (err) return cb2(err);
var fileInfo = {
fileDate: stats.birthtime,
filename: file
};
console.log('fileInfo', fileInfo);
compareDates(fileInfo, filePath);
cb2(null, fileInfo);
});
}, cb1);
});
});
*/
});
Upvotes: 0
Reputation: 56
First thoughts:
Here https://github.com/node-schedule/node-schedule is written about cron pattern: there are 6 asterisks. The first one (optional) corresponds to seconds. You need to omit one last asterisks in order to run it every minute.
Have you tried to run it first like this? Did it work?
var cron = require('node-schedule');
cron.scheduleJob('*/1 * * * *', function() {
console.log('This runs at the 30th mintue of every hour. FROM NODE SCHEDULE');
});
Upvotes: 1
Reputation: 111258
You may have better luck running other of the modules that are providing the same functionality.
See one of those modules:
and more modules here:
Upvotes: 0