@@ -87,56 +87,104 @@ exports.get_crontab = function(_id, callback) {
87
87
} ) ;
88
88
} ;
89
89
90
- exports . runjob = function ( _id , callback ) {
90
+ exports . runjob = function ( _id ) {
91
91
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 )
95
115
} ) ;
96
116
} ) ;
97
117
} ;
98
118
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
+
99
175
// Set actual crontab file from the db
100
- exports . set_crontab = function ( env_vars , callback ) {
176
+ exports . set_crontab = function ( env_vars , callback ) {
101
177
exports . crontabs ( function ( tabs ) {
102
178
var crontab_string = "" ;
103
179
if ( env_vars ) {
104
- crontab_string = env_vars + "\n" ;
180
+ crontab_string += env_vars ;
181
+ crontab_string += "\n" ;
105
182
}
106
183
tabs . forEach ( function ( tab ) {
107
184
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 )
140
188
crontab_string += "\n" ;
141
189
}
142
190
} ) ;
0 commit comments