Browse Source

Added error log support

v0.1.5
alseambusher 9 years ago
parent
commit
860e08a336
  1. 1
      .gitignore
  2. 9
      app.js
  3. 22
      crontab.js
  4. 1
      crontabs/logs/README.md
  5. 7
      public/js/script.js
  6. 1
      routes.js
  7. 5
      views/index.ejs
  8. 1
      views/popup.ejs

1
.gitignore vendored

@ -1,3 +1,4 @@
*.swp
node_modules
crontabs/*.db
crontabs/logs/*.log

9
app.js

@ -37,7 +37,7 @@ app.get(routes.root, function(req, res) {
res.render('index', {
routes : JSON.stringify(routes),
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) {
// new job
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
else{
@ -132,6 +132,11 @@ app.get(routes.import_crontab, function(req, res) {
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() {
console.log("Crontab UI is running at localhost:" + app.get('port'))
})

22
crontab.js

@ -7,7 +7,9 @@ var exec = require('child_process').exec;
var fs = require('fs');
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 = {};
data.name = name;
data.command = command;
@ -16,17 +18,18 @@ crontab = function(name, command, schedule, stopped){
data.stopped = stopped;
}
data.timestamp = (new Date()).toString();
data.logging = logging;
return data;
}
exports.create_new = function(name, command, schedule){
var tab = crontab(name, command, schedule, false);
exports.create_new = function(name, command, schedule, logging){
var tab = crontab(name, command, schedule, false, logging);
tab.created = new Date().valueOf();
db.insert(tab);
}
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){
@ -52,7 +55,16 @@ exports.set_crontab = function(){
var crontab_string = "";
tabs.forEach(function(tab){
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) {

1
crontabs/logs/README.md

@ -0,0 +1 @@
This folder will have all the log files

7
public/js/script.js

@ -74,13 +74,16 @@ function editJob(_id){
}
schedule = job.schedule;
job_command = job.command;
console.log(job.logging)
if (job.logging && job.logging != "false")
$("#job-logging").prop("checked", true);
job_string();
}
$("#job-save").unbind("click"); // remove existing events attached to this
$("#job-save").click(function(){
// 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();
})
});
@ -102,7 +105,7 @@ function newJob(){
$("#job-save").unbind("click"); // remove existing events attached to this
$("#job-save").click(function(){
// 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();
})
});

1
routes.js

@ -12,4 +12,5 @@ exports.routes = {
"export": "/export",
"import": "/import", // this is import from database
"import_crontab": "/import_crontab", // this is from existing crontab
"logger": "/logger",
}

5
views/index.ejs

@ -63,11 +63,14 @@
</td>
<td><%= crontab.command %></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>
<!-- controls based on crontab state -->
<% 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-info" onclick="stopJob('<%= crontab._id %>')"><span class="glyphicon glyphicon-stop" aria-hidden="true"></span> Stop</a>
<% } else { %>

1
views/popup.ejs

@ -71,6 +71,7 @@
<br />
<label>Job</label>
<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 class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

Loading…
Cancel
Save