-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrun.ts
More file actions
256 lines (229 loc) · 8.02 KB
/
run.ts
File metadata and controls
256 lines (229 loc) · 8.02 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
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {Args, Flags} from '@oclif/core';
import {JobCommand, type B2COperationContext} from '@salesforce/b2c-tooling-sdk/cli';
import {
executeJob,
waitForJob,
JobExecutionError,
type JobExecution,
} from '@salesforce/b2c-tooling-sdk/operations/jobs';
import {t} from '../../i18n/index.js';
export default class JobRun extends JobCommand<typeof JobRun> {
static args = {
jobId: Args.string({
description: 'Job ID to execute',
required: true,
}),
};
static description = t('commands.job.run.description', 'Execute a job on a B2C Commerce instance');
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> my-custom-job',
'<%= config.bin %> <%= command.id %> my-custom-job --wait',
String.raw`<%= config.bin %> <%= command.id %> my-custom-job -P "SiteScope={\"all_storefront_sites\":true}" -P OtherParam=value`,
'<%= config.bin %> <%= command.id %> my-custom-job --wait --timeout 600',
String.raw`<%= config.bin %> <%= command.id %> sfcc-search-index-product-full-update --body '{"site_scope":{"named_sites":["RefArch"]}}'`,
];
static flags = {
...JobCommand.baseFlags,
wait: Flags.boolean({
char: 'w',
description: 'Wait for job to complete',
default: false,
}),
timeout: Flags.integer({
char: 't',
description: 'Timeout in seconds when waiting (default: no timeout)',
}),
param: Flags.string({
char: 'P',
description: 'Job parameter in format "name=value" (use -P multiple times for multiple params)',
multiple: true,
multipleNonGreedy: true,
exclusive: ['body'],
}),
body: Flags.string({
char: 'B',
description: 'Raw JSON request body (for system jobs with non-standard schemas)',
exclusive: ['param'],
}),
'no-wait-running': Flags.boolean({
description: 'Do not wait for running job to finish before starting',
default: false,
}),
'show-log': Flags.boolean({
description: 'Show job log on failure',
default: true,
}),
};
protected operations = {
executeJob,
waitForJob,
};
async run(): Promise<JobExecution> {
this.requireOAuthCredentials();
const {jobId} = this.args;
const {wait, timeout, param, body, 'no-wait-running': noWaitRunning, 'show-log': showLog} = this.flags;
// Parse parameters or body
const parameters = this.parseParameters(param || []);
const rawBody = body ? this.parseBody(body) : undefined;
// Create lifecycle context
const context = this.createContext('job:run', {
jobId,
parameters: rawBody ? undefined : parameters,
body: rawBody,
wait,
hostname: this.resolvedConfig.values.hostname,
});
// Run beforeOperation hooks - check for skip
const beforeResult = await this.runBeforeHooks(context);
if (beforeResult.skip) {
this.log(
t('commands.job.run.skipped', 'Job execution skipped: {{reason}}', {
reason: beforeResult.skipReason || 'skipped by plugin',
}),
);
// Return a mock execution for JSON output
return {execution_status: 'finished', exit_status: {code: 'skipped'}} as unknown as JobExecution;
}
this.log(
t('commands.job.run.executing', 'Executing job {{jobId}} on {{hostname}}...', {
jobId,
hostname: this.resolvedConfig.values.hostname!,
}),
);
let execution: JobExecution;
try {
execution = await this.operations.executeJob(this.instance, jobId, {
parameters: rawBody ? undefined : parameters,
body: rawBody,
waitForRunning: !noWaitRunning,
});
} catch (error) {
this.handleExecutionError(error, context);
}
this.log(
t('commands.job.run.started', 'Job started: {{executionId}} (status: {{status}})', {
executionId: execution.id,
status: execution.execution_status,
}),
);
// Wait for completion if requested
if (wait) {
execution = await this.waitForJobCompletion({
jobId,
executionId: execution.id!,
timeout,
showLog,
context,
});
} else {
// Not waiting - run afterOperation hooks with current state
await this.runAfterHooks(context, {
success: true,
duration: Date.now() - context.startTime,
data: execution,
});
}
return execution;
}
private handleExecutionError(error: unknown, context: B2COperationContext): never {
// Run afterOperation hooks with failure (fire-and-forget, errors ignored)
this.runAfterHooks(context, {
success: false,
error: error instanceof Error ? error : new Error(String(error)),
duration: Date.now() - context.startTime,
}).catch(() => {});
if (error instanceof Error) {
this.error(t('commands.job.run.executionFailed', 'Failed to execute job: {{message}}', {message: error.message}));
}
throw error;
}
private async handleWaitError(error: unknown, showLog: boolean, context: B2COperationContext): Promise<never> {
// Run afterOperation hooks with failure
await this.runAfterHooks(context, {
success: false,
error: error instanceof Error ? error : new Error(String(error)),
duration: Date.now() - context.startTime,
data: error instanceof JobExecutionError ? error.execution : undefined,
});
if (error instanceof JobExecutionError) {
if (showLog) {
await this.showJobLog(error.execution);
}
this.error(
t('commands.job.run.jobFailed', 'Job failed: {{status}}', {
status: error.execution.exit_status?.code || 'ERROR',
}),
);
}
throw error;
}
private parseBody(body: string): Record<string, unknown> {
try {
return JSON.parse(body) as Record<string, unknown>;
} catch {
this.error(t('commands.job.run.invalidBody', 'Invalid JSON body: {{body}}', {body}));
}
}
private parseParameters(params: string[]): Array<{name: string; value: string}> {
return params.map((p) => {
const eqIndex = p.indexOf('=');
if (eqIndex === -1) {
this.error(
t('commands.job.run.invalidParam', 'Invalid parameter format: {{param}}. Expected "name=value"', {param: p}),
);
}
return {
name: p.slice(0, eqIndex),
value: p.slice(eqIndex + 1),
};
});
}
private async waitForJobCompletion(options: {
jobId: string;
executionId: string;
timeout: number | undefined;
showLog: boolean;
context: B2COperationContext;
}): Promise<JobExecution> {
const {jobId, executionId, timeout, showLog, context} = options;
this.log(t('commands.job.run.waiting', 'Waiting for job to complete...'));
try {
const execution = await this.operations.waitForJob(this.instance, jobId, executionId, {
timeout: timeout ? timeout * 1000 : undefined,
onProgress: (exec, elapsed) => {
if (!this.jsonEnabled()) {
const elapsedSec = Math.floor(elapsed / 1000);
this.log(
t('commands.job.run.progress', ' Status: {{status}} ({{elapsed}}s elapsed)', {
status: exec.execution_status,
elapsed: elapsedSec.toString(),
}),
);
}
},
});
const durationSec = execution.duration ? (execution.duration / 1000).toFixed(1) : 'N/A';
this.log(
t('commands.job.run.completed', 'Job completed: {{status}} (duration: {{duration}}s)', {
status: execution.exit_status?.code || execution.execution_status,
duration: durationSec,
}),
);
// Run afterOperation hooks with success
await this.runAfterHooks(context, {
success: true,
duration: Date.now() - context.startTime,
data: execution,
});
return execution;
} catch (error) {
return this.handleWaitError(error, showLog, context);
}
}
}