Fix: Correctly abort process on timeout (#162)

* Pass in abort controller signal

* Rearrange
This commit is contained in:
Marco Munizaga 2023-04-08 10:53:38 -07:00 committed by GitHub
parent f9863b901c
commit d342ac541e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import { ComposeSpecification, PropertiesServices } from "../compose-spec/compos
import { stringify } from 'yaml'; import { stringify } from 'yaml';
const exec = util.promisify(execStd); const exec = util.promisify(execStd);
const timeoutSecs = 3 * 60
export type RunOpts = { export type RunOpts = {
up: { up: {
@ -43,14 +44,10 @@ export async function run(namespace: string, compose: ComposeSpecification, opts
} }
try { try {
const timeoutSecs = 3 * 60 const controller = new AbortController();
let timeoutId const timeoutId = setTimeout(() => controller.abort(), 1000 * timeoutSecs)
const { stdout, stderr } = const { signal } = controller;
(await Promise.race([ const { stdout, stderr } = await exec(`docker compose -f ${path.join(dir, "compose.yaml")} up ${upFlags.join(" ")}`, { signal })
exec(`docker compose -f ${path.join(dir, "compose.yaml")} up ${upFlags.join(" ")}`),
// Timeout - uses any type because this will only reject the promise.
new Promise<any>((resolve, reject) => { timeoutId = setTimeout(() => reject("Timeout"), 1000 * timeoutSecs) })
]))
clearTimeout(timeoutId) clearTimeout(timeoutId)
const testResults = stdout.match(/.*dialer.*({.*)/) const testResults = stdout.match(/.*dialer.*({.*)/)
if (testResults === null || testResults.length < 2) { if (testResults === null || testResults.length < 2) {