@@ -3,11 +3,14 @@ import * as path from 'path';
3
3
import * as fs from 'fs' ;
4
4
import * as envPaths from 'env-paths' ;
5
5
import { getStandalone , generateCACertificate } from 'mockttp' ;
6
+ import { Mutex } from 'async-mutex' ;
6
7
7
8
import updateCommand from '@oclif/plugin-update/lib/commands/update' ;
8
9
9
10
import { HttpToolkitServer } from './httptoolkit-server' ;
10
11
import { checkBrowserConfig } from './browsers' ;
12
+ import { reportError } from './error-tracking' ;
13
+ import { delay } from './util' ;
11
14
12
15
const canAccess = util . promisify ( fs . access ) ;
13
16
const mkDir = util . promisify ( fs . mkdir ) ;
@@ -68,8 +71,26 @@ export async function runHTK(options: {
68
71
configPath,
69
72
https : httpsConfig
70
73
} ) ;
74
+
75
+ const updateMutex = new Mutex ( ) ;
71
76
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
+ ) ;
73
94
} ) ;
74
95
75
96
await htkServer . start ( ) ;
0 commit comments