Skip to content

Commit

Permalink
Adding import from crontab
Browse files Browse the repository at this point in the history
  • Loading branch information
devliftmike authored and alseambusher committed Aug 5, 2016
1 parent 78f133a commit 285bcd1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ app.set('port', (process.env.PORT || 8000));

app.get(routes.root, function(req, res) {
// get all the crontabs
crontab.reload_db();
crontab.crontabs( function(docs){
res.render('index', {
routes : JSON.stringify(routes),
Expand Down
39 changes: 29 additions & 10 deletions crontab.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,35 @@ exports.get_env = function(){
exports.import_crontab = function(){
exec("crontab -l", function(error, stdout, stderr){
var lines = stdout.split("\n");
lines.forEach(function(line){
/*
trim the spaces at edges
split the line based of space and tab
remove empty splits
If the first character is @
*/
//if(line.indexOf("@")

var namePrefix = new Date().getTime();

lines.forEach(function(line, index){
var regex = /^((\@[a-zA-Z]+\s)|(([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s))/;
var command = line.replace(regex, '').trim();
var schedule = line.replace(command, '').trim();

if(command && schedule){
var name = namePrefix + '_' + index;

db.crontabs.findOne({ command: command, schedule: schedule }, function(err, doc) {
if(err) {
throw err;
}
if(!doc){
exports.create_new(name, command, null, null, schedule, null);
}
else{
doc.command = command;
doc.schedule = schedule;
exports.update(doc);
}
});


}

})
console.log(stdout);
//console.log(stdout);
});
}
10 changes: 10 additions & 0 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ function setCrontab(){
});
}

function getCrontab(){
messageBox("<p> Do you want to get the crontab file? </p>", "Confirm crontab retrieval", null, null, function(){
$.get(routes.import_crontab, { "env_vars": $("#env_vars").val() }, function(){
// TODO show only if success
infoMessageBox("Successfuly got the crontab file!","Information");
location.reload();
});
});
}

function editJob(_id){
var job = null;
crontabs.forEach(function(crontab){
Expand Down
1 change: 1 addition & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<a class="btn btn-warning" onclick="import_db()"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Import</a>
<a class="btn btn-warning" href="<%= JSON.parse(routes).export %>"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Export</a>
<!--<a class="btn btn-info" onclick="import_crontab()"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Import from crontab</a>-->
<a class="btn btn-success" onclick="getCrontab();"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Get from crontab</a>
<a class="btn btn-success" onclick="setCrontab();"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Save to crontab</a>
<br/>
<br/>
Expand Down

0 comments on commit 285bcd1

Please sign in to comment.