Skip to content

Commit 17a1823

Browse files
committed
Merge branch 'feature/log-runjob' of https://github.com/AndreiArdelean1/crontab-ui into AndreiArdelean1-feature/log-runjob
2 parents ab42a39 + 0da601a commit 17a1823

File tree

1 file changed

+86
-38
lines changed

1 file changed

+86
-38
lines changed

crontab.js

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -87,56 +87,104 @@ exports.get_crontab = function(_id, callback) {
8787
});
8888
};
8989

90-
exports.runjob = function(_id, callback) {
90+
exports.runjob = function(_id) {
9191
db.find({_id: _id}).exec(function(err, docs){
92-
var res = docs[0];
93-
exec(res.command, function(error, stdout, stderr){
94-
console.log(stdout);
92+
let res = docs[0];
93+
94+
let env_vars = exports.get_env()
95+
96+
var crontab_job_string_command = crontabCommand_noEnvVars(res)
97+
98+
crontab_job_string_command = addEnvironmentVariablesToCommand(env_vars, crontab_job_string_command)
99+
100+
console.log("Running job")
101+
console.log("ID: " + _id)
102+
console.log("Original command: " + res.command)
103+
console.log("Executed command: " + crontab_job_string_command)
104+
105+
exec(crontab_job_string_command, function(error, stdout, stderr){
106+
if (error) {
107+
console.log("Error:")
108+
console.log(error)
109+
}
110+
111+
console.log("stdout:")
112+
console.log(stdout)
113+
console.log("stderr:")
114+
console.log(stderr)
95115
});
96116
});
97117
};
98118

119+
crontabCommand_noEnvVars = function(tab) {
120+
var crontab_job_string = "";
121+
122+
let stderr = path.join(cronPath, tab._id + ".stderr");
123+
let stdout = path.join(cronPath, tab._id + ".stdout");
124+
let log_file = path.join(exports.log_folder, tab._id + ".log");
125+
let log_file_stdout = path.join(exports.log_folder, tab._id + ".stdout.log");
126+
127+
var crontab_job_string_command = tab.command
128+
129+
if(crontab_job_string_command[crontab_job_string_command.length-1] != ";") { // add semicolon
130+
crontab_job_string_command +=";";
131+
}
132+
133+
crontab_job_string = crontab_job_string_command
134+
crontab_job_string = "{ " + crontab_job_string + " }"
135+
// write stdout to file
136+
crontab_job_string = "(" + crontab_job_string + " | tee " + stdout + ")"
137+
// write stderr to file
138+
crontab_job_string = "(" + crontab_job_string + " 3>&1 1>&2 2>&3 | tee " + stderr + ") 3>&1 1>&2 2>&3"
139+
crontab_job_string = "(" + crontab_job_string + ")"
140+
141+
if (tab.logging && tab.logging == "true") {
142+
crontab_job_string += "; if test -f " + stderr +
143+
"; then date >> \"" + log_file + "\"" +
144+
"; cat " + stderr + " >> \"" + log_file + "\"" +
145+
"; fi";
146+
147+
crontab_job_string += "; if test -f " + stdout +
148+
"; then date >> \"" + log_file_stdout + "\"" +
149+
"; cat " + stdout + " >> \"" + log_file_stdout + "\"" +
150+
"; fi";
151+
}
152+
153+
if (tab.hook) {
154+
crontab_job_string += "; if test -f " + stdout +
155+
"; then " + tab.hook + " < " + stdout +
156+
"; fi";
157+
}
158+
159+
if (tab.mailing && JSON.stringify(tab.mailing) != "{}"){
160+
crontab_job_string += "; /usr/local/bin/node " + __dirname + "/bin/crontab-ui-mailer.js " + tab._id + " " + stdout + " " + stderr;
161+
}
162+
163+
return crontab_job_string
164+
}
165+
166+
addEnvironmentVariablesToCommand = function(env_vars, command) {
167+
if (env_vars) {
168+
let oneLineEnvVars = env_vars.replace(/\s*\n\s*/g,' ').trim()
169+
return "(" + oneLineEnvVars + "; (" + command + "))"
170+
}
171+
172+
return "(" + command + ")"
173+
}
174+
99175
// Set actual crontab file from the db
100-
exports.set_crontab = function(env_vars, callback){
176+
exports.set_crontab = function(env_vars, callback) {
101177
exports.crontabs( function(tabs){
102178
var crontab_string = "";
103179
if (env_vars) {
104-
crontab_string = env_vars + "\n";
180+
crontab_string += env_vars;
181+
crontab_string += "\n";
105182
}
106183
tabs.forEach(function(tab){
107184
if(!tab.stopped) {
108-
let stderr = path.join(cronPath, tab._id + ".stderr");
109-
let stdout = path.join(cronPath, tab._id + ".stdout");
110-
let log_file = path.join(exports.log_folder, tab._id + ".log");
111-
let log_file_stdout = path.join(exports.log_folder, tab._id + ".stdout.log");
112-
113-
if(tab.command[tab.command.length-1] != ";") // add semicolon
114-
tab.command +=";";
115-
116-
crontab_string += tab.schedule + " ({ " + tab.command + " } | tee " + stdout + ") 3>&1 1>&2 2>&3 | tee " + stderr;
117-
118-
if (tab.logging && tab.logging == "true") {
119-
crontab_string += "; if test -f " + stderr +
120-
"; then date >> \"" + log_file + "\"" +
121-
"; cat " + stderr + " >> \"" + log_file + "\"" +
122-
"; fi";
123-
124-
crontab_string += "; if test -f " + stdout +
125-
"; then date >> \"" + log_file_stdout + "\"" +
126-
"; cat " + stdout + " >> \"" + log_file_stdout + "\"" +
127-
"; fi";
128-
}
129-
130-
if (tab.hook) {
131-
crontab_string += "; if test -f " + stdout +
132-
"; then " + tab.hook + " < " + stdout +
133-
"; fi";
134-
}
135-
136-
if (tab.mailing && JSON.stringify(tab.mailing) != "{}"){
137-
crontab_string += "; /usr/local/bin/node " + __dirname + "/bin/crontab-ui-mailer.js " + tab._id + " " + stdout + " " + stderr;
138-
}
139-
185+
crontab_string += tab.schedule
186+
crontab_string += " "
187+
crontab_string += crontabCommand_noEnvVars(tab)
140188
crontab_string += "\n";
141189
}
142190
});

0 commit comments

Comments
 (0)