@@ -5,8 +5,8 @@ db.loadDatabase(function (err) {
55} ) ;
66var exec = require ( 'child_process' ) . exec ;
77var fs = require ( 'fs' ) ;
8- var cron_parser = require ( "cron-parser" )
9- var os = require ( "os" )
8+ var cron_parser = require ( "cron-parser" ) ;
9+ var os = require ( "os" ) ;
1010
1111exports . log_folder = __dirname + '/crontabs/logs' ;
1212exports . env_file = __dirname + '/crontabs/env.db' ;
@@ -22,36 +22,36 @@ crontab = function(name, command, schedule, stopped, logging){
2222 data . timestamp = ( new Date ( ) ) . toString ( ) ;
2323 data . logging = logging ;
2424 return data ;
25- }
25+ } ;
2626
2727exports . create_new = function ( name , command , schedule , logging ) {
2828 var tab = crontab ( name , command , schedule , false , logging ) ;
2929 tab . created = new Date ( ) . valueOf ( ) ;
3030 db . insert ( tab ) ;
31- }
31+ } ;
3232
3333exports . update = function ( data ) {
3434 db . update ( { _id : data . _id } , crontab ( data . name , data . command , data . schedule , null , data . logging ) ) ;
35- }
35+ } ;
3636
3737exports . status = function ( _id , stopped ) {
3838 db . update ( { _id : _id } , { $set : { stopped : stopped } } ) ;
39- }
39+ } ;
4040
4141exports . remove = function ( _id ) {
4242 db . remove ( { _id : _id } , { } ) ;
43- }
43+ } ;
4444exports . crontabs = function ( callback ) {
4545 db . find ( { } ) . sort ( { created : - 1 } ) . exec ( function ( err , docs ) {
4646 for ( var i = 0 ; i < docs . length ; i ++ ) {
4747 if ( docs [ i ] . schedule == "@reboot" )
48- docs [ i ] . next = "Next Reboot"
48+ docs [ i ] . next = "Next Reboot" ;
4949 else
5050 docs [ i ] . next = cron_parser . parseExpression ( docs [ i ] . schedule ) . next ( ) . toString ( ) ;
5151 }
5252 callback ( docs ) ;
5353 } ) ;
54- }
54+ } ;
5555exports . set_crontab = function ( env_vars ) {
5656 exports . crontabs ( function ( tabs ) {
5757 var crontab_string = "" ;
@@ -64,7 +64,7 @@ exports.set_crontab = function(env_vars){
6464 tmp_log = "/tmp/" + tab . _id + ".log" ;
6565 log_file = exports . log_folder + "/" + tab . _id + ".log" ;
6666 if ( tab . command [ tab . command . length - 1 ] != ";" ) // add semicolon
67- tab . command += ";"
67+ tab . command += ";" ;
6868 //{ command; } 2>/tmp/<id>.log|| {if test -f /tmp/<id>; then date >> <log file>; cat /tmp/<id>.log >> <log file>; rm /tmp<id>.log }
6969 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" ;
7070 }
@@ -79,10 +79,10 @@ exports.set_crontab = function(env_vars){
7979 } ) ;
8080
8181 } ) ;
82- }
82+ } ;
8383
8484exports . get_backup_names = function ( ) {
85- var backups = [ ]
85+ var backups = [ ] ;
8686 fs . readdirSync ( __dirname + '/crontabs' ) . forEach ( function ( file ) {
8787 // file name begins with backup
8888 if ( file . indexOf ( "backup" ) == 0 ) {
@@ -92,10 +92,10 @@ exports.get_backup_names = function(){
9292
9393 // Sort by date. Newest on top
9494 for ( var i = 0 ; i < backups . length ; i ++ ) {
95- var Ti = backups [ i ] . split ( "backup" ) [ 1 ]
95+ var Ti = backups [ i ] . split ( "backup" ) [ 1 ] ;
9696 Ti = new Date ( Ti . substring ( 0 , Ti . length - 3 ) ) . valueOf ( ) ;
9797 for ( var j = 0 ; j < i ; j ++ ) {
98- var Tj = backups [ j ] . split ( "backup" ) [ 1 ]
98+ var Tj = backups [ j ] . split ( "backup" ) [ 1 ] ;
9999 Tj = new Date ( Tj . substring ( 0 , Tj . length - 3 ) ) . valueOf ( ) ;
100100 if ( Ti > Tj ) {
101101 var temp = backups [ i ] ;
@@ -106,62 +106,56 @@ exports.get_backup_names = function(){
106106 }
107107
108108 return backups ;
109- }
109+ } ;
110110
111111exports . backup = function ( ) {
112112 //TODO check if it failed
113113 fs . createReadStream ( __dirname + '/crontabs/crontab.db' ) . pipe ( fs . createWriteStream ( __dirname + '/crontabs/backup ' + ( new Date ( ) ) . toString ( ) . replace ( "+" , " " ) + '.db' ) ) ;
114- }
114+ } ;
115115
116116exports . restore = function ( db_name ) {
117117 fs . createReadStream ( __dirname + '/crontabs/' + db_name ) . pipe ( fs . createWriteStream ( __dirname + '/crontabs/crontab.db' ) ) ;
118118 db . loadDatabase ( ) ; // reload the database
119- }
119+ } ;
120120
121121exports . reload_db = function ( ) {
122122 db . loadDatabase ( ) ;
123- }
123+ } ;
124124
125125exports . get_env = function ( ) {
126126 if ( fs . existsSync ( exports . env_file ) ) {
127127 return fs . readFileSync ( exports . env_file , 'utf8' ) . replace ( "\n" , "\n" ) ;
128128 }
129- return ""
130- }
129+ return "" ;
130+ } ;
131131
132- // TODO
133132exports . import_crontab = function ( ) {
134133 exec ( "crontab -l" , function ( error , stdout , stderr ) {
135134 var lines = stdout . split ( "\n" ) ;
136-
137135 var namePrefix = new Date ( ) . getTime ( ) ;
138136
139137 lines . forEach ( function ( line , index ) {
140138 var regex = / ^ ( ( \@ [ a - z A - Z ] + \s ) | ( ( [ ^ \s ] + ) \s ( [ ^ \s ] + ) \s ( [ ^ \s ] + ) \s ( [ ^ \s ] + ) \s ( [ ^ \s ] + ) \s ) ) / ;
141139 var command = line . replace ( regex , '' ) . trim ( ) ;
142140 var schedule = line . replace ( command , '' ) . trim ( ) ;
143-
141+
144142 if ( command && schedule ) {
145143 var name = namePrefix + '_' + index ;
146144
147- db . crontabs . findOne ( { command : command , schedule : schedule } , function ( err , doc ) {
145+ db . findOne ( { command : command , schedule : schedule } , function ( err , doc ) {
148146 if ( err ) {
149147 throw err ;
150148 }
151149 if ( ! doc ) {
152- exports . create_new ( name , command , null , null , schedule , null ) ;
150+ exports . create_new ( name , command , schedule , null ) ;
153151 }
154152 else {
155153 doc . command = command ;
156154 doc . schedule = schedule ;
157155 exports . update ( doc ) ;
158156 }
159157 } ) ;
160-
161-
162158 }
163-
164- } )
165- //console.log(stdout);
159+ } ) ;
166160 } ) ;
167- }
161+ } ;
0 commit comments