From 39b6b99f4df8744b9bd433b39278d16de51b9fd5 Mon Sep 17 00:00:00 2001 From: Corey Gaspard Date: Wed, 19 Jul 2017 09:16:07 -0500 Subject: [PATCH] Better docker support --- .dockerignore | 2 ++ Dockerfile | 3 +++ crontab.js | 30 ++++++++++++++++++++---------- supervisord.conf | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d992b0f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +.git \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2fd14b4..0a8f85e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,6 +18,9 @@ ENV HOST 0.0.0.0 ENV PORT 8000 +ENV CRON_PATH /tmp +ENV CRON_IN_DOCKER true + EXPOSE $PORT CMD ["supervisord", "-c", "/etc/supervisord.conf"] diff --git a/crontab.js b/crontab.js index 69d9012..1712cf5 100644 --- a/crontab.js +++ b/crontab.js @@ -1,7 +1,14 @@ /*jshint esversion: 6*/ //load database var Datastore = require('nedb'); +var path = require("path"); var db = new Datastore({ filename: __dirname + '/crontabs/crontab.db' }); +var cronPath = "/tmp"; + +if(process.env.CRON_PATH !== undefined) { + console.log(`Path to crond files set using env variables ${process.env.CRON_PATH}`); + cronPath = process.env.CRON_PATH; +} db.loadDatabase(function (err) { if (err) throw err; // no hope, just terminate @@ -76,9 +83,9 @@ exports.set_crontab = function(env_vars, callback){ } tabs.forEach(function(tab){ if(!tab.stopped) { - let stderr = "/tmp/" + tab._id + ".stderr"; - let stdout = "/tmp/" + tab._id + ".stdout"; - let log_file = exports.log_folder + "/" + tab._id + ".log"; + let stderr = path.join(cronPath, tab._id + ".stderr"); + let stdout = path.join(cronPath, tab._id + ".stdout"); + let log_file = path.join(exports.log_folder, tab._id + ".log"); if(tab.command[tab.command.length-1] != ";") // add semicolon tab.command +=";"; @@ -99,7 +106,7 @@ exports.set_crontab = function(env_vars, callback){ } if (tab.mailing && JSON.stringify(tab.mailing) != "{}"){ - crontab_string += "; /usr/local/bin/node " + __dirname + "/bin/crontab-ui-mailer.js " + tab._id + " " + stdout + " " + stderr; + crontab_string += "; " + __dirname + "/bin/crontab-ui-mailer.js " + tab._id + " " + stdout + " " + stderr; } crontab_string += "\n"; @@ -109,13 +116,16 @@ exports.set_crontab = function(env_vars, callback){ fs.writeFile(exports.env_file, env_vars, function(err) { if (err) callback(err); - fs.writeFile("/tmp/crontab", crontab_string, function(err) { + fs.writeFile(path.join(cronPath, "crontab"), crontab_string, function(err) { if (err) return callback(err); - - exec("crontab /tmp/crontab", function(err) { - if (err) return callback(err); - else callback(); - }); + /// In docker we're running crond using busybox implementation of crond + /// It is launched as part of the container startup process, so no need to run it again + if(process.env.CRON_IN_DOCKER === undefined) { + exec("crontab " + path.join(cronPath, "crontab"), function(err) { + if (err) return callback(err); + else callback(); + }); + } }); }); }); diff --git a/supervisord.conf b/supervisord.conf index 93197bd..ad6adc4 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -2,7 +2,7 @@ nodaemon=true [program:crontab] -command=crond -l 2 -f +command=crond -l 2 -f -c %(CRON_PATH)s stderr_logfile = /var/log/crontab-stderr.log stdout_logfile = /var/log/crontab-stdout.log