You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crontab-ui/crontab.js

123 lines
3.1 KiB

//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');
10 years ago
var cron_parser = require("cron-parser")
crontab = function(name, command, schedule, stopped){
var data = {};
data.name = name;
data.command = command;
data.schedule = schedule;
10 years ago
if(stopped != null) {
data.stopped = stopped;
}
data.timestamp = (new Date()).toString();
return data;
}
exports.create_new = function(name, command, schedule){
var tab = crontab(name, command, schedule, false);
10 years ago
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));
}
10 years ago
exports.status = function(_id, stopped){
db.update({_id: _id},{$set: {stopped: stopped}});
}
exports.remove = function(_id){
db.remove({_id: _id}, {});
}
exports.crontabs = function(callback){
10 years ago
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(){
exports.crontabs( function(tabs){
var crontab_string = "";
tabs.forEach(function(tab){
if(!tab.stopped){
crontab_string += tab.schedule + " " + tab.command + "\n";
}
});
fs.writeFile("/tmp/crontab", crontab_string, function(err) {
exec("crontab /tmp/crontab");
});
});
}
10 years ago
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);
}
});
10 years ago
// 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;
}
}
}
10 years ago
return backups;
}
exports.backup = function(){
//TODO check if it failed
10 years ago
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
}
10 years ago
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);
});
10 years ago
}