Added error log support

This commit is contained in:
alseambusher
2015-12-16 02:04:44 -08:00
parent 01dbc47ef3
commit 860e08a336
8 changed files with 37 additions and 10 deletions

View File

@@ -7,7 +7,9 @@ var exec = require('child_process').exec;
var fs = require('fs');
var cron_parser = require("cron-parser")
crontab = function(name, command, schedule, stopped){
exports.log_folder = __dirname + '/crontabs/logs';
crontab = function(name, command, schedule, stopped, logging){
var data = {};
data.name = name;
data.command = command;
@@ -16,17 +18,18 @@ crontab = function(name, command, schedule, stopped){
data.stopped = stopped;
}
data.timestamp = (new Date()).toString();
data.logging = logging;
return data;
}
exports.create_new = function(name, command, schedule){
var tab = crontab(name, command, schedule, false);
exports.create_new = function(name, command, schedule, logging){
var tab = crontab(name, command, schedule, false, logging);
tab.created = new Date().valueOf();
db.insert(tab);
}
exports.update = function(data){
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null));
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null, data.logging));
}
exports.status = function(_id, stopped){
@@ -52,7 +55,16 @@ exports.set_crontab = function(){
var crontab_string = "";
tabs.forEach(function(tab){
if(!tab.stopped){
crontab_string += tab.schedule + " " + tab.command + "\n";
if (tab.logging && tab.logging == "true"){
tmp_log = "/tmp/" + tab._id + ".log";
log_file = exports.log_folder + "/" + tab._id + ".log";
if(tab.command[tab.command.length-1] != ";") // add semicolon
tab.command +=";"
//{ command; } 2>/tmp/<id>.log|| {if test -f /tmp/<id>; then date >> <log file>; cat /tmp/<id>.log >> <log file>; rm /tmp<id>.log }
crontab_string += tab.schedule + " { " + tab.command + " } 2> " + tmp_log +"; if test -f " + tmp_log +"; then date >> " + log_file + "; cat " + tmp_log + " >> " + log_file + "; rm " + tmp_log + "; fi \n";
}
else
crontab_string += tab.schedule + " " + tab.command + "\n";
}
});
fs.writeFile("/tmp/crontab", crontab_string, function(err) {