Skip to content

Commit 285bcd1

Browse files
devliftmikealseambusher
authored andcommitted
Adding import from crontab
1 parent 78f133a commit 285bcd1

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ app.set('port', (process.env.PORT || 8000));
3434

3535
app.get(routes.root, function(req, res) {
3636
// get all the crontabs
37+
crontab.reload_db();
3738
crontab.crontabs( function(docs){
3839
res.render('index', {
3940
routes : JSON.stringify(routes),

crontab.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,35 @@ exports.get_env = function(){
133133
exports.import_crontab = function(){
134134
exec("crontab -l", function(error, stdout, stderr){
135135
var lines = stdout.split("\n");
136-
lines.forEach(function(line){
137-
/*
138-
trim the spaces at edges
139-
split the line based of space and tab
140-
remove empty splits
141-
If the first character is @
142-
143-
*/
144-
//if(line.indexOf("@")
136+
137+
var namePrefix = new Date().getTime();
138+
139+
lines.forEach(function(line, index){
140+
var regex = /^((\@[a-zA-Z]+\s)|(([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s([^\s]+)\s))/;
141+
var command = line.replace(regex, '').trim();
142+
var schedule = line.replace(command, '').trim();
143+
144+
if(command && schedule){
145+
var name = namePrefix + '_' + index;
146+
147+
db.crontabs.findOne({ command: command, schedule: schedule }, function(err, doc) {
148+
if(err) {
149+
throw err;
150+
}
151+
if(!doc){
152+
exports.create_new(name, command, null, null, schedule, null);
153+
}
154+
else{
155+
doc.command = command;
156+
doc.schedule = schedule;
157+
exports.update(doc);
158+
}
159+
});
160+
161+
162+
}
163+
145164
})
146-
console.log(stdout);
165+
//console.log(stdout);
147166
});
148167
}

public/js/script.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ function setCrontab(){
5353
});
5454
}
5555

56+
function getCrontab(){
57+
messageBox("<p> Do you want to get the crontab file? </p>", "Confirm crontab retrieval", null, null, function(){
58+
$.get(routes.import_crontab, { "env_vars": $("#env_vars").val() }, function(){
59+
// TODO show only if success
60+
infoMessageBox("Successfuly got the crontab file!","Information");
61+
location.reload();
62+
});
63+
});
64+
}
65+
5666
function editJob(_id){
5767
var job = null;
5868
crontabs.forEach(function(crontab){

views/index.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<a class="btn btn-warning" onclick="import_db()"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Import</a>
3636
<a class="btn btn-warning" href="<%= JSON.parse(routes).export %>"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Export</a>
3737
<!--<a class="btn btn-info" onclick="import_crontab()"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Import from crontab</a>-->
38+
<a class="btn btn-success" onclick="getCrontab();"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Get from crontab</a>
3839
<a class="btn btn-success" onclick="setCrontab();"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Save to crontab</a>
3940
<br/>
4041
<br/>

0 commit comments

Comments
 (0)