Skip to content

Commit b165d6d

Browse files
committed
Merge branch 'master' of https://github.com/iterative/cml into runner-faster-termination
2 parents 40dcbc9 + d6c382a commit b165d6d

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

bin/cml/runner.js

+42-32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let RUNNER;
1414
let RUNNER_ID;
1515
let RUNNER_JOBS_RUNNING = [];
1616
let RUNNER_SHUTTING_DOWN = false;
17+
let RUNNER_TIMER = 0;
1718
const GH_5_MIN_TIMEOUT = (72 * 60 - 5) * 60 * 1000;
1819

1920
const shutdown = async (opts) => {
@@ -236,6 +237,7 @@ const runLocal = async (opts) => {
236237
RUNNER_JOBS_RUNNING = RUNNER_JOBS_RUNNING.filter(
237238
(job) => job.id !== jobId
238239
);
240+
RUNNER_TIMER = 0;
239241

240242
if (single && cml.driver === 'bitbucket') {
241243
await shutdown({ ...opts, reason: 'single job' });
@@ -264,47 +266,55 @@ const runLocal = async (opts) => {
264266
RUNNER = proc;
265267
({ id: RUNNER_ID } = await cml.runnerByName({ name }));
266268

267-
if (parseInt(idleTimeout) > 0) {
269+
if (idleTimeout > 0) {
268270
const watcher = setInterval(async () => {
269271
let idle = RUNNER_JOBS_RUNNING.length === 0;
270272

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+
}
285299
}
300+
} catch (err) {
301+
winston.error(
302+
`Error connecting the SCM: ${err.message}. Will try again in ${idleTimeout} secs`
303+
);
286304

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;
294306
}
295-
} catch (err) {
296-
winston.error(
297-
`Error connecting the SCM: ${err.message}. Will try again in ${idleTimeout} secs`
298-
);
299307

300-
idle = false;
308+
if (idle) {
309+
shutdown({ ...opts, reason: `timeout:${idleTimeout}` });
310+
clearInterval(watcher);
311+
} else {
312+
RUNNER_TIMER = 0;
313+
}
301314
}
302315

303-
if (idle) {
304-
shutdown({ ...opts, reason: `timeout:${idleTimeout}` });
305-
clearInterval(watcher);
306-
}
307-
}, idleTimeout * 1000);
316+
RUNNER_TIMER++;
317+
}, 1000);
308318
}
309319

310320
if (!noRetry) {

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dvcorg/cml",
3-
"version": "0.15.2",
3+
"version": "0.16.0",
44
"description": "<p align=\"center\"> <img src=\"https://static.iterative.ai/img/cml/title_strip_trim.png\" width=400> </p>",
55
"author": {
66
"name": "Iterative Inc",

0 commit comments

Comments
 (0)