From 8315ca4f5036ef93193486bfff5dd6cd7edd52bb Mon Sep 17 00:00:00 2001 From: alse Date: Tue, 9 Jun 2020 19:08:22 +0530 Subject: [PATCH] Adding support custom for CRON_DB_PATH --- app.js | 10 +++++----- crontab.js | 17 +++++++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index c9309d2..8e28cca 100755 --- a/app.js +++ b/app.js @@ -153,7 +153,7 @@ app.get(routes.restore_backup, function(req, res) { // export current crontab db so that user can download it 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 mimetype = mime.lookup(file); @@ -170,7 +170,7 @@ app.post(routes.import, function(req, res) { var fstream; req.pipe(req.busboy); 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); fstream.on('close', function () { 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 if (process.argv.includes("--autosave")){ crontab.autosave_crontab(()=>{}); - fs.watchFile(__dirname + '/crontabs/crontab.db', () => { + fs.watchFile(crontab.crontab_db_file, () => { crontab.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")){ console.log("Resetting crontab-ui"); - var crontabdb = __dirname + "/crontabs/crontab.db"; - var envdb = __dirname + "/crontabs/env.db"; + var crontabdb = crontab.crontab_db_file; + var envdb = crontab.env_file; console.log("Deleting " + crontabdb); try{ diff --git a/crontab.js b/crontab.js index dbd701a..6e588b1 100644 --- a/crontab.js +++ b/crontab.js @@ -2,7 +2,14 @@ //load database var Datastore = require('nedb'); 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"; if(process.env.CRON_PATH !== undefined) { @@ -18,8 +25,6 @@ var exec = require('child_process').exec; var fs = require('fs'); 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){ var data = {}; @@ -160,7 +165,7 @@ exports.set_crontab = function(env_vars, callback){ exports.get_backup_names = function(){ var backups = []; - fs.readdirSync(__dirname + '/crontabs').forEach(function(file){ + fs.readdirSync(exports.db_folder).forEach(function(file){ // file name begins with backup if(file.indexOf("backup") === 0){ backups.push(file); @@ -187,11 +192,11 @@ exports.get_backup_names = function(){ 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')); + 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){ - 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 };