From 6ed3695e36f8fce4a6c4ac22482ceecdd8d77001 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 31 Dec 2023 00:14:36 +0800 Subject: [PATCH] Chore: timeout properly --- Build/build-speedtest-domainset.ts | 93 +++++++++++++++--------------- Build/index.ts | 3 + 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/Build/build-speedtest-domainset.ts b/Build/build-speedtest-domainset.ts index 2b72a945..77db7b0a 100644 --- a/Build/build-speedtest-domainset.ts +++ b/Build/build-speedtest-domainset.ts @@ -141,65 +141,62 @@ export const buildSpeedtestDomainSet = task(import.meta.path, async () => { } } - let timer; - - const pMap = ([ - 'Hong Kong', - 'Taiwan', - 'China Telecom', - 'China Mobile', - 'China Unicom', - 'Japan', - 'Tokyo', - 'Singapore', - 'Korea', - 'Canada', - 'Toronto', - 'Montreal', - 'Los Ang', - 'San Jos', - 'Seattle', - 'New York', - 'Dallas', - 'Miami', - 'Berlin', - 'Frankfurt', - 'London', - 'Paris', - 'Amsterdam', - 'Moscow', - 'Australia', - 'Sydney', - 'Brazil', - 'Turkey' - ]).reduce>>((pMap, keyword) => { - pMap[keyword] = querySpeedtestApi(keyword).then(hostnameGroup => { - hostnameGroup.forEach(hostname => { - if (hostname) { - domains.add(hostname); - } + await new Promise((resolve) => { + const pMap = ([ + 'Hong Kong', + 'Taiwan', + 'China Telecom', + 'China Mobile', + 'China Unicom', + 'Japan', + 'Tokyo', + 'Singapore', + 'Korea', + 'Canada', + 'Toronto', + 'Montreal', + 'Los Ang', + 'San Jos', + 'Seattle', + 'New York', + 'Dallas', + 'Miami', + 'Berlin', + 'Frankfurt', + 'London', + 'Paris', + 'Amsterdam', + 'Moscow', + 'Australia', + 'Sydney', + 'Brazil', + 'Turkey' + ]).reduce>>((pMap, keyword) => { + pMap[keyword] = querySpeedtestApi(keyword).then(hostnameGroup => { + hostnameGroup.forEach(hostname => { + if (hostname) { + domains.add(hostname); + } + }); }); - }); - return pMap; - }, {}); + return pMap; + }, {}); - try { - timer = setTimeout(() => { + const timer = setTimeout(() => { console.error(picocolors.red('Task timeout!')); Object.entries(pMap).forEach(([name, p]) => { console.log(`[${name}]`, Bun.peek.status(p)); }); - throw new Error('timeout'); + resolve(); }, 1000 * 60 * 2); - await Promise.all(Object.values(pMap)); - } finally { - if (timer) { + Promise.all(Object.values(pMap)).then(() => { clearTimeout(timer); - } - } + resolve(); + }); + }); const gorhill = await getGorhillPublicSuffixPromise(); const deduped = sortDomains(domainDeduper(Array.from(domains)), gorhill); diff --git a/Build/index.ts b/Build/index.ts index 248713e6..229c4e38 100644 --- a/Build/index.ts +++ b/Build/index.ts @@ -102,6 +102,9 @@ import type { TaskResult } from './lib/trace-runner'; ]); printStats(stats); + + // Finish the build to avoid leaking timer/fetch ref + process.exit(0); } catch (e) { console.trace(e); console.error('Something went wrong!');