-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathjob.js
More file actions
404 lines (372 loc) · 17.8 KB
/
job.js
File metadata and controls
404 lines (372 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
var request = require('request');
var util = require('util');
var opn = require('open');
var auth = require('./auth');
var config = require('./config').obtain();
var console = require('./log');
var ocapi = require('./ocapi');
const LOG_FILE_DIR = '/on/demandware.servlet/webdav/Sites/Impex/log/';
const JOB_EXECUTION_STATUS_POLL_TIMEOUT = 5000;
const JOB_EXECUTION_STATUS_POLL_ERROR_THRESHOLD = 5;
const JOB_EXECUTION_STATUS_CODE_RUNNING = 'RUNNING';
const JOB_EXECUTION_EXIT_STATUS_ERROR = 'error';
// enable request debugging
if ( process.env.DEBUG ) {
require('request-debug')(request);
}
function runJob(instance, job_id, request_doc, token, callback) {
var endpoint = '/s/-/dw/data/' + ocapi.getOcapiVersion() + '/jobs/{job_id}/executions'
endpoint = endpoint.replace('{job_id}', job_id);
// build the request options
var options = ocapi.getOptions(instance, endpoint, token);
// the post body
options['body'] = (request_doc ? request_doc : null);
// just do the request and pass the callback
request.post(options, callback);
}
function run(instance, job_id, request_doc, asJson) {
runJob(instance, job_id, request_doc, auth.getToken(), function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && ( res.statusCode == 200 || res.statusCode == 202 ) && !res.fault) {
var job_execution = res.body;
// handle options for job status output
if (asJson) {
console.json(job_execution);
return;
}
console.info('Job "%s" started on %s.', job_id, instance);
console.info('Job execution id: %s', job_execution.id);
console.info('Check the status of this job execution by running:');
console.info();
console.info(' sfcc-ci job:status "%s" "%s" -i %s', job_id, job_execution.id, instance);
console.info();
return;
}
// in case of errors
var result = { error : util.format('Starting job "%s" on %s failed',
job_id, instance), fault : res.body.fault };
// handle case of import job with res.body.fault.type === 'UnknownPropertyException'
// this must be a support user, so we need to re-run with normal job parameters form
if (job_id === 'sfcc-site-archive-import' && request_doc.file_name
&& res.body.fault && res.body.fault.type === 'UnknownPropertyException') {
if (!asJson) {
console.warn("internal users must use different form for import; re-running job", request_doc)
}
return run(instance, job_id, {
parameters: [
{
name: "ImportFile",
value: request_doc.file_name
}
]
}, asJson);
}
// TODO handle export as well
if (asJson) {
console.json(result);
} else {
console.error(result['error']);
console.debug(result['fault']);
}
}, function() {
run(instance, job_id, request_doc, asJson);
});
});
}
function runSync(instance, job_id, request_doc, asJson, failFast) {
runJob(instance, job_id, request_doc, auth.getToken(), function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && ( res.statusCode == 200 || res.statusCode == 202 ) && !res.fault) {
var job_execution = res.body;
if (!asJson) {
console.info('Job "%s" started on %s. Execution id is: %s',
job_id, instance, job_execution.id);
console.info('Waiting for job to finish...');
}
// no failure
var running = true;
var result = {};
// error threshold
var errorThreshold = JOB_EXECUTION_STATUS_POLL_ERROR_THRESHOLD;
var timeout = setInterval(function() {
getStatus(instance, job_id, job_execution.id, auth.getToken(), function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && res.statusCode == 200 && !res.fault) {
// update the execution details
job_execution = res.body;
if (job_execution.status !== JOB_EXECUTION_STATUS_CODE_RUNNING ) {
running = false;
}
return;
} else if ( err && res.statusCode > 200 && errorThreshold > 0) {
// decrease the error threshold
errorThreshold--;
// don't set an error and allow the polling to continue
console.debug('Polling job status failed. Polling error threshold not reached.');
} else if ( err && res.statusCode > 200 && errorThreshold === 0) {
// report a reached error threshold during polling
result['error'] = util.format('Polling job status "%s" on %s failed. Error ' +
'threshold reached. Stop polling, the job may still run.', job_id, instance);
} else {
// in case of errors, mark failure
result['fault'] = res.body.fault;
result['error'] = util.format('Running job "%s" on %s failed', job_id, instance);
}
});
});
if (!running || result['error']) {
clearInterval(timeout);
if (result['error']) {
if (asJson) {
console.json(result);
return;
}
console.error(result['error']);
console.debug(result['fault']);
} else {
if (asJson) {
console.json(job_execution);
return;
}
console.info('Job "%s" finished. Status is: %s (%s)',
job_id, job_execution.execution_status, job_execution.status);
console.info('');
console.info('Execution details:');
console.info('');
console.prettyPrint(job_execution);
console.info('');
console.info('Log file:');
console.info('');
if (job_execution['is_log_file_existing']) {
console.info(' https://' + instance + LOG_FILE_DIR + job_execution['log_file_name']);
} else {
console.info(' No log file available');
}
// report a failing job gracefully with fail-fast
if (failFast
&& typeof job_execution.exit_status === 'object'
&& job_execution.exit_status !== null
&& job_execution.exit_status.status === JOB_EXECUTION_EXIT_STATUS_ERROR ) {
console.error("Job ended with error. You may check the log files for further details.");
}
}
}
}, JOB_EXECUTION_STATUS_POLL_TIMEOUT);
return;
}
// in case of errors
var result = { error : util.format('Starting job "%s" on %s failed',
job_id, instance), fault : res.body.fault };
// handle case of import job with res.body.fault.type === 'UnknownPropertyException'
// this must be a support user, so we need to re-run with normal job parameters form
if (job_id === 'sfcc-site-archive-import' && request_doc.file_name
&& res.body.fault && res.body.fault.type === 'UnknownPropertyException') {
if (!asJson) {
console.warn("internal users must use different form for import; re-running job", request_doc)
}
return runSync(instance, job_id, {
parameters: [
{
name: "ImportFile",
value: request_doc.file_name
}
]
}, asJson);
}
if (asJson) {
console.json(result);
} else {
console.error(result['error']);
console.debug(result['fault']);
}
}, function() {
runSync(instance, job_id, request_doc, asJson, failFast);
});
});
}
function getStatus(instance, job_id, job_execution_id, token, callback) {
var endpoint = '/s/-/dw/data/' + ocapi.getOcapiVersion() + '/jobs/{job_id}/executions/{job_execution_id}'
endpoint = endpoint.replace('{job_id}', job_id).replace('{job_execution_id}', job_execution_id);
// build the request options
var options = ocapi.getOptions(instance, endpoint, token);
// just do the request and pass the callback
request.get(options, callback);
}
function status(instance, job_id, job_execution_id, verbose, streamLog, openLogFile, asJson) {
getStatus(instance, job_id, job_execution_id, auth.getToken(), function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && res.statusCode == 200 && !res.fault) {
var job_execution = res.body;
// handle options for job status output
if (asJson) {
console.json(job_execution);
return;
} else if (streamLog) {
console.info('Stream log...');
// ensure log is available
if (job_execution['is_log_file_existing']) {
// make request to log file
var logUrl = 'https://' + instance + LOG_FILE_DIR + job_execution['log_file_name'];
requestLogFile(instance, job_execution['log_file_name'], auth.getToken()).pipe(process.stdout);
} else {
console.info('No log available');
}
return;
}
console.info('Status of job execution %s (%s): %s (%s)',
job_execution_id, job_id, job_execution.execution_status, job_execution.status);
if (verbose) {
// verbose log output
console.info('');
console.info('Execution details:');
console.info('');
console.prettyPrint(job_execution);
console.info('');
console.info('Log file:');
console.info('');
if (job_execution['is_log_file_existing']) {
console.info(' https://' + instance + LOG_FILE_DIR + job_execution['log_file_name']);
} else {
console.info(' No log file available');
}
} else if (openLogFile) {
// open log file in browser
var logFileUrl = 'https://' + instance + LOG_FILE_DIR + job_execution['log_file_name'];
console.info('');
console.info('Opening browser to log file...');
opn(logFileUrl);
}
console.info('');
return;
}
// in case of errors
var result = { error : util.format('Getting job execution "%s" for job "%s" on %s failed',
job_execution_id, job_id, instance), fault : res.body.fault };
if (asJson) {
console.json(result);
} else {
console.error(result['error']);
console.debug(result['fault']);
}
}, function() {
status(instance, job_id, job_execution_id, verbose, streamLog, openLogFile, asJson);
});
});
}
function buildParameters(job_params) {
var params = [];
if (job_params) {
job_params.forEach(function (param) {
var split = param.split('=');
params.push({
name: split[0],
value: (split.length > 1 ? split[1] : null)
});
});
}
return params;
}
function requestLogFile(instance, logfile, token) {
var endpoint = LOG_FILE_DIR + logfile
// build the request options
var options = ocapi.getOptions(instance, endpoint, token);
console.debug(options);
// just do the request and return
return request.get(options);
}
module.exports.run = run;
module.exports.runSync = runSync;
module.exports.runJob = runJob;
module.exports.buildParameters = buildParameters;
module.exports.status = status;
module.exports.api = {
/**
* Starts a job execution on a Commerce Cloud instance. The job is triggered and the result of the attempt to
* start the job is returned. You may use the API function status to get the current job execution status.
*
* @param {String} instance Instance to start the job on
* @param {String} job_id The job to start
* @param {String} token The Oauth token to use for authentication
* @param {Array} job_params Array containing job parameters. A job parameter must be denoted by an object holding a key and a value property.
* @param {Function} callback Callback function executed as a result. The error and the job execution details will be passed as parameters to the callback function.
*/
run : function (instance, job_id, job_params, token, callback) {
// check parameters
if (typeof(instance) !== 'string') {
throw new TypeError('Parameter instance missing or not of type String');
}
if (typeof(job_id) !== 'string') {
throw new TypeError('Parameter job_id missing or not of type String');
}
if (typeof(job_params) !== 'object') {
throw new TypeError('Parameter job_params must either be null or not of type Object');
}
if (typeof(token) !== 'string') {
throw new TypeError('Parameter token missing or not of type String');
}
if (typeof(callback) !== 'function') {
throw new TypeError('Parameter callback missing or not of type Function');
}
runJob(instance, job_id, job_params, token, function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && ( res.statusCode == 200 || res.statusCode == 202 ) && !res.fault) {
// if successful, callback with execution details
callback(err, res);
return;
}
// in case of errors, callback with err
callback(err, res);
return;
});
});
},
/**
* Get the status of a job execution on a Commerce Cloud instance.
*
* @param {String} instance Instance the job was executed on.
* @param {String} job_id The job to get the execution status for
* @param {String} job_execution_id The job execution id to get the status for
* @param {String} token The Oauth token to use for authentication
* @param {Function} callback Callback function executed as a result. The error and the job execution details will be passed as parameters to the callback function.
*/
status : function (instance, job_id, job_execution_id, token, callback) {
// check parameters
if (typeof(instance) !== 'string') {
throw new TypeError('Parameter instance missing or not of type String');
}
if (typeof(job_id) !== 'string') {
throw new TypeError('Parameter job_id missing or not of type String');
}
if (typeof(job_execution_id) !== 'string') {
throw new TypeError('Parameter job_execution_id missing or not of type String');
}
if (typeof(token) !== 'string') {
throw new TypeError('Parameter token missing or not of type String');
}
if (typeof(callback) !== 'function') {
throw new TypeError('Parameter callback missing or not of type Function');
}
getStatus(instance, job_id, job_execution_id, token, function (err, res) {
ocapi.ensureValidToken(err, res, function(err, res) {
if (!err && res.statusCode == 200 && !res.fault) {
// Success: Callback with execution details
callback(undefined, res.body);
return;
}
// Handle Errors
if (res.text) {
callback(err, JSON.parse(res.text));
} else {
callback(err, undefined);
}
return;
});
});
}
};