@ -7,7 +7,9 @@ var exec = require('child_process').exec;
var fs = require ( 'fs' ) ;
var fs = require ( 'fs' ) ;
var cron _parser = require ( "cron-parser" )
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 = { } ;
var data = { } ;
data . name = name ;
data . name = name ;
data . command = command ;
data . command = command ;
@ -16,17 +18,18 @@ crontab = function(name, command, schedule, stopped){
data . stopped = stopped ;
data . stopped = stopped ;
}
}
data . timestamp = ( new Date ( ) ) . toString ( ) ;
data . timestamp = ( new Date ( ) ) . toString ( ) ;
data . logging = logging ;
return data ;
return data ;
}
}
exports . create _new = function ( name , command , schedule ) {
exports . create _new = function ( name , command , schedule , logging ) {
var tab = crontab ( name , command , schedule , false ) ;
var tab = crontab ( name , command , schedule , false , logging ) ;
tab . created = new Date ( ) . valueOf ( ) ;
tab . created = new Date ( ) . valueOf ( ) ;
db . insert ( tab ) ;
db . insert ( tab ) ;
}
}
exports . update = function ( data ) {
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 ) {
exports . status = function ( _id , stopped ) {
@ -52,6 +55,15 @@ exports.set_crontab = function(){
var crontab _string = "" ;
var crontab _string = "" ;
tabs . forEach ( function ( tab ) {
tabs . forEach ( function ( tab ) {
if ( ! tab . stopped ) {
if ( ! tab . stopped ) {
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" ;
crontab _string += tab . schedule + " " + tab . command + "\n" ;
}
}
} ) ;
} ) ;