From fbc16bd91230c067b4f4eb94b1f9b51159565b98 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 25 May 2022 18:29:49 +0200 Subject: [PATCH 1/4] Runner faster termination --- bin/cml/runner.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/bin/cml/runner.js b/bin/cml/runner.js index bc1c05407..1ac489dbd 100755 --- a/bin/cml/runner.js +++ b/bin/cml/runner.js @@ -21,14 +21,7 @@ const shutdown = async (opts) => { RUNNER_SHUTTING_DOWN = true; const { error, cloud } = opts; - const { - name, - workdir = '', - tfResource, - noRetry, - reason, - destroyDelay - } = opts; + const { name, workdir = '', tfResource, noRetry, reason } = opts; const tfPath = workdir; const unregisterRunner = async () => { @@ -36,8 +29,8 @@ const shutdown = async (opts) => { try { winston.info(`Unregistering runner ${name}...`); - RUNNER && RUNNER.kill('SIGINT'); await cml.unregisterRunner({ name }); + RUNNER && RUNNER.kill('SIGINT'); winston.info('\tSuccess'); } catch (err) { winston.error(`\tFailed: ${err.message}`); @@ -81,9 +74,6 @@ const shutdown = async (opts) => { winston.info('runner status', { reason, status: 'terminated' }); } - winston.info(`waiting ${destroyDelay} seconds before exiting...`); - await sleep(destroyDelay); - if (!cloud) { try { await unregisterRunner(); @@ -413,6 +403,7 @@ exports.command = 'runner'; exports.description = 'Launch and register a self-hosted runner'; exports.handler = async (opts) => { + const { destroyDelay } = opts; if (process.env.RUNNER_NAME) { winston.warn( 'ignoring RUNNER_NAME environment variable, use CML_RUNNER_NAME or --name instead' @@ -421,6 +412,8 @@ exports.handler = async (opts) => { try { await run(opts); } catch (error) { + winston.info(`waiting ${destroyDelay} seconds before exiting...`); + await sleep(destroyDelay); await shutdown({ ...opts, error }); throw error; } From 216cd335f7aa09767afe9a1d76d91a2b05bb20e5 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Thu, 26 May 2022 14:51:17 +0200 Subject: [PATCH 2/4] delayed destroy --- bin/cml/runner.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bin/cml/runner.js b/bin/cml/runner.js index 1ac489dbd..95f55a246 100755 --- a/bin/cml/runner.js +++ b/bin/cml/runner.js @@ -21,7 +21,15 @@ const shutdown = async (opts) => { RUNNER_SHUTTING_DOWN = true; const { error, cloud } = opts; - const { name, workdir = '', tfResource, noRetry, reason } = opts; + const { + name, + workdir = '', + tfResource, + noRetry, + reason, + destroyDelay, + destroyDelayed + } = opts; const tfPath = workdir; const unregisterRunner = async () => { @@ -61,6 +69,11 @@ const shutdown = async (opts) => { const destroyTerraform = async () => { if (!tfResource) return; + if (destroyDelayed) { + winston.info(`Waiting ${destroyDelay} seconds to destroy`); + await sleep(destroyDelay); + } + try { winston.debug(await tf.destroy({ dir: tfPath })); } catch (err) { @@ -403,7 +416,6 @@ exports.command = 'runner'; exports.description = 'Launch and register a self-hosted runner'; exports.handler = async (opts) => { - const { destroyDelay } = opts; if (process.env.RUNNER_NAME) { winston.warn( 'ignoring RUNNER_NAME environment variable, use CML_RUNNER_NAME or --name instead' @@ -412,10 +424,7 @@ exports.handler = async (opts) => { try { await run(opts); } catch (error) { - winston.info(`waiting ${destroyDelay} seconds before exiting...`); - await sleep(destroyDelay); - await shutdown({ ...opts, error }); - throw error; + await shutdown({ ...opts, error, destroyDelayed: true }); } }; From 40dcbc9a35dceca0009c5e6ed85d083c7911b363 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Sun, 29 May 2022 11:45:50 +0200 Subject: [PATCH 3/4] single check --- bin/cml/runner.js | 18 +++++++++++------- src/cml.js | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/cml/runner.js b/bin/cml/runner.js index 95f55a246..9f9fbca40 100755 --- a/bin/cml/runner.js +++ b/bin/cml/runner.js @@ -28,7 +28,7 @@ const shutdown = async (opts) => { noRetry, reason, destroyDelay, - destroyDelayed + single } = opts; const tfPath = workdir; @@ -41,7 +41,13 @@ const shutdown = async (opts) => { RUNNER && RUNNER.kill('SIGINT'); winston.info('\tSuccess'); } catch (err) { - winston.error(`\tFailed: ${err.message}`); + if (single) { + winston.warn( + `\tFailed: Runner in --single mode, might have cleared itself.` + ); + } else { + winston.error(`\tFailed: ${err.message}`); + } } }; @@ -69,10 +75,8 @@ const shutdown = async (opts) => { const destroyTerraform = async () => { if (!tfResource) return; - if (destroyDelayed) { - winston.info(`Waiting ${destroyDelay} seconds to destroy`); - await sleep(destroyDelay); - } + winston.info(`Waiting ${destroyDelay} seconds to destroy`); + await sleep(destroyDelay); try { winston.debug(await tf.destroy({ dir: tfPath })); @@ -424,7 +428,7 @@ exports.handler = async (opts) => { try { await run(opts); } catch (error) { - await shutdown({ ...opts, error, destroyDelayed: true }); + await shutdown({ ...opts, error }); } }; diff --git a/src/cml.js b/src/cml.js index 060900622..af04108c5 100755 --- a/src/cml.js +++ b/src/cml.js @@ -339,7 +339,9 @@ class CML { } async unregisterRunner(opts = {}) { - const { id: runnerId } = await this.runnerByName(opts); + const { id: runnerId } = (await this.runnerByName(opts)) || {}; + if (!runnerId) throw new Error(`Runner with id ${runnerId} not found`); + return await getDriver(this).unregisterRunner({ runnerId, ...opts }); } From 6afa2deb4ed091c5fc3d8a684104e9e74ab59143 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Sun, 29 May 2022 15:44:25 +0200 Subject: [PATCH 4/4] Remove unnecessary --- bin/cml/runner.js | 11 ++--------- src/cml.js | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/bin/cml/runner.js b/bin/cml/runner.js index 8cf1a3546..19458d66d 100755 --- a/bin/cml/runner.js +++ b/bin/cml/runner.js @@ -28,8 +28,7 @@ const shutdown = async (opts) => { tfResource, noRetry, reason, - destroyDelay, - single + destroyDelay } = opts; const tfPath = workdir; @@ -42,13 +41,7 @@ const shutdown = async (opts) => { RUNNER && RUNNER.kill('SIGINT'); winston.info('\tSuccess'); } catch (err) { - if (single) { - winston.warn( - `\tFailed: Runner in --single mode, might have cleared itself.` - ); - } else { - winston.error(`\tFailed: ${err.message}`); - } + winston.error(`\tFailed: ${err.message}`); } }; diff --git a/src/cml.js b/src/cml.js index af04108c5..76f6698f9 100755 --- a/src/cml.js +++ b/src/cml.js @@ -340,7 +340,7 @@ class CML { async unregisterRunner(opts = {}) { const { id: runnerId } = (await this.runnerByName(opts)) || {}; - if (!runnerId) throw new Error(`Runner with id ${runnerId} not found`); + if (!runnerId) throw new Error(`Runner not found`); return await getDriver(this).unregisterRunner({ runnerId, ...opts }); }