diff --git a/app.js b/app.js index 17d6896..90d9119 100755 --- a/app.js +++ b/app.js @@ -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), diff --git a/crontab.js b/crontab.js index 5401850..92e9784 100644 --- a/crontab.js +++ b/crontab.js @@ -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); }); } diff --git a/public/js/script.js b/public/js/script.js index 943cec5..878546e 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -53,6 +53,16 @@ function setCrontab(){ }); } +function getCrontab(){ + messageBox("

Do you want to get the crontab file?

", "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){ diff --git a/views/index.ejs b/views/index.ejs index 902306c..60b0da2 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -35,6 +35,7 @@ Import Export + Get from crontab Save to crontab