Create and Edit crontabs

pull/1/head
Suresh Alse 10 years ago
parent 035d432b7a
commit b73c16c8c4

@ -8,6 +8,12 @@ var routes = require("./routes").routes;
// set the view engine to ejs // set the view engine to ejs
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// include all folders // include all folders
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public/css')); app.use(express.static(__dirname + '/public/css'));
@ -21,7 +27,6 @@ app.get(routes.root, function(req, res) {
//crontab.create_new("/usr/bin/find", "0 2 12 * *"); //crontab.create_new("/usr/bin/find", "0 2 12 * *");
//crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *"); //crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *");
crontab.crontabs( function(docs){ crontab.crontabs( function(docs){
console.log(docs);
res.render('index', { res.render('index', {
routes : routes, routes : routes,
crontabs : JSON.stringify(docs) crontabs : JSON.stringify(docs)
@ -29,8 +34,16 @@ app.get(routes.root, function(req, res) {
}); });
}) })
app.get(routes.save, function(req, res) { app.post(routes.save, function(req, res) {
res.render('index'); // new job
if(req.body._id == -1){
crontab.create_new(req.body.name, req.body.command, req.body.schedule);
}
// edit job
else{
crontab.update(req.body);
}
res.end();
}) })
app.listen(app.get('port'), function() { app.listen(app.get('port'), function() {

@ -11,7 +11,7 @@ crontab = function(name, command, schedule, stopped){
data.name = name; data.name = name;
data.command = command; data.command = command;
data.schedule = schedule; data.schedule = schedule;
data.stopped = stopped; if(stopped != null) data.stopped = stopped;
data.timestamp = (new Date()).toString(); data.timestamp = (new Date()).toString();
return data; return data;
} }
@ -21,12 +21,14 @@ exports.create_new = function(name, command, schedule){
db.insert(tab); db.insert(tab);
} }
exports.update = function(data){
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null));
}
exports.crontabs = function(callback){ exports.crontabs = function(callback){
db.find({}, function(err, docs){ db.find({}, function(err, docs){
callback(docs); callback(docs);
}); });
} }
exports.set_crontab = function(){ exports.set_crontab = function(){
exports.crontabs( function(tabs){ exports.crontabs( function(tabs){
var crontab_string = ""; var crontab_string = "";

@ -9,7 +9,8 @@
"dependencies": { "dependencies": {
"express": "latest", "express": "latest",
"ejs": "latest", "ejs": "latest",
"nedb": "latest" "nedb": "latest",
"body-parser": "latest"
}, },
"engines": { "engines": {
"node": "latest" "node": "latest"

@ -52,7 +52,6 @@ function editJob(_id){
// if macro not used // if macro not used
if(job.schedule.indexOf("@") != 0){ if(job.schedule.indexOf("@") != 0){
var components = job.schedule.split(" "); var components = job.schedule.split(" ");
console.log(components);
$("#job-minute").val(components[0]); $("#job-minute").val(components[0]);
$("#job-hour").val(components[1]); $("#job-hour").val(components[1]);
$("#job-day").val(components[2]); $("#job-day").val(components[2]);
@ -63,10 +62,34 @@ function editJob(_id){
job_command = job.command; job_command = job.command;
job_string(); job_string();
} }
$("#job-save").click(function(){
// TODO good old boring validations
$.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){
location.reload();
})
});
} }
function newJob(){ function newJob(){
schedule = ""
job_command = ""
$("#job-minute").val("*");
$("#job-hour").val("*");
$("#job-day").val("*");
$("#job-month").val("*");
$("#job-week").val("*");
$("#job").modal("show"); $("#job").modal("show");
$("#job-name").val("");
$("#job-command").val("");
job_string();
$("#job-save").click(function(){
// TODO good old boring validations
$.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){
location.reload();
})
});
} }
@ -82,3 +105,4 @@ function set_schedule(){
schedule = $("#job-minute").val() + " " +$("#job-hour").val() + " " +$("#job-day").val() + " " +$("#job-month").val() + " " +$("#job-week").val(); schedule = $("#job-minute").val() + " " +$("#job-hour").val() + " " +$("#job-day").val() + " " +$("#job-month").val() + " " +$("#job-week").val();
job_string(); job_string();
} }

@ -1,5 +1,6 @@
exports.routes = { exports.routes = {
"root" : "/", "root" : "/",
"downloads" : "/downloads", "downloads" : "/downloads",
"save" : "/save" "save" : "/save",
"crontab" : "/crontab"
} }

@ -58,7 +58,7 @@
</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>
<button type="button" class="btn btn-primary" data-dismiss="modal">Save</button> <button type="button" class="btn btn-primary" data-dismiss="modal" id="job-save">Save</button>
</div> </div>
</div><!-- /.modal-content --> </div><!-- /.modal-content -->
</div><!-- /.modal-dialog --> </div><!-- /.modal-dialog -->

Loading…
Cancel
Save