You've already forked crontab-ui
mirror of
https://github.com/alseambusher/crontab-ui.git
synced 2025-12-15 11:43:58 +08:00
Proper, node-js style error handling for set_crontab
It's a start...
This commit is contained in:
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user