66 */
77
88import { ReadStream } from 'node:fs' ;
9+ import { EOL } from 'node:os' ;
910import { Connection , Messages , SfError } from '@salesforce/core' ;
1011import { Ux } from '@salesforce/sf-plugins-core' ;
1112import type { Schema } from '@jsforce/jsforce-node' ;
@@ -31,7 +32,7 @@ const messages = Messages.loadMessages('@salesforce/plugin-data', 'batcher');
3132type BatchEntry = Record < string , string > ;
3233type Batches = BatchEntry [ ] [ ] ;
3334
34- type BulkResult = {
35+ export type BulkResult = {
3536 $ : {
3637 xmlns : string ;
3738 } ;
@@ -58,19 +59,12 @@ export class Batcher {
5859 * @param jobId {string}
5960 * @param doneCallback
6061 */
61- public async fetchAndDisplayJobStatus (
62- jobId : string ,
63- doneCallback ?: ( ...args : [ { job : JobInfo } ] ) => void
64- ) : Promise < JobInfo > {
62+ public async fetchAndDisplayJobStatus ( jobId : string ) : Promise < JobInfo > {
6563 const job = this . conn . bulk . job ( jobId ) ;
6664 const jobInfo = await job . check ( ) ;
6765
6866 this . bulkStatus ( jobInfo , undefined , undefined , true ) ;
6967
70- if ( doneCallback ) {
71- doneCallback ( { job : jobInfo } ) ;
72- }
73-
7468 return jobInfo ;
7569 }
7670
@@ -120,8 +114,7 @@ export class Batcher {
120114 records : ReadStream ,
121115 sobjectType : string ,
122116 wait ?: number
123- ) : Promise < BulkResult [ ] | JobInfo [ ] > {
124- const batchesCompleted = 0 ;
117+ ) : Promise < BulkResult [ ] | JobInfo [ ] | undefined > {
125118 let batchesQueued = 0 ;
126119 const overallInfo = false ;
127120
@@ -130,7 +123,10 @@ export class Batcher {
130123 // The error handling for this gets quite tricky when there are multiple batches
131124 // Currently, we bail out early by calling an Error.exit
132125 // But, we might want to actually continue to the next batch.
133- return ( await Promise . all (
126+ //
127+ // async: batches are created and return array of batch info.
128+ // sync: batches are created, it waits until they all finish and Promise.all resolves `undefined`.
129+ const batchInfos = ( await Promise . all (
134130 batches . map (
135131 async ( batch : Array < Record < string , string > > , i : number ) : Promise < BulkResult | BatchInfo | void | JobInfo > => {
136132 const newBatch = job . createBatch ( ) ;
@@ -186,14 +182,16 @@ export class Batcher {
186182 }
187183 ) ;
188184 } else {
189- resolve ( this . waitForCompletion ( newBatch , batchesCompleted , overallInfo , i + 1 , batches . length , wait ) ) ;
185+ resolve ( this . waitForCompletion ( newBatch , overallInfo , i + 1 , wait ) ) ;
190186 }
191187
192188 void newBatch . execute ( batch ) ;
193189 } ) ;
194190 }
195191 )
196- ) ) as BulkResult [ ] ;
192+ ) ) as BulkResult [ ] | undefined ;
193+
194+ return wait ? [ await this . fetchAndDisplayJobStatus ( job . id ! ) ] : batchInfos ;
197195 }
198196
199197 /**
@@ -229,12 +227,10 @@ export class Batcher {
229227 */
230228 private async waitForCompletion < J extends Schema , T extends BulkOperation > (
231229 newBatch : Batch < J , T > ,
232- batchesCompleted : number ,
233230 overallInfo : boolean ,
234231 batchNum : number ,
235- totalNumBatches : number ,
236232 waitMins : number
237- ) : Promise < JobInfo > {
233+ ) : Promise < void > {
238234 return new Promise ( ( resolve , reject ) => {
239235 void newBatch . on (
240236 'queue' ,
@@ -245,10 +241,10 @@ export class Batcher {
245241 if ( result . state === 'Failed' ) {
246242 reject ( result . stateMessage ) ;
247243 } else if ( ! overallInfo ) {
248- this . ux . log ( messages . getMessage ( 'PollingInfo' , [ POLL_FREQUENCY_MS / 1000 , batchInfo . jobId ] ) ) ;
244+ this . ux . log ( messages . getMessage ( 'PollingInfo' , [ POLL_FREQUENCY_MS / 1000 , batchInfo . jobId , batchInfo . id ] ) ) ;
249245 overallInfo = true ;
250246 }
251- this . ux . log ( messages . getMessage ( 'BatchQueued' , [ batchNum , batchInfo . id ] ) ) ;
247+ this . ux . log ( messages . getMessage ( 'BatchQueued' , [ batchNum , batchInfo . id ] ) , EOL ) ;
252248 newBatch . poll ( POLL_FREQUENCY_MS , waitMins * 60_000 ) ;
253249 }
254250 ) ;
@@ -257,10 +253,7 @@ export class Batcher {
257253 void newBatch . on ( 'response' , async ( results : BulkIngestBatchResult ) : Promise < void > => {
258254 const summary : BatchInfo = await newBatch . check ( ) ;
259255 this . bulkStatus ( summary , results , batchNum ) ;
260- batchesCompleted ++ ;
261- if ( batchesCompleted === totalNumBatches ) {
262- resolve ( await this . fetchAndDisplayJobStatus ( summary . jobId ) ) ;
263- }
256+ resolve ( ) ;
264257 } ) ;
265258 } ) ;
266259 }
0 commit comments