Skip to content

Commit 65acd94

Browse files
committed
autosave mode
1 parent f2987e7 commit 65acd94

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

README/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ Read [this](http://lifepluslinux.blogspot.in/2015/06/crontab-ui-easy-and-safe-wa
1717

1818
npm install -g crontab-ui
1919
crontab-ui
20-
20+
2121
If you need to set/use an alternate port, you may do so by setting an environment variable before starting the process:
2222

2323
PORT=9000 crontab-ui
2424

25+
If you need to autosave your changes to crontab directly:
26+
27+
crontab-ui --autosave
28+
2529
###Adding, deleting, pausing and resuming jobs.
2630

2731
Once setup Crontab UI provides you with a web interface using which you can manage all the jobs without much hassle.

app.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*jshint esversion: 6*/
12
var express = require('express');
23
var app = express();
34
var crontab = require("./crontab");
@@ -9,15 +10,14 @@ var mime = require('mime');
910
var fs = require('fs');
1011
var busboy = require('connect-busboy'); // for file upload
1112

12-
1313
// include the routes
1414
var routes = require("./routes").routes;
1515

1616
// set the view engine to ejs
1717
app.set('view engine', 'ejs');
1818

1919
var bodyParser = require('body-parser');
20-
app.use( bodyParser.json() ); // to support JSON-encoded bodies
20+
app.use(bodyParser.json()); // to support JSON-encoded bodies
2121
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
2222
extended: true
2323
}));
@@ -29,12 +29,14 @@ app.use(express.static(__dirname + '/public/css'));
2929
app.use(express.static(__dirname + '/public/js'));
3030
app.set('views', __dirname + '/views');
3131

32-
//set port
32+
// set port to 8000 or the value set by environment var PORT
3333
app.set('port', (process.env.PORT || 8000));
3434

35+
// root page handler
3536
app.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+
*/
4956
app.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
6169
app.post(routes.stop, function(req, res) {
6270
crontab.status(req.body._id, true);
6371
res.end();
6472
});
6573

74+
// set start to job
6675
app.post(routes.start, function(req, res) {
6776
crontab.status(req.body._id, false);
6877
res.end();
6978
});
7079

80+
// remove a job
7181
app.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
7587
app.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
8295
app.get(routes.backup, function(req, res) {
8396
crontab.backup();
8497
res.end();
8598
});
8699

100+
// This renders the restore page similar to backup page
87101
app.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
99114
app.get(routes.delete_backup, function(req, res) {
100115
restore.delete(req.query.db);
101116
res.end();
102117
});
103118

119+
// restore from backup db
104120
app.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
109126
app.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
123140
app.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
136154
app.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
141160
app.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

168186
app.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
});

crontab.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*jshint esversion: 6*/
12
//load database
23
var Datastore = require('nedb');
34
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' });
@@ -68,7 +69,6 @@ exports.set_crontab = function(env_vars, callback){
6869
log_file = exports.log_folder + "/" + tab._id + ".log";
6970
if(tab.command[tab.command.length-1] != ";") // add semicolon
7071
tab.command +=";";
71-
//{ command; } 2>/tmp/<id>.log|| {if test -f /tmp/<id>; then date >> <log file>; cat /tmp/<id>.log >> <log file>; rm /tmp<id>.log }
7272
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";
7373
}
7474
else {
@@ -129,7 +129,7 @@ exports.restore = function(db_name){
129129
db.loadDatabase(); // reload the database
130130
};
131131

132-
exports.reload_db= function(){
132+
exports.reload_db = function(){
133133
db.loadDatabase();
134134
};
135135

@@ -170,3 +170,8 @@ exports.import_crontab = function(){
170170
});
171171
});
172172
};
173+
174+
exports.autosave_crontab = function(callback) {
175+
let env_vars = exports.get_env();
176+
exports.set_crontab(env_vars, callback);
177+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crontab-ui",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Easy and safe way to manage your crontab file",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)