autosave mode

v0.2.1 v0.2.1
alseambusher 2016-12-03 21:09:12 +05:30
parent f2987e7ee4
commit 65acd94271
4 changed files with 48 additions and 11 deletions

View File

@ -17,11 +17,15 @@ Read [this](http://lifepluslinux.blogspot.in/2015/06/crontab-ui-easy-and-safe-wa
npm install -g crontab-ui npm install -g crontab-ui
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: 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 PORT=9000 crontab-ui
If you need to autosave your changes to crontab directly:
crontab-ui --autosave
###Adding, deleting, pausing and resuming jobs. ###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. Once setup Crontab UI provides you with a web interface using which you can manage all the jobs without much hassle.

42
app.js
View File

@ -1,3 +1,4 @@
/*jshint esversion: 6*/
var express = require('express'); var express = require('express');
var app = express(); var app = express();
var crontab = require("./crontab"); var crontab = require("./crontab");
@ -9,7 +10,6 @@ var mime = require('mime');
var fs = require('fs'); var fs = require('fs');
var busboy = require('connect-busboy'); // for file upload var busboy = require('connect-busboy'); // for file upload
// include the routes // include the routes
var routes = require("./routes").routes; var routes = require("./routes").routes;
@ -17,7 +17,7 @@ var routes = require("./routes").routes;
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
var bodyParser = require('body-parser'); 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 app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true extended: true
})); }));
@ -29,12 +29,14 @@ app.use(express.static(__dirname + '/public/css'));
app.use(express.static(__dirname + '/public/js')); app.use(express.static(__dirname + '/public/js'));
app.set('views', __dirname + '/views'); 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)); app.set('port', (process.env.PORT || 8000));
// root page handler
app.get(routes.root, function(req, res) { app.get(routes.root, function(req, res) {
// get all the crontabs // reload the database before rendering
crontab.reload_db(); crontab.reload_db();
// send all the required parameters
crontab.crontabs( function(docs){ crontab.crontabs( function(docs){
res.render('index', { res.render('index', {
routes : JSON.stringify(routes), 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) { app.post(routes.save, function(req, res) {
// new job // new job
if(req.body._id == -1){ if(req.body._id == -1){
@ -58,20 +65,25 @@ app.post(routes.save, function(req, res) {
res.end(); res.end();
}); });
// set stop to job
app.post(routes.stop, function(req, res) { app.post(routes.stop, function(req, res) {
crontab.status(req.body._id, true); crontab.status(req.body._id, true);
res.end(); res.end();
}); });
// set start to job
app.post(routes.start, function(req, res) { app.post(routes.start, function(req, res) {
crontab.status(req.body._id, false); crontab.status(req.body._id, false);
res.end(); res.end();
}); });
// remove a job
app.post(routes.remove, function(req, res) { app.post(routes.remove, function(req, res) {
crontab.remove(req.body._id); crontab.remove(req.body._id);
res.end(); res.end();
}); });
// set crontab. Needs env_vars to be passed
app.get(routes.crontab, function(req, res, next) { app.get(routes.crontab, function(req, res, next) {
crontab.set_crontab(req.query.env_vars, function(err) { crontab.set_crontab(req.query.env_vars, function(err) {
if (err) next(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) { app.get(routes.backup, function(req, res) {
crontab.backup(); crontab.backup();
res.end(); res.end();
}); });
// This renders the restore page similar to backup page
app.get(routes.restore, function(req, res) { app.get(routes.restore, function(req, res) {
// get all the crontabs // get all the crontabs
restore.crontabs(req.query.db, function(docs){ 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) { app.get(routes.delete_backup, function(req, res) {
restore.delete(req.query.db); restore.delete(req.query.db);
res.end(); res.end();
}); });
// restore from backup db
app.get(routes.restore_backup, function(req, res) { app.get(routes.restore_backup, function(req, res) {
crontab.restore(req.query.db); crontab.restore(req.query.db);
res.end(); res.end();
}); });
// export current crontab db so that user can download it
app.get(routes.export, function(req, res) { app.get(routes.export, function(req, res) {
var file = __dirname + '/crontabs/crontab.db'; var file = __dirname + '/crontabs/crontab.db';
@ -119,7 +136,7 @@ app.get(routes.export, function(req, res) {
filestream.pipe(res); filestream.pipe(res);
}); });
// import from exported crontab db
app.post(routes.import, function(req, res) { app.post(routes.import, function(req, res) {
var fstream; var fstream;
req.pipe(req.busboy); 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) { app.get(routes.import_crontab, function(req, res) {
crontab.import_crontab(); crontab.import_crontab();
res.end(); res.end();
}); });
// get the log file a given job. id passed as query param
app.get(routes.logger, function(req, res) { app.get(routes.logger, function(req, res) {
var fs = require("fs");
_file = crontab.log_folder +"/"+req.query.id+".log"; _file = crontab.log_folder +"/"+req.query.id+".log";
if (fs.existsSync(_file)) if (fs.existsSync(_file))
res.sendFile(_file); res.sendFile(_file);
@ -155,7 +173,7 @@ app.use(function(err, req, res, next) {
data.message = err.message || 'Internal Server Error'; data.message = err.message || 'Internal Server Error';
if (process.env.NODE_ENV === 'development' && err.stack) { if (process.env.NODE_ENV === 'development' && err.stack) {
data.stack = err.stack data.stack = err.stack;
} }
if (parseInt(data.statusCode) >= 500) { if (parseInt(data.statusCode) >= 500) {
@ -166,5 +184,15 @@ app.use(function(err, req, res, next) {
}); });
app.listen(app.get('port'), function() { 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')); console.log("Crontab UI is running at http://localhost:" + app.get('port'));
}); });

View File

@ -1,3 +1,4 @@
/*jshint esversion: 6*/
//load database //load database
var Datastore = require('nedb'); var Datastore = require('nedb');
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' }); 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"; log_file = exports.log_folder + "/" + tab._id + ".log";
if(tab.command[tab.command.length-1] != ";") // add semicolon if(tab.command[tab.command.length-1] != ";") // add semicolon
tab.command +=";"; 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"; 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 { else {
@ -129,7 +129,7 @@ exports.restore = function(db_name){
db.loadDatabase(); // reload the database db.loadDatabase(); // reload the database
}; };
exports.reload_db= function(){ exports.reload_db = function(){
db.loadDatabase(); 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);
};

View File

@ -1,6 +1,6 @@
{ {
"name": "crontab-ui", "name": "crontab-ui",
"version": "0.2.0", "version": "0.2.1",
"description": "Easy and safe way to manage your crontab file", "description": "Easy and safe way to manage your crontab file",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {