@@ -14,6 +14,7 @@ let RUNNER;
14
14
let RUNNER_ID ;
15
15
let RUNNER_JOBS_RUNNING = [ ] ;
16
16
let RUNNER_SHUTTING_DOWN = false ;
17
+ let RUNNER_TIMER = 0 ;
17
18
const GH_5_MIN_TIMEOUT = ( 72 * 60 - 5 ) * 60 * 1000 ;
18
19
19
20
const shutdown = async ( opts ) => {
@@ -236,6 +237,7 @@ const runLocal = async (opts) => {
236
237
RUNNER_JOBS_RUNNING = RUNNER_JOBS_RUNNING . filter (
237
238
( job ) => job . id !== jobId
238
239
) ;
240
+ RUNNER_TIMER = 0 ;
239
241
240
242
if ( single && cml . driver === 'bitbucket' ) {
241
243
await shutdown ( { ...opts , reason : 'single job' } ) ;
@@ -264,47 +266,55 @@ const runLocal = async (opts) => {
264
266
RUNNER = proc ;
265
267
( { id : RUNNER_ID } = await cml . runnerByName ( { name } ) ) ;
266
268
267
- if ( parseInt ( idleTimeout ) > 0 ) {
269
+ if ( idleTimeout > 0 ) {
268
270
const watcher = setInterval ( async ( ) => {
269
271
let idle = RUNNER_JOBS_RUNNING . length === 0 ;
270
272
271
- try {
272
- if ( cml . driver === 'github' ) {
273
- const job = await cml . runnerJob ( { runnerId : RUNNER_ID } ) ;
274
-
275
- if ( ! job && ! idle ) {
276
- winston . error (
277
- `Runner is idle as per the GitHub API but busy as per CML internal state. Resetting jobs. Retrying in ${ idleTimeout } seconds...`
278
- ) ;
279
- winston . warn ( `CML GitHub driver response: ${ JSON . stringify ( job ) } ` ) ;
280
- winston . warn (
281
- `CML internal state: ${ JSON . stringify ( RUNNER_JOBS_RUNNING ) } `
282
- ) ;
283
-
284
- RUNNER_JOBS_RUNNING = [ ] ;
273
+ if ( RUNNER_TIMER >= idleTimeout ) {
274
+ try {
275
+ if ( cml . driver === 'github' ) {
276
+ const job = await cml . runnerJob ( { runnerId : RUNNER_ID } ) ;
277
+
278
+ if ( ! job && ! idle ) {
279
+ winston . error (
280
+ `Runner is idle as per the GitHub API but busy as per CML internal state. Resetting jobs. Retrying in ${ idleTimeout } seconds...`
281
+ ) ;
282
+ winston . warn (
283
+ `CML GitHub driver response: ${ JSON . stringify ( job ) } `
284
+ ) ;
285
+ winston . warn (
286
+ `CML internal state: ${ JSON . stringify ( RUNNER_JOBS_RUNNING ) } `
287
+ ) ;
288
+
289
+ RUNNER_JOBS_RUNNING = [ ] ;
290
+ }
291
+
292
+ if ( job && idle ) {
293
+ winston . error (
294
+ `Runner is busy as per the GitHub API but idle as per CML internal state. Retrying in ${ idleTimeout } seconds...`
295
+ ) ;
296
+
297
+ idle = false ;
298
+ }
285
299
}
300
+ } catch ( err ) {
301
+ winston . error (
302
+ `Error connecting the SCM: ${ err . message } . Will try again in ${ idleTimeout } secs`
303
+ ) ;
286
304
287
- if ( job && idle ) {
288
- winston . error (
289
- `Runner is busy as per the GitHub API but idle as per CML internal state. Retrying in ${ idleTimeout } seconds...`
290
- ) ;
291
-
292
- idle = false ;
293
- }
305
+ idle = false ;
294
306
}
295
- } catch ( err ) {
296
- winston . error (
297
- `Error connecting the SCM: ${ err . message } . Will try again in ${ idleTimeout } secs`
298
- ) ;
299
307
300
- idle = false ;
308
+ if ( idle ) {
309
+ shutdown ( { ...opts , reason : `timeout:${ idleTimeout } ` } ) ;
310
+ clearInterval ( watcher ) ;
311
+ } else {
312
+ RUNNER_TIMER = 0 ;
313
+ }
301
314
}
302
315
303
- if ( idle ) {
304
- shutdown ( { ...opts , reason : `timeout:${ idleTimeout } ` } ) ;
305
- clearInterval ( watcher ) ;
306
- }
307
- } , idleTimeout * 1000 ) ;
316
+ RUNNER_TIMER ++ ;
317
+ } , 1000 ) ;
308
318
}
309
319
310
320
if ( ! noRetry ) {
0 commit comments