From d6a477d0bd4430f2bb71487cd6662704808c3e0b Mon Sep 17 00:00:00 2001 From: alseambusher Date: Wed, 14 Dec 2016 18:42:19 +0530 Subject: [PATCH] mailing stdout and stderr as attachments --- bin/crontab-ui-mailer.js | 33 ++++++----------------- crontab.js | 57 +++++++++++++++++++++------------------- package.json | 3 ++- public/js/script.js | 17 +++++++++++- 4 files changed, 56 insertions(+), 54 deletions(-) mode change 100644 => 100755 bin/crontab-ui-mailer.js diff --git a/bin/crontab-ui-mailer.js b/bin/crontab-ui-mailer.js old mode 100644 new mode 100755 index 7025224..3dceedc --- a/bin/crontab-ui-mailer.js +++ b/bin/crontab-ui-mailer.js @@ -1,36 +1,19 @@ #!/usr/bin/env node var defaults = require("../config/mailconfig.js"); - +var crontab = require("../crontab.js"); var nodemailer = require('nodemailer'); -// create reusable transporter object using the default SMTP transport -var transporter = nodemailer.createTransport(defaults.transporterStr); -var mailOptions = defaults.mailOptions; - -var stdin = process.stdin, - stdout = process.stdout, - inputChunks = []; +crontab.get_crontab(process.argv[process.argv.length -1 -2], function(job){ + var transporter = nodemailer.createTransport(job.mailing.transporterStr); + var mailOptions = job.mailing.mailOptions; -stdin.resume(); -stdin.setEncoding('utf8'); - -stdin.on('data', function (chunk) { - inputChunks.push(chunk); -}); - -stdin.on('end', function () { - var inputJSON = inputChunks.join(), - mailOptions = JSON.parse(inputJSON); - - // outputJSON = JSON.stringify(parsedData, null, ' '); - // stdout.write(outputJSON); - // stdout.write('\n'); -}); + mailOptions.attachments = [{filename: "stdout.txt", path: process.argv[process.argv.length -1 -1]}, {filename: "stderr.txt", path: process.argv[process.argv.length -1]}]; -transporter.sendMail(mailOptions, function(error, info){ + transporter.sendMail(mailOptions, function(error, info){ if(error){ - return console.log(error); + return console.log(error); } console.log('Message sent: ' + info.response); + }); }); diff --git a/crontab.js b/crontab.js index 5707610..48b88b4 100644 --- a/crontab.js +++ b/crontab.js @@ -10,7 +10,6 @@ db.loadDatabase(function (err) { var exec = require('child_process').exec; var fs = require('fs'); var cron_parser = require("cron-parser"); -var os = require("os"); exports.log_folder = __dirname + '/crontabs/logs'; exports.env_file = __dirname + '/crontabs/env.db'; @@ -62,6 +61,12 @@ exports.crontabs = function(callback){ }); }; +exports.get_crontab = function(_id, callback) { + db.find({_id: _id}).exec(function(err, docs){ + callback(docs[0]); + }); +}; + // Set actual crontab file from the db exports.set_crontab = function(env_vars, callback){ exports.crontabs( function(tabs){ @@ -71,35 +76,33 @@ 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"; + + if(tab.command[tab.command.length-1] != ";") // add semicolon + tab.command +=";"; + + crontab_string += tab.schedule + " ({ " + tab.command + " } | tee " + stdout + ") 3>&1 1>&2 2>&3 | tee " + stderr; + if (tab.logging && tab.logging == "true") { - let tmp_log = "/tmp/" + tab._id + ".log"; - let log_file = exports.log_folder + "/" + tab._id + ".log"; - if(tab.command[tab.command.length-1] != ";") // add semicolon - tab.command +=";"; - // hook is in beta - if (tab.hook){ - let tmp_hook = "/tmp/" + tab._id + ".hook"; - crontab_string += tab.schedule + " ({ " + tab.command + " } | tee " + tmp_hook + ") 3>&1 1>&2 2>&3 | tee " + tmp_log + - "; if test -f " + tmp_log + - "; then date >> " + log_file + - "; cat " + tmp_log + " >> " + log_file + - "; rm " + tmp_log + - "; fi; if test -f " + tmp_hook + - "; then " + tab.hook + " < " + tmp_hook + - "; rm " + tmp_hook + - "; fi \n"; - } else { - 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"; - } + crontab_string += "; if test -f " + stderr + + "; then date >> " + log_file + + "; cat " + stderr + " >> " + log_file + + "; fi"; } - else { - crontab_string += tab.schedule + " " + tab.command + "\n"; + + if (tab.hook) { + crontab_string += "; if test -f " + stdout + + "; then " + tab.hook + " < " + stdout + + "; fi"; } + + 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 += "\n"; } }); diff --git a/package.json b/package.json index 85a359d..b44ac90 100755 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "mime": "latest", "cron-parser": "latest", "connect-busboy": "latest", - "moment": "latest" + "moment": "latest", + "nodemailer": "latest" }, "engines": { "node": "latest" diff --git a/public/js/script.js b/public/js/script.js index 9777b19..39fc8b9 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -224,11 +224,26 @@ function setMailConfig(a){ }; container.appendChild(button); + let buttonClear = document.createElement("a"); + buttonClear.className = "btn btn-default btn-small"; + buttonClear.innerHTML = "Clear"; + buttonClear.onclick = function(){ + document.getElementById("transporterInput").value = ""; + document.getElementById("mailOptionsInput").innerHTML = ""; + }; + container.appendChild(buttonClear); + messageBox(container, "Mailing", null, null, function(){ let transporterStr = document.getElementById("transporterInput").value; - let mailOptions = JSON.parse(document.getElementById("mailOptionsInput").innerHTML); + let mailOptions; + try{ + mailOptions = JSON.parse(document.getElementById("mailOptionsInput").value); + } catch (err) {} + if (transporterStr && mailOptions){ a.setAttribute("data-json", JSON.stringify({transporterStr: transporterStr, mailOptions: mailOptions})); + } else { + a.setAttribute("data-json", JSON.stringify({})); } }); }