diff --git a/README/README.md b/README/README.md index 1eb10a7..21ba14f 100644 --- a/README/README.md +++ b/README/README.md @@ -17,11 +17,15 @@ Read [this](http://lifepluslinux.blogspot.in/2015/06/crontab-ui-easy-and-safe-wa npm install -g crontab-ui crontab-ui - + If you need to set/use an alternate port, you may do so by setting an environment variable before starting the process: PORT=9000 crontab-ui +If you need to autosave your changes to crontab directly: + + crontab-ui --autosave + ###Adding, deleting, pausing and resuming jobs. Once setup Crontab UI provides you with a web interface using which you can manage all the jobs without much hassle. diff --git a/app.js b/app.js index 3087b38..18d904a 100755 --- a/app.js +++ b/app.js @@ -1,3 +1,4 @@ +/*jshint esversion: 6*/ var express = require('express'); var app = express(); var crontab = require("./crontab"); @@ -9,7 +10,6 @@ var mime = require('mime'); var fs = require('fs'); var busboy = require('connect-busboy'); // for file upload - // include the routes var routes = require("./routes").routes; @@ -17,7 +17,7 @@ var routes = require("./routes").routes; app.set('view engine', 'ejs'); var bodyParser = require('body-parser'); -app.use( bodyParser.json() ); // to support JSON-encoded bodies +app.use(bodyParser.json()); // to support JSON-encoded bodies app.use(bodyParser.urlencoded({ // to support URL-encoded bodies extended: true })); @@ -29,12 +29,14 @@ app.use(express.static(__dirname + '/public/css')); app.use(express.static(__dirname + '/public/js')); app.set('views', __dirname + '/views'); -//set port +// set port to 8000 or the value set by environment var PORT app.set('port', (process.env.PORT || 8000)); +// root page handler app.get(routes.root, function(req, res) { - // get all the crontabs + // reload the database before rendering crontab.reload_db(); + // send all the required parameters crontab.crontabs( function(docs){ res.render('index', { routes : JSON.stringify(routes), @@ -46,6 +48,11 @@ app.get(routes.root, function(req, res) { }); }); +/* +Handle to save crontab to database +If it is a new job @param _id is set to -1 +@param name, command, schedule, logging has to be sent with _id (if exists) +*/ app.post(routes.save, function(req, res) { // new job if(req.body._id == -1){ @@ -58,20 +65,25 @@ app.post(routes.save, function(req, res) { res.end(); }); +// set stop to job app.post(routes.stop, function(req, res) { crontab.status(req.body._id, true); res.end(); }); +// set start to job app.post(routes.start, function(req, res) { crontab.status(req.body._id, false); res.end(); }); +// remove a job app.post(routes.remove, function(req, res) { crontab.remove(req.body._id); res.end(); }); + +// set crontab. Needs env_vars to be passed app.get(routes.crontab, function(req, res, next) { crontab.set_crontab(req.query.env_vars, function(err) { if (err) next(err); @@ -79,11 +91,13 @@ app.get(routes.crontab, function(req, res, next) { }); }); +// backup crontab db app.get(routes.backup, function(req, res) { crontab.backup(); res.end(); }); +// This renders the restore page similar to backup page app.get(routes.restore, function(req, res) { // get all the crontabs restore.crontabs(req.query.db, function(docs){ @@ -96,16 +110,19 @@ app.get(routes.restore, function(req, res) { }); }); +// delete backup db app.get(routes.delete_backup, function(req, res) { restore.delete(req.query.db); res.end(); }); +// restore from backup db app.get(routes.restore_backup, function(req, res) { crontab.restore(req.query.db); res.end(); }); +// export current crontab db so that user can download it app.get(routes.export, function(req, res) { var file = __dirname + '/crontabs/crontab.db'; @@ -119,7 +136,7 @@ app.get(routes.export, function(req, res) { filestream.pipe(res); }); - +// import from exported crontab db app.post(routes.import, function(req, res) { var fstream; req.pipe(req.busboy); @@ -133,13 +150,14 @@ app.post(routes.import, function(req, res) { }); }); +// import from current ACTUALL crontab app.get(routes.import_crontab, function(req, res) { crontab.import_crontab(); res.end(); }); +// get the log file a given job. id passed as query param app.get(routes.logger, function(req, res) { - var fs = require("fs"); _file = crontab.log_folder +"/"+req.query.id+".log"; if (fs.existsSync(_file)) res.sendFile(_file); @@ -155,7 +173,7 @@ app.use(function(err, req, res, next) { data.message = err.message || 'Internal Server Error'; if (process.env.NODE_ENV === 'development' && err.stack) { - data.stack = err.stack + data.stack = err.stack; } if (parseInt(data.statusCode) >= 500) { @@ -166,5 +184,15 @@ app.use(function(err, req, res, next) { }); app.listen(app.get('port'), function() { + // If --autosave is used then we will also save whatever is in the db automatically without having to mention it explictly + // we do this by watching log file and setting a on change hook to it + if (process.argv.includes("--autosave")){ + crontab.autosave_crontab(()=>{}); + fs.watchFile(__dirname + '/crontabs/crontab.db', () => { + crontab.autosave_crontab(()=>{ + console.log("Attempted to autosave crontab"); + }); + }); + } console.log("Crontab UI is running at http://localhost:" + app.get('port')); }); diff --git a/crontab.js b/crontab.js index b6f743f..3c41f89 100644 --- a/crontab.js +++ b/crontab.js @@ -1,3 +1,4 @@ +/*jshint esversion: 6*/ //load database var Datastore = require('nedb'); var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' }); @@ -68,7 +69,6 @@ exports.set_crontab = function(env_vars, callback){ log_file = exports.log_folder + "/" + tab._id + ".log"; if(tab.command[tab.command.length-1] != ";") // add semicolon tab.command +=";"; - //{ command; } 2>/tmp/.log|| {if test -f /tmp/; then date >> ; cat /tmp/.log >> ; rm /tmp.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 { @@ -129,7 +129,7 @@ exports.restore = function(db_name){ db.loadDatabase(); // reload the database }; -exports.reload_db= function(){ +exports.reload_db = function(){ db.loadDatabase(); }; @@ -170,3 +170,8 @@ exports.import_crontab = function(){ }); }); }; + +exports.autosave_crontab = function(callback) { + let env_vars = exports.get_env(); + exports.set_crontab(env_vars, callback); +}; diff --git a/package.json b/package.json index 3c0cc74..47a168c 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crontab-ui", - "version": "0.2.0", + "version": "0.2.1", "description": "Easy and safe way to manage your crontab file", "main": "index.js", "scripts": {