From 035d432b7ad3e66bc01f4a93250a9be1a95856b5 Mon Sep 17 00:00:00 2001 From: Suresh Alse Date: Fri, 12 Jun 2015 14:41:53 +0530 Subject: [PATCH] reading from crontab db and populating UI --- README.md | 6 ++-- app.js | 4 ++- crontab.js | 10 +++--- public/js/script.js | 84 +++++++++++++++++++++++++++++++++++++++++++++ views/index.ejs | 55 +++++++++++++++++++---------- views/popup.ejs | 65 +++++++++++++++++++++++++++++++++++ 6 files changed, 197 insertions(+), 27 deletions(-) create mode 100644 public/js/script.js create mode 100644 views/popup.ejs diff --git a/README.md b/README.md index e221310..b7baf73 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,20 @@ Crontab UI ========== -Easily manage your cron jobs +Editing the plain text crontab is error prone for managing jobs, e.g., adding jobs, deleting jobs, or pausing jobs. A small mistake can easily bring down all the jobs and might cost you a lot of time. With Crontab UI, it is very easy to manage crontab. Here are the key features of Crontab UI. 1. Easy setup -2. Easy management of jobs +2. Easy and safe adding, deleting or pausing jobs. Easy to maintain hundreds of jobs. 3. Backups 4. Download and schedule scripts which are online +5. Manage crontabs on multiple machines easily. No SSH, No copy-pasting. TODO ==== 1. Run jobs as different user 2. Online backup 3. Profiling jobs +4. Logs diff --git a/app.js b/app.js index afda546..0071993 100755 --- a/app.js +++ b/app.js @@ -18,11 +18,13 @@ app.set('port', (process.env.PORT || 8000)); app.get(routes.root, function(req, res) { // get all the crontabs + //crontab.create_new("/usr/bin/find", "0 2 12 * *"); + //crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *"); crontab.crontabs( function(docs){ console.log(docs); res.render('index', { routes : routes, - crontabs : docs + crontabs : JSON.stringify(docs) }); }); }) diff --git a/crontab.js b/crontab.js index 6dac738..985403e 100644 --- a/crontab.js +++ b/crontab.js @@ -6,8 +6,9 @@ db.loadDatabase(function (err) { var exec = require('child_process').exec; var fs = require('fs'); -crontab = function(command, schedule, stopped){ +crontab = function(name, command, schedule, stopped){ var data = {}; + data.name = name; data.command = command; data.schedule = schedule; data.stopped = stopped; @@ -15,8 +16,8 @@ crontab = function(command, schedule, stopped){ return data; } -exports.create_new = function(command, schedule){ - var tab = crontab(command, schedule, false); +exports.create_new = function(name, command, schedule){ + var tab = crontab(name, command, schedule, false); db.insert(tab); } @@ -35,9 +36,8 @@ exports.set_crontab = function(){ } }); fs.writeFile("/tmp/crontab", crontab_string, function(err) { - //couldnt write + exec("crontab /tmp/crontab"); }); - exec("crontab /tmp/crontab"); }); } diff --git a/public/js/script.js b/public/js/script.js new file mode 100644 index 0000000..49e8411 --- /dev/null +++ b/public/js/script.js @@ -0,0 +1,84 @@ +/*********** MessageBox ****************/ +// simply show info. Only close button +function infoMessageBox(message, title){ + $("#modal-body").html(message); + $("#modal-title").html(title); + $("#modal-button").hide(); + $("#popup").modal('show'); +} +// modal with full control +function messageBox(body, title, ok_text, close_text, callback){ + $("#modal-body").html(body); + $("#modal-title").html(title); + if (ok_text) $("#modal-button").html(ok_text); + $("#modal-button").show(); + if(close_text) $("#modal-close-button").html(close_text); + $("#modal-button").click(callback); + $("#popup").modal("show"); +} + + +/*********** crontab actions ****************/ + +function deleteJob(_id){ + // TODO fix this. pass callback properly + messageBox("

Do you want to delete this Job?

", "Confirm delete", null, null, function(){ + console.log("delete job"); + }); +} + +function stopJob(_id){ + messageBox("

Do you want to stop this Job?

", "Confirm stop job", null, null, function(){ + console.log("stop job"); + }); +} + +function startJob(_id){ + messageBox("

Do you want to start this Job?

", "Confirm start job", null, null, function(){ + console.log("start job"); + }); +} + +function editJob(_id){ + var job = null; + crontabs.forEach(function(crontab){ + if(crontab._id == _id) + job = crontab; + }); + if(job){ + $("#job").modal("show"); + $("#job-name").val(job.name); + $("#job-command").val(job.command); + // if macro not used + if(job.schedule.indexOf("@") != 0){ + var components = job.schedule.split(" "); + console.log(components); + $("#job-minute").val(components[0]); + $("#job-hour").val(components[1]); + $("#job-day").val(components[2]); + $("#job-month").val(components[3]); + $("#job-week").val(components[4]); + } + schedule = job.schedule; + job_command = job.command; + job_string(); + } +} + +function newJob(){ + $("#job").modal("show"); +} + + +// script corresponding to job popup management +var schedule = ""; +var job_command = ""; +function job_string(){ + $("#job-string").val(schedule + " " + job_command); + return schedule + " " + job_command; +} + +function set_schedule(){ + schedule = $("#job-minute").val() + " " +$("#job-hour").val() + " " +$("#job-day").val() + " " +$("#job-month").val() + " " +$("#job-week").val(); + job_string(); +} diff --git a/views/index.ejs b/views/index.ejs index 5a7bda2..c07e02d 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -2,55 +2,72 @@ Crontab UI + + <% include navbar %>
-

Jobs

+

Cronjobs

+ - <% crontabs.forEach(function(crontab){ %> + <% JSON.parse(crontabs).forEach(function(crontab){ %> + <% if (!crontab.stopped) { %> - - - - - <% } else { %> + <% } %> + + - <% } %> <% }); %>
Job Time Last Modified
<%= crontab.command %><%= crontab.schedule %><%= crontab.timestamp %> - Edit - Stop - -
+ <% if (crontab.name) { %> + + <% } %> + <%= crontab.command %> <%= crontab.schedule %> <%= crontab.timestamp %> - Start - + + + <% if (!crontab.stopped) { %> + Edit + Stop + <% } else { %> + Start + <% } %> + +
- New + New Backup Export - - Reset - Save - + Import from crontab + Save to crontab
+
+
+<% include popup.ejs %> diff --git a/views/popup.ejs b/views/popup.ejs new file mode 100644 index 0000000..7d2e06a --- /dev/null +++ b/views/popup.ejs @@ -0,0 +1,65 @@ + + + +