Added error log support
parent
01dbc47ef3
commit
860e08a336
|
@ -1,3 +1,4 @@
|
||||||
*.swp
|
*.swp
|
||||||
node_modules
|
node_modules
|
||||||
crontabs/*.db
|
crontabs/*.db
|
||||||
|
crontabs/logs/*.log
|
||||||
|
|
9
app.js
9
app.js
|
@ -37,7 +37,7 @@ app.get(routes.root, function(req, res) {
|
||||||
res.render('index', {
|
res.render('index', {
|
||||||
routes : JSON.stringify(routes),
|
routes : JSON.stringify(routes),
|
||||||
crontabs : JSON.stringify(docs),
|
crontabs : JSON.stringify(docs),
|
||||||
backups : crontab.get_backup_names()
|
backups : crontab.get_backup_names(),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -45,7 +45,7 @@ app.get(routes.root, function(req, res) {
|
||||||
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){
|
||||||
crontab.create_new(req.body.name, req.body.command, req.body.schedule);
|
crontab.create_new(req.body.name, req.body.command, req.body.schedule, req.body.logging);
|
||||||
}
|
}
|
||||||
// edit job
|
// edit job
|
||||||
else{
|
else{
|
||||||
|
@ -132,6 +132,11 @@ app.get(routes.import_crontab, function(req, res) {
|
||||||
res.end();
|
res.end();
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get(routes.logger, function(req, res) {
|
||||||
|
console.log(crontab.log_folder)
|
||||||
|
res.sendFile(crontab.log_folder +"/"+req.query.id+".log");
|
||||||
|
})
|
||||||
|
|
||||||
app.listen(app.get('port'), function() {
|
app.listen(app.get('port'), function() {
|
||||||
console.log("Crontab UI is running at localhost:" + app.get('port'))
|
console.log("Crontab UI is running at localhost:" + app.get('port'))
|
||||||
})
|
})
|
||||||
|
|
22
crontab.js
22
crontab.js
|
@ -7,7 +7,9 @@ var exec = require('child_process').exec;
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var cron_parser = require("cron-parser")
|
var cron_parser = require("cron-parser")
|
||||||
|
|
||||||
crontab = function(name, command, schedule, stopped){
|
exports.log_folder = __dirname + '/crontabs/logs';
|
||||||
|
|
||||||
|
crontab = function(name, command, schedule, stopped, logging){
|
||||||
var data = {};
|
var data = {};
|
||||||
data.name = name;
|
data.name = name;
|
||||||
data.command = command;
|
data.command = command;
|
||||||
|
@ -16,17 +18,18 @@ crontab = function(name, command, schedule, stopped){
|
||||||
data.stopped = stopped;
|
data.stopped = stopped;
|
||||||
}
|
}
|
||||||
data.timestamp = (new Date()).toString();
|
data.timestamp = (new Date()).toString();
|
||||||
|
data.logging = logging;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create_new = function(name, command, schedule){
|
exports.create_new = function(name, command, schedule, logging){
|
||||||
var tab = crontab(name, command, schedule, false);
|
var tab = crontab(name, command, schedule, false, logging);
|
||||||
tab.created = new Date().valueOf();
|
tab.created = new Date().valueOf();
|
||||||
db.insert(tab);
|
db.insert(tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.update = function(data){
|
exports.update = function(data){
|
||||||
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null));
|
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null, data.logging));
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.status = function(_id, stopped){
|
exports.status = function(_id, stopped){
|
||||||
|
@ -52,7 +55,16 @@ exports.set_crontab = function(){
|
||||||
var crontab_string = "";
|
var crontab_string = "";
|
||||||
tabs.forEach(function(tab){
|
tabs.forEach(function(tab){
|
||||||
if(!tab.stopped){
|
if(!tab.stopped){
|
||||||
crontab_string += tab.schedule + " " + tab.command + "\n";
|
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
|
||||||
|
crontab_string += tab.schedule + " " + tab.command + "\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
|
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
This folder will have all the log files
|
|
@ -74,13 +74,16 @@ function editJob(_id){
|
||||||
}
|
}
|
||||||
schedule = job.schedule;
|
schedule = job.schedule;
|
||||||
job_command = job.command;
|
job_command = job.command;
|
||||||
|
console.log(job.logging)
|
||||||
|
if (job.logging && job.logging != "false")
|
||||||
|
$("#job-logging").prop("checked", true);
|
||||||
job_string();
|
job_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
$("#job-save").unbind("click"); // remove existing events attached to this
|
$("#job-save").unbind("click"); // remove existing events attached to this
|
||||||
$("#job-save").click(function(){
|
$("#job-save").click(function(){
|
||||||
// TODO good old boring validations
|
// TODO good old boring validations
|
||||||
$.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){
|
$.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id, logging: $("#job-logging").prop("checked")}, function(){
|
||||||
location.reload();
|
location.reload();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -102,7 +105,7 @@ function newJob(){
|
||||||
$("#job-save").unbind("click"); // remove existing events attached to this
|
$("#job-save").unbind("click"); // remove existing events attached to this
|
||||||
$("#job-save").click(function(){
|
$("#job-save").click(function(){
|
||||||
// TODO good old boring validations
|
// TODO good old boring validations
|
||||||
$.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){
|
$.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1, logging: $("#job-logging").prop("checked")}, function(){
|
||||||
location.reload();
|
location.reload();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,4 +12,5 @@ exports.routes = {
|
||||||
"export": "/export",
|
"export": "/export",
|
||||||
"import": "/import", // this is import from database
|
"import": "/import", // this is import from database
|
||||||
"import_crontab": "/import_crontab", // this is from existing crontab
|
"import_crontab": "/import_crontab", // this is from existing crontab
|
||||||
|
"logger": "/logger",
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,11 +63,14 @@
|
||||||
</td>
|
</td>
|
||||||
<td><%= crontab.command %></td>
|
<td><%= crontab.command %></td>
|
||||||
<td><span style="cursor:pointer" data-toggle="tooltip" data-placement="bottom" title="<%= crontab.next %>"><%= crontab.schedule %></span></td>
|
<td><span style="cursor:pointer" data-toggle="tooltip" data-placement="bottom" title="<%= crontab.next %>"><%= crontab.schedule %></span></td>
|
||||||
<td><%= crontab.timestamp %></td>
|
<td style="width:20%"><%= crontab.timestamp %></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
<!-- controls based on crontab state -->
|
<!-- controls based on crontab state -->
|
||||||
<% if (!crontab.stopped) { %>
|
<% if (!crontab.stopped) { %>
|
||||||
|
<% if (crontab.logging && crontab.logging != "false") {%>
|
||||||
|
<a class="btn btn-primary" data-toggle="tooltip" data-placement="left" title="See Log" href="<%= JSON.parse(routes).logger + '?id=' + crontab._id %>" target="_blank"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span></a>
|
||||||
|
<% } %>
|
||||||
<a class="btn btn-primary" onclick="editJob('<%= crontab._id %>')"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Edit</a>
|
<a class="btn btn-primary" onclick="editJob('<%= crontab._id %>')"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Edit</a>
|
||||||
<a class="btn btn-info" onclick="stopJob('<%= crontab._id %>')"><span class="glyphicon glyphicon-stop" aria-hidden="true"></span> Stop</a>
|
<a class="btn btn-info" onclick="stopJob('<%= crontab._id %>')"><span class="glyphicon glyphicon-stop" aria-hidden="true"></span> Stop</a>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
|
|
|
@ -71,6 +71,7 @@
|
||||||
<br />
|
<br />
|
||||||
<label>Job</label>
|
<label>Job</label>
|
||||||
<input type='text' class='form-control' id='job-string' disabled='disabled'/><br />
|
<input type='text' class='form-control' id='job-string' disabled='disabled'/><br />
|
||||||
|
<label><input type="checkbox" id="job-logging" style="position:relative;top:2px"/> Enable error logging.</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
||||||
|
|
Loading…
Reference in New Issue