Browse Source

mailing stdout and stderr as attachments

v0.2.3
alseambusher 8 years ago
parent
commit
d6a477d0bd
  1. 33
      bin/crontab-ui-mailer.js
  2. 57
      crontab.js
  3. 3
      package.json
  4. 17
      public/js/script.js

33
bin/crontab-ui-mailer.js

@ -1,36 +1,19 @@
#!/usr/bin/env node #!/usr/bin/env node
var defaults = require("../config/mailconfig.js"); var defaults = require("../config/mailconfig.js");
var crontab = require("../crontab.js");
var nodemailer = require('nodemailer'); var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport crontab.get_crontab(process.argv[process.argv.length -1 -2], function(job){
var transporter = nodemailer.createTransport(defaults.transporterStr); var transporter = nodemailer.createTransport(job.mailing.transporterStr);
var mailOptions = defaults.mailOptions; var mailOptions = job.mailing.mailOptions;
var stdin = process.stdin,
stdout = process.stdout,
inputChunks = [];
stdin.resume(); mailOptions.attachments = [{filename: "stdout.txt", path: process.argv[process.argv.length -1 -1]}, {filename: "stderr.txt", path: process.argv[process.argv.length -1]}];
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');
});
transporter.sendMail(mailOptions, function(error, info){ transporter.sendMail(mailOptions, function(error, info){
if(error){ if(error){
return console.log(error); return console.log(error);
} }
console.log('Message sent: ' + info.response); console.log('Message sent: ' + info.response);
});
}); });

57
crontab.js

@ -10,7 +10,6 @@ db.loadDatabase(function (err) {
var exec = require('child_process').exec; 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");
var os = require("os");
exports.log_folder = __dirname + '/crontabs/logs'; exports.log_folder = __dirname + '/crontabs/logs';
exports.env_file = __dirname + '/crontabs/env.db'; 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 // Set actual crontab file from the db
exports.set_crontab = function(env_vars, callback){ exports.set_crontab = function(env_vars, callback){
exports.crontabs( function(tabs){ exports.crontabs( function(tabs){
@ -71,35 +76,33 @@ exports.set_crontab = function(env_vars, callback){
} }
tabs.forEach(function(tab){ tabs.forEach(function(tab){
if(!tab.stopped) { 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") { if (tab.logging && tab.logging == "true") {
let tmp_log = "/tmp/" + tab._id + ".log"; crontab_string += "; if test -f " + stderr +
let log_file = exports.log_folder + "/" + tab._id + ".log"; "; then date >> " + log_file +
if(tab.command[tab.command.length-1] != ";") // add semicolon "; cat " + stderr + " >> " + log_file +
tab.command +=";"; "; fi";
// 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";
}
} }
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";
} }
}); });

3
package.json

@ -14,7 +14,8 @@
"mime": "latest", "mime": "latest",
"cron-parser": "latest", "cron-parser": "latest",
"connect-busboy": "latest", "connect-busboy": "latest",
"moment": "latest" "moment": "latest",
"nodemailer": "latest"
}, },
"engines": { "engines": {
"node": "latest" "node": "latest"

17
public/js/script.js

@ -224,11 +224,26 @@ function setMailConfig(a){
}; };
container.appendChild(button); 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(){ messageBox(container, "Mailing", null, null, function(){
let transporterStr = document.getElementById("transporterInput").value; 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){ if (transporterStr && mailOptions){
a.setAttribute("data-json", JSON.stringify({transporterStr: transporterStr, mailOptions: mailOptions})); a.setAttribute("data-json", JSON.stringify({transporterStr: transporterStr, mailOptions: mailOptions}));
} else {
a.setAttribute("data-json", JSON.stringify({}));
} }
}); });
} }

Loading…
Cancel
Save