Skip to content

Commit 1dd9f3b

Browse files
committed
Improve update error handling & reliability
1 parent 7b4ca63 commit 1dd9f3b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import * as path from 'path';
33
import * as fs from 'fs';
44
import * as envPaths from 'env-paths';
55
import { getStandalone, generateCACertificate } from 'mockttp';
6+
import { Mutex } from 'async-mutex';
67

78
import updateCommand from '@oclif/plugin-update/lib/commands/update';
89

910
import { HttpToolkitServer } from './httptoolkit-server';
1011
import { checkBrowserConfig } from './browsers';
12+
import { reportError } from './error-tracking';
13+
import { delay } from './util';
1114

1215
const canAccess = util.promisify(fs.access);
1316
const mkDir = util.promisify(fs.mkdir);
@@ -68,8 +71,26 @@ export async function runHTK(options: {
6871
configPath,
6972
https: httpsConfig
7073
});
74+
75+
const updateMutex = new Mutex();
7176
htkServer.on('update-requested', () => {
72-
updateCommand.run(['stable']);
77+
updateMutex.runExclusive(() =>
78+
(<Promise<void>> updateCommand.run(['stable']))
79+
.catch((error) => {
80+
// Received successful update that wants to restart the server
81+
if (error.code === 'EEXIT') {
82+
// Block future update checks for one hour.
83+
84+
// If we don't, we'll redownload the same update again every check.
85+
// We don't want to block it completely though, in case this server
86+
// stays open for a very long time.
87+
return delay(1000 * 60 * 60);
88+
}
89+
90+
console.log(error);
91+
reportError('Failed to check for updates');
92+
})
93+
);
7394
});
7495

7596
await htkServer.start();

0 commit comments

Comments
 (0)