Skip to content

Commit

Permalink
Chore: timeout properly
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 30, 2023
1 parent 48b3c45 commit 6ed3695
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
93 changes: 45 additions & 48 deletions Build/build-speedtest-domainset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, Promise<void>>>((pMap, keyword) => {
pMap[keyword] = querySpeedtestApi(keyword).then(hostnameGroup => {
hostnameGroup.forEach(hostname => {
if (hostname) {
domains.add(hostname);
}
await new Promise<void>((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<Record<string, Promise<void>>>((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);
Expand Down
3 changes: 3 additions & 0 deletions Build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down

0 comments on commit 6ed3695

Please sign in to comment.