1+ /*jshint esversion: 6*/
12var express = require ( 'express' ) ;
23var app = express ( ) ;
34var crontab = require ( "./crontab" ) ;
@@ -9,15 +10,14 @@ var mime = require('mime');
910var fs = require ( 'fs' ) ;
1011var busboy = require ( 'connect-busboy' ) ; // for file upload
1112
12-
1313// include the routes
1414var routes = require ( "./routes" ) . routes ;
1515
1616// set the view engine to ejs
1717app . set ( 'view engine' , 'ejs' ) ;
1818
1919var bodyParser = require ( 'body-parser' ) ;
20- app . use ( bodyParser . json ( ) ) ; // to support JSON-encoded bodies
20+ app . use ( bodyParser . json ( ) ) ; // to support JSON-encoded bodies
2121app . use ( bodyParser . urlencoded ( { // to support URL-encoded bodies
2222 extended : true
2323} ) ) ;
@@ -29,12 +29,14 @@ app.use(express.static(__dirname + '/public/css'));
2929app . use ( express . static ( __dirname + '/public/js' ) ) ;
3030app . set ( 'views' , __dirname + '/views' ) ;
3131
32- //set port
32+ // set port to 8000 or the value set by environment var PORT
3333app . set ( 'port' , ( process . env . PORT || 8000 ) ) ;
3434
35+ // root page handler
3536app . get ( routes . root , function ( req , res ) {
36- // get all the crontabs
37+ // reload the database before rendering
3738 crontab . reload_db ( ) ;
39+ // send all the required parameters
3840 crontab . crontabs ( function ( docs ) {
3941 res . render ( 'index' , {
4042 routes : JSON . stringify ( routes ) ,
@@ -46,6 +48,11 @@ app.get(routes.root, function(req, res) {
4648 } ) ;
4749} ) ;
4850
51+ /*
52+ Handle to save crontab to database
53+ If it is a new job @param _id is set to -1
54+ @param name, command, schedule, logging has to be sent with _id (if exists)
55+ */
4956app . post ( routes . save , function ( req , res ) {
5057 // new job
5158 if ( req . body . _id == - 1 ) {
@@ -58,32 +65,39 @@ app.post(routes.save, function(req, res) {
5865 res . end ( ) ;
5966} ) ;
6067
68+ // set stop to job
6169app . post ( routes . stop , function ( req , res ) {
6270 crontab . status ( req . body . _id , true ) ;
6371 res . end ( ) ;
6472} ) ;
6573
74+ // set start to job
6675app . post ( routes . start , function ( req , res ) {
6776 crontab . status ( req . body . _id , false ) ;
6877 res . end ( ) ;
6978} ) ;
7079
80+ // remove a job
7181app . post ( routes . remove , function ( req , res ) {
7282 crontab . remove ( req . body . _id ) ;
7383 res . end ( ) ;
7484} ) ;
85+
86+ // set crontab. Needs env_vars to be passed
7587app . get ( routes . crontab , function ( req , res , next ) {
7688 crontab . set_crontab ( req . query . env_vars , function ( err ) {
7789 if ( err ) next ( err ) ;
7890 else res . end ( ) ;
7991 } ) ;
8092} ) ;
8193
94+ // backup crontab db
8295app . get ( routes . backup , function ( req , res ) {
8396 crontab . backup ( ) ;
8497 res . end ( ) ;
8598} ) ;
8699
100+ // This renders the restore page similar to backup page
87101app . get ( routes . restore , function ( req , res ) {
88102 // get all the crontabs
89103 restore . crontabs ( req . query . db , function ( docs ) {
@@ -96,16 +110,19 @@ app.get(routes.restore, function(req, res) {
96110 } ) ;
97111} ) ;
98112
113+ // delete backup db
99114app . get ( routes . delete_backup , function ( req , res ) {
100115 restore . delete ( req . query . db ) ;
101116 res . end ( ) ;
102117} ) ;
103118
119+ // restore from backup db
104120app . get ( routes . restore_backup , function ( req , res ) {
105121 crontab . restore ( req . query . db ) ;
106122 res . end ( ) ;
107123} ) ;
108124
125+ // export current crontab db so that user can download it
109126app . get ( routes . export , function ( req , res ) {
110127 var file = __dirname + '/crontabs/crontab.db' ;
111128
@@ -119,7 +136,7 @@ app.get(routes.export, function(req, res) {
119136 filestream . pipe ( res ) ;
120137} ) ;
121138
122-
139+ // import from exported crontab db
123140app . post ( routes . import , function ( req , res ) {
124141 var fstream ;
125142 req . pipe ( req . busboy ) ;
@@ -133,13 +150,14 @@ app.post(routes.import, function(req, res) {
133150 } ) ;
134151} ) ;
135152
153+ // import from current ACTUALL crontab
136154app . get ( routes . import_crontab , function ( req , res ) {
137155 crontab . import_crontab ( ) ;
138156 res . end ( ) ;
139157} ) ;
140158
159+ // get the log file a given job. id passed as query param
141160app . get ( routes . logger , function ( req , res ) {
142- var fs = require ( "fs" ) ;
143161 _file = crontab . log_folder + "/" + req . query . id + ".log" ;
144162 if ( fs . existsSync ( _file ) )
145163 res . sendFile ( _file ) ;
@@ -155,7 +173,7 @@ app.use(function(err, req, res, next) {
155173 data . message = err . message || 'Internal Server Error' ;
156174
157175 if ( process . env . NODE_ENV === 'development' && err . stack ) {
158- data . stack = err . stack
176+ data . stack = err . stack ;
159177 }
160178
161179 if ( parseInt ( data . statusCode ) >= 500 ) {
@@ -166,5 +184,15 @@ app.use(function(err, req, res, next) {
166184} ) ;
167185
168186app . listen ( app . get ( 'port' ) , function ( ) {
187+ // If --autosave is used then we will also save whatever is in the db automatically without having to mention it explictly
188+ // we do this by watching log file and setting a on change hook to it
189+ if ( process . argv . includes ( "--autosave" ) ) {
190+ crontab . autosave_crontab ( ( ) => { } ) ;
191+ fs . watchFile ( __dirname + '/crontabs/crontab.db' , ( ) => {
192+ crontab . autosave_crontab ( ( ) => {
193+ console . log ( "Attempted to autosave crontab" ) ;
194+ } ) ;
195+ } ) ;
196+ }
169197 console . log ( "Crontab UI is running at http://localhost:" + app . get ( 'port' ) ) ;
170198} ) ;
0 commit comments