Merge pull request #21 from joerx/master
Proper, node-js style error handling for set_crontabv0.2.1
commit
8f6fc6439a
34
app.js
34
app.js
|
@ -72,9 +72,11 @@ app.post(routes.remove, function(req, res) {
|
|||
crontab.remove(req.body._id);
|
||||
res.end();
|
||||
});
|
||||
app.get(routes.crontab, function(req, res) {
|
||||
crontab.set_crontab(req.query.env_vars);
|
||||
res.end();
|
||||
app.get(routes.crontab, function(req, res, next) {
|
||||
crontab.set_crontab(req.query.env_vars, function(err) {
|
||||
if (err) next(err);
|
||||
else res.end();
|
||||
});
|
||||
});
|
||||
|
||||
app.get(routes.backup, function(req, res) {
|
||||
|
@ -127,8 +129,8 @@ app.post(routes.import, function(req, res) {
|
|||
fstream.on('close', function () {
|
||||
crontab.reload_db();
|
||||
res.redirect(routes.root);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.get(routes.import_crontab, function(req, res) {
|
||||
|
@ -145,6 +147,24 @@ app.get(routes.logger, function(req, res) {
|
|||
res.end("No errors logged yet");
|
||||
});
|
||||
|
||||
app.listen(app.get('port'), function() {
|
||||
console.log("Crontab UI is running at http://localhost:" + app.get('port'));
|
||||
// error handler
|
||||
app.use(function(err, req, res, next) {
|
||||
var data = {};
|
||||
var statusCode = err.statusCode || 500;
|
||||
|
||||
data.message = err.message || 'Internal Server Error';
|
||||
|
||||
if (process.env.NODE_ENV === 'development' && err.stack) {
|
||||
data.stack = err.stack
|
||||
}
|
||||
|
||||
if (parseInt(data.statusCode) >= 500) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
res.status(statusCode).json(data);
|
||||
});
|
||||
|
||||
app.listen(app.get('port'), function() {
|
||||
console.log("Crontab UI is running at http://localhost:" + app.get('port'));
|
||||
});
|
||||
|
|
29
crontab.js
29
crontab.js
|
@ -1,8 +1,11 @@
|
|||
//load database
|
||||
var Datastore = require('nedb');
|
||||
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' });
|
||||
|
||||
db.loadDatabase(function (err) {
|
||||
if (err) throw err; // no hope, just terminate
|
||||
});
|
||||
|
||||
var exec = require('child_process').exec;
|
||||
var fs = require('fs');
|
||||
var cron_parser = require("cron-parser");
|
||||
|
@ -52,32 +55,40 @@ exports.crontabs = function(callback){
|
|||
callback(docs);
|
||||
});
|
||||
};
|
||||
exports.set_crontab = function(env_vars){
|
||||
exports.set_crontab = function(env_vars, callback){
|
||||
exports.crontabs( function(tabs){
|
||||
var crontab_string = "";
|
||||
if (env_vars) {
|
||||
crontab_string = env_vars + "\n";
|
||||
}
|
||||
tabs.forEach(function(tab){
|
||||
if(!tab.stopped){
|
||||
if (tab.logging && tab.logging == "true"){
|
||||
if(!tab.stopped) {
|
||||
if (tab.logging && tab.logging == "true") {
|
||||
tmp_log = "/tmp/" + tab._id + ".log";
|
||||
log_file = exports.log_folder + "/" + tab._id + ".log";
|
||||
if(tab.command[tab.command.length-1] != ";") // add semicolon
|
||||
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";
|
||||
}
|
||||
else
|
||||
}
|
||||
else {
|
||||
crontab_string += tab.schedule + " " + tab.command + "\n";
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
fs.writeFile(exports.env_file, env_vars);
|
||||
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
|
||||
exec("crontab /tmp/crontab");
|
||||
});
|
||||
fs.writeFile(exports.env_file, env_vars, function(err) {
|
||||
if (err) callback(err);
|
||||
|
||||
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
|
||||
if (err) return callback(err);
|
||||
|
||||
exec("crontab /tmp/crontab", function(err) {
|
||||
if (err) return callback(err);
|
||||
else callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -6,6 +6,15 @@ function infoMessageBox(message, title){
|
|||
$("#info-title").html(title);
|
||||
$("#info-popup").modal('show');
|
||||
}
|
||||
// like info, but for errors.
|
||||
function errorMessageBox(message) {
|
||||
var msg =
|
||||
"Operation failed: " + message + ". " +
|
||||
"Please see error log for details.";
|
||||
$("#info-body").html(msg);
|
||||
$("#info-title").html("Error");
|
||||
$("#info-popup").modal('show');
|
||||
}
|
||||
// modal with full control
|
||||
function messageBox(body, title, ok_text, close_text, callback){
|
||||
$("#modal-body").html(body);
|
||||
|
@ -50,6 +59,8 @@ function setCrontab(){
|
|||
$.get(routes.crontab, { "env_vars": $("#env_vars").val() }, function(){
|
||||
// TODO show only if success
|
||||
infoMessageBox("Successfuly set crontab file!","Information");
|
||||
}).fail(function(response) {
|
||||
errorMessageBox(response.statusText,"Error");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue