Adding support custom for CRON_DB_PATH

pull/107/head^2
alse 5 years ago
parent 970e461621
commit 8315ca4f50

@ -153,7 +153,7 @@ app.get(routes.restore_backup, function(req, res) {
// export current crontab db so that user can download it // export current crontab db so that user can download it
app.get(routes.export, function(req, res) { app.get(routes.export, function(req, res) {
var file = __dirname + '/crontabs/crontab.db'; var file = crontab.crontab_db_file;
var filename = path.basename(file); var filename = path.basename(file);
var mimetype = mime.lookup(file); var mimetype = mime.lookup(file);
@ -170,7 +170,7 @@ app.post(routes.import, function(req, res) {
var fstream; var fstream;
req.pipe(req.busboy); req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) { req.busboy.on('file', function (fieldname, file, filename) {
fstream = fs.createWriteStream(__dirname + '/crontabs/crontab.db'); fstream = fs.createWriteStream(crontab.crontab_db_file);
file.pipe(fstream); file.pipe(fstream);
fstream.on('close', function () { fstream.on('close', function () {
crontab.reload_db(); crontab.reload_db();
@ -244,7 +244,7 @@ app.listen(app.get('port'), app.get('host'), function() {
// we do this by watching log file and setting a on change hook to it // we do this by watching log file and setting a on change hook to it
if (process.argv.includes("--autosave")){ if (process.argv.includes("--autosave")){
crontab.autosave_crontab(()=>{}); crontab.autosave_crontab(()=>{});
fs.watchFile(__dirname + '/crontabs/crontab.db', () => { fs.watchFile(crontab.crontab_db_file, () => {
crontab.autosave_crontab(()=>{ crontab.autosave_crontab(()=>{
console.log("Attempted to autosave crontab"); console.log("Attempted to autosave crontab");
}); });
@ -252,8 +252,8 @@ app.listen(app.get('port'), app.get('host'), function() {
} }
if (process.argv.includes("--reset")){ if (process.argv.includes("--reset")){
console.log("Resetting crontab-ui"); console.log("Resetting crontab-ui");
var crontabdb = __dirname + "/crontabs/crontab.db"; var crontabdb = crontab.crontab_db_file;
var envdb = __dirname + "/crontabs/env.db"; var envdb = crontab.env_file;
console.log("Deleting " + crontabdb); console.log("Deleting " + crontabdb);
try{ try{

@ -2,7 +2,14 @@
//load database //load database
var Datastore = require('nedb'); var Datastore = require('nedb');
var path = require("path"); var path = require("path");
var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' });
exports.db_folder = process.env.CRON_DB_PATH === undefined ? path.join(__dirname, "crontabs") : process.env.CRON_DB_PATH;
console.log("Cron db path: " + exports.db_folder);
exports.log_folder = path.join(exports.db_folder, 'logs');
exports.env_file = path.join(exports.db_folder, 'env.db');
exports.crontab_db_file = path.join(exports.db_folder, 'crontab.db');
var db = new Datastore({ filename: exports.crontab_db_file});
var cronPath = "/tmp"; var cronPath = "/tmp";
if(process.env.CRON_PATH !== undefined) { if(process.env.CRON_PATH !== undefined) {
@ -18,8 +25,6 @@ var exec = require('child_process').exec;
var fs = require('fs'); var fs = require('fs');
var cron_parser = require("cron-parser"); var cron_parser = require("cron-parser");
exports.log_folder = __dirname + '/crontabs/logs';
exports.env_file = __dirname + '/crontabs/env.db';
crontab = function(name, command, schedule, stopped, logging, mailing){ crontab = function(name, command, schedule, stopped, logging, mailing){
var data = {}; var data = {};
@ -160,7 +165,7 @@ exports.set_crontab = function(env_vars, callback){
exports.get_backup_names = function(){ exports.get_backup_names = function(){
var backups = []; var backups = [];
fs.readdirSync(__dirname + '/crontabs').forEach(function(file){ fs.readdirSync(exports.db_folder).forEach(function(file){
// file name begins with backup // file name begins with backup
if(file.indexOf("backup") === 0){ if(file.indexOf("backup") === 0){
backups.push(file); backups.push(file);
@ -187,11 +192,11 @@ exports.get_backup_names = function(){
exports.backup = function(){ exports.backup = function(){
//TODO check if it failed //TODO check if it failed
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString().replace("+", " ") + '.db')); fs.createReadStream(exports.crontab_db_file).pipe(fs.createWriteStream( path.join(exports.db_folder, 'backup ' + (new Date()).toString().replace("+", " ") + '.db')));
}; };
exports.restore = function(db_name){ exports.restore = function(db_name){
fs.createReadStream( __dirname + '/crontabs/' + db_name).pipe(fs.createWriteStream( __dirname + '/crontabs/crontab.db')); fs.createReadStream(path.join(exports.db_folder, db_name)).pipe(fs.createWriteStream(exports.crontab_db_file));
db.loadDatabase(); // reload the database db.loadDatabase(); // reload the database
}; };

Loading…
Cancel
Save