66 catchNotFoundOrThrow,
77} = require ( '../utils' ) ;
88
9+ /**
10+ * We need to supply some number for the API,
11+ * because it would not accept "Infinity".
12+ * 999999 seconds is more than 10 days.
13+ * @type {number }
14+ */
915const MAX_WAIT_FOR_FINISH = 999999 ;
1016
1117/**
@@ -74,20 +80,24 @@ class ResourceClient extends ApiClient {
7480 * @private
7581 */
7682 async _waitForFinish ( options = { } ) {
77- const { waitSecs } = options ;
83+ const {
84+ waitSecs = MAX_WAIT_FOR_FINISH ,
85+ } = options ;
86+ const waitMillis = waitSecs * 1000 ;
7887 let job ;
7988
8089 const startedAt = Date . now ( ) ;
8190 const shouldRepeat = ( ) => {
82- if ( waitSecs && ( Date . now ( ) - startedAt ) / 1000 >= waitSecs ) return false ;
83- if ( job && ACT_JOB_TERMINAL_STATUSES . includes ( job . status ) ) return false ;
84- return true ;
91+ const millisSinceStart = Date . now ( ) - startedAt ;
92+ if ( millisSinceStart >= waitMillis ) return false ;
93+ const hasJobEnded = job && ACT_JOB_TERMINAL_STATUSES . includes ( job . status ) ;
94+ return ! hasJobEnded ;
8595 } ;
8696
87- while ( shouldRepeat ( ) ) {
88- const waitForFinish = waitSecs === undefined
89- ? MAX_WAIT_FOR_FINISH
90- : Math . round ( waitSecs - ( ( Date . now ( ) - startedAt ) / 1000 ) ) ;
97+ do {
98+ const millisSinceStart = Date . now ( ) - startedAt ;
99+ const remainingWaitSeconds = Math . round ( ( waitMillis - millisSinceStart ) / 1000 ) ;
100+ const waitForFinish = Math . max ( 0 , remainingWaitSeconds ) ;
91101
92102 const requestOpts = {
93103 url : this . _url ( ) ,
@@ -104,7 +114,7 @@ class ResourceClient extends ApiClient {
104114 // It might take some time for database replicas to get up-to-date,
105115 // so getRun() might return null. Wait a little bit and try it again.
106116 if ( ! job ) await new Promise ( ( resolve ) => setTimeout ( resolve , 250 ) ) ;
107- }
117+ } while ( ( shouldRepeat ( ) ) ) ;
108118
109119 if ( ! job ) {
110120 const jobName = this . constructor . name . match ( / ( \w + ) C l i e n t / ) [ 1 ] . toLowerCase ( ) ;
0 commit comments