Monday, 24 April 2017

fs.unlink method is not executing on linux server?

I am executing compareDates method on CronJob that remove all files that are older than 30 days it works good on local windows but i dont see this method executing on linux. Any idea how can i correct this issue ?

ctrl.js

var directories = ['/../../logs/dit', '/../../logs/st','/../../logs/uat'];
var currentDate = new Date();
module.exports = function CronJob() {
    var rule = new cronSchedule.RecurrenceRule();
    rule.hour = 8;
    rule.minute = 0;
    var dailyJob = cronSchedule.scheduleJob(rule, function() {
    console.log('Testing 8AM');
            async.eachSeries(directories, function (dir, cb1) {
                var dir = __dirname + 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 };
                            compareDates(fileInfo, filePath);
                            cb2(null, fileInfo);
                        });
                    }, cb1);

                });
            }, function (err, fileInfos) {
                if (err) {
                    console.info('error', err);
                    return;
                }
            });
    });
}

    function compareDates(file,path) {
            console.log('Comapre Method',file);
            var timeDiff = Math.abs(currentDate.getTime() - file.fileDate.getTime());
            var dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
            if (dayDifference >= 30) {
                  fs.unlink(path, function(err) {
                            if (err) {
                                // file doens't exist
                                console.info("File doesn't exist, won't remove it.");
                            } else {
                                console.log('removed file', file);
                            }
                        });

            }

    }

console.log data

Comapre Method { fileDate: Wed Apr 19 2017 00:36:26 GMT-0400 (EDT),
  filename: 'server.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:34 GMT-0400 (EDT),
  filename: 'server1.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:33 GMT-0400 (EDT),
  filename: 'server10.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:25 GMT-0400 (EDT),
  filename: 'server11.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:27 GMT-0400 (EDT),
  filename: 'server2.log' }
Comapre Method { fileDate: Wed Apr 19 2017 00:36:34 GMT-0400 (EDT),
  filename: 'server20170317143423.log' }



via hussain

No comments:

Post a Comment