forked from alseambusher/crontab-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.js
137 lines (122 loc) · 3.82 KB
/
crontab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//load database
var Datastore = require('nedb');
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' });
db.loadDatabase(function (err) {
});
var exec = require('child_process').exec;
var fs = require('fs');
var cron_parser = require("cron-parser")
exports.log_folder = __dirname + '/crontabs/logs';
crontab = function(name, command, schedule, stopped, logging){
var data = {};
data.name = name;
data.command = command;
data.schedule = schedule;
if(stopped != null) {
data.stopped = stopped;
}
data.timestamp = (new Date()).toString();
data.logging = logging;
return data;
}
exports.create_new = function(name, command, schedule, logging){
var tab = crontab(name, command, schedule, false, logging);
tab.created = new Date().valueOf();
db.insert(tab);
}
exports.update = function(data){
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null, data.logging));
}
exports.status = function(_id, stopped){
db.update({_id: _id},{$set: {stopped: stopped}});
}
exports.remove = function(_id){
db.remove({_id: _id}, {});
}
exports.crontabs = function(callback){
db.find({}).sort({ created: -1 }).exec(function(err, docs){
for(var i=0; i<docs.length; i++){
if(docs[i].schedule == "@reboot")
docs[i].next = "Next Reboot"
else
docs[i].next = cron_parser.parseExpression(docs[i].schedule).next().toString();
}
callback(docs);
});
}
exports.set_crontab = function(env_vars){
exports.crontabs( function(tabs){
var crontab_string = "";
if (env_vars) {
crontab_string = env_vars + "\n";
}
tabs.forEach(function(tab){
if(!tab.stopped){
if (tab.logging && tab.logging == "true"){
tmp_log = "/tmp/" + tab._id + ".log";
log_file = exports.log_folder + "/" + tab._id + ".log";
if(tab.command[tab.command.length-1] != ";") // add semicolon
tab.command +=";"
//{ command; } 2>/tmp/<id>.log|| {if test -f /tmp/<id>; then date >> <log file>; cat /tmp/<id>.log >> <log file>; rm /tmp<id>.log }
crontab_string += tab.schedule + " { " + tab.command + " } 2> " + tmp_log +"; if test -f " + tmp_log +"; then date >> " + log_file + "; cat " + tmp_log + " >> " + log_file + "; rm " + tmp_log + "; fi \n";
}
else
crontab_string += tab.schedule + " " + tab.command + "\n";
}
});
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
exec("crontab /tmp/crontab");
});
});
}
exports.get_backup_names = function(){
var backups = []
fs.readdirSync(__dirname + '/crontabs').forEach(function(file){
// file name begins with backup
if(file.indexOf("backup") == 0){
backups.push(file);
}
});
// Sort by date. Newest on top
for(var i=0; i<backups.length; i++){
var Ti = backups[i].split("backup")[1]
Ti = new Date(Ti.substring(0, Ti.length-3)).valueOf();
for(var j=0; j<i; j++){
var Tj = backups[j].split("backup")[1]
Tj = new Date(Tj.substring(0, Tj.length-3)).valueOf();
if(Ti > Tj){
var temp = backups[i];
backups[i] = backups[j];
backups[j] = temp;
}
}
}
return backups;
}
exports.backup = function(){
//TODO check if it failed
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString().replace("+", " ") + '.db'));
}
exports.restore = function(db_name){
fs.createReadStream( __dirname + '/crontabs/' + db_name).pipe(fs.createWriteStream( __dirname + '/crontabs/crontab.db'));
db.loadDatabase(); // reload the database
}
exports.reload_db= function(){
db.loadDatabase();
}
// TODO
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("@")
})
console.log(stdout);
});
}