-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwait.ts
More file actions
106 lines (94 loc) · 3.21 KB
/
wait.ts
File metadata and controls
106 lines (94 loc) · 3.21 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
/*
* 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} from '@salesforce/b2c-tooling-sdk/cli';
import {waitForJob, JobExecutionError, type JobExecution} from '@salesforce/b2c-tooling-sdk/operations/jobs';
import {t} from '../../i18n/index.js';
export default class JobWait extends JobCommand<typeof JobWait> {
static args = {
jobId: Args.string({
description: 'Job ID',
required: true,
}),
executionId: Args.string({
description: 'Execution ID to wait for',
required: true,
}),
};
static description = t('commands.job.wait.description', 'Wait for a job execution to complete');
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> my-job abc123-def456',
'<%= config.bin %> <%= command.id %> my-job abc123-def456 --timeout 600',
'<%= config.bin %> <%= command.id %> my-job abc123-def456 --poll-interval 5',
];
static flags = {
...JobCommand.baseFlags,
timeout: Flags.integer({
char: 't',
description: 'Timeout in seconds (default: no timeout)',
}),
'poll-interval': Flags.integer({
description: 'Polling interval in seconds',
default: 3,
}),
'show-log': Flags.boolean({
description: 'Show job log on failure',
default: true,
}),
};
protected operations = {
waitForJob,
};
async run(): Promise<JobExecution> {
this.requireOAuthCredentials();
const {jobId, executionId} = this.args;
const {timeout, 'poll-interval': pollInterval, 'show-log': showLog} = this.flags;
this.log(
t('commands.job.wait.waiting', 'Waiting for job {{jobId}} execution {{executionId}}...', {
jobId,
executionId,
}),
);
try {
const execution = await this.operations.waitForJob(this.instance, jobId, executionId, {
timeout: timeout ? timeout * 1000 : undefined,
pollInterval: pollInterval * 1000,
onProgress: (exec, elapsed) => {
if (!this.jsonEnabled()) {
const elapsedSec = Math.floor(elapsed / 1000);
this.log(
t('commands.job.wait.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.wait.completed', 'Job completed: {{status}} (duration: {{duration}}s)', {
status: execution.exit_status?.code || execution.execution_status,
duration: durationSec,
}),
);
return execution;
} catch (error) {
if (error instanceof JobExecutionError) {
if (showLog) {
await this.showJobLog(error.execution);
}
this.error(
t('commands.job.wait.jobFailed', 'Job failed: {{status}}', {
status: error.execution.exit_status?.code || 'ERROR',
}),
);
}
throw error;
}
}
}