6
6
7
7
import { Event } from 'vscode' ;
8
8
import '../../../../common/extensions' ;
9
- import { createDeferred , sleep } from '../../../../common/utils/async' ;
9
+ import { createDeferred } from '../../../../common/utils/async' ;
10
10
import { logWarning } from '../../../../logging' ;
11
11
import { PythonEnvInfo } from '../../info' ;
12
12
import { getMinimalPartialInfo } from '../../info/env' ;
@@ -32,37 +32,12 @@ export interface IPythonEnvsCache {
32
32
flush ( ) : Promise < void > ;
33
33
}
34
34
35
- type CachingLocatorOptions = {
36
- refreshMinutes : number ,
37
- refreshRetryMinutes : number ,
38
- } ;
39
-
40
- // Set defaults and otherwise adjust values.
41
- function normalizeCachingLocatorOptions (
42
- opts : Partial < CachingLocatorOptions > ,
43
- defaults : CachingLocatorOptions = {
44
- refreshMinutes : 24 * 60 , // 1 day
45
- refreshRetryMinutes : 10 ,
46
- } ,
47
- ) : CachingLocatorOptions {
48
- const normalized = { ...opts } ;
49
- if ( normalized . refreshMinutes === undefined ) {
50
- normalized . refreshMinutes = defaults . refreshMinutes ;
51
- }
52
- if ( normalized . refreshRetryMinutes === undefined ) {
53
- normalized . refreshRetryMinutes = defaults . refreshRetryMinutes ;
54
- }
55
- return normalized as CachingLocatorOptions ;
56
- }
57
-
58
35
/**
59
36
* A locator that stores the known environments in the given cache.
60
37
*/
61
38
export class CachingLocator implements ILocator {
62
39
public readonly onChanged : Event < PythonEnvsChangedEvent > ;
63
40
64
- private readonly opts : CachingLocatorOptions ;
65
-
66
41
private readonly watcher = new PythonEnvsWatcher ( ) ;
67
42
68
43
private readonly initializing = createDeferred < void > ( ) ;
@@ -74,22 +49,11 @@ export class CachingLocator implements ILocator {
74
49
constructor (
75
50
private readonly cache : IPythonEnvsCache ,
76
51
private readonly locator : ILocator ,
77
- opts : {
78
- refreshMinutes ?: number ,
79
- refreshRetryMinutes ?: number ,
80
- } = { } ,
81
52
) {
82
53
this . onChanged = this . watcher . onChanged ;
83
- this . opts = normalizeCachingLocatorOptions ( opts ) ;
84
54
// eslint-disable-next-line @typescript-eslint/no-use-before-define
85
55
this . looper = new BackgroundLooper ( {
86
56
runDefault : ( ) => this . doRefresh ( ) ,
87
- retry : {
88
- intervalms : this . opts . refreshRetryMinutes * 60 * 1000 ,
89
- } ,
90
- periodic : {
91
- intervalms : this . opts . refreshMinutes * 60 * 1000 ,
92
- } ,
93
57
} ) ;
94
58
}
95
59
@@ -237,68 +201,9 @@ type RequestID = number;
237
201
type RunFunc = ( ) => Promise < void > ;
238
202
type NotifyFunc = ( ) => void ;
239
203
240
- type RetryOptions = {
241
- maxRetries : number ;
242
- intervalms : number ;
243
- } ;
244
-
245
- // Set defaults and otherwise adjust values.
246
- function normalizeRetryOptions (
247
- opts : Partial < RetryOptions > | undefined ,
248
- defaults : RetryOptions = {
249
- maxRetries : 3 ,
250
- intervalms : 100 ,
251
- } ,
252
- ) : RetryOptions | undefined {
253
- if ( opts === undefined ) {
254
- return undefined ;
255
- }
256
- const normalized = { ...opts } ;
257
- if ( normalized . maxRetries === undefined ) {
258
- normalized . maxRetries = defaults . maxRetries ;
259
- } else if ( normalized . maxRetries < 0 ) {
260
- // This is effectively infinity.
261
- normalized . maxRetries = Number . MAX_SAFE_INTEGER ;
262
- }
263
- if ( normalized . intervalms === undefined ) {
264
- normalized . intervalms = defaults . intervalms ;
265
- }
266
- return normalized as RetryOptions ;
267
- }
268
-
269
- type PeriodicOptions = {
270
- intervalms : number ;
271
- initialTimestamp : number ;
272
- } ;
273
-
274
- function normalizePeriodicOptions (
275
- opts : Partial < PeriodicOptions > | undefined ,
276
- defaults : PeriodicOptions = {
277
- intervalms : - 1 ,
278
- initialTimestamp : - 1 ,
279
- } ,
280
- ) : PeriodicOptions | undefined {
281
- if ( opts === undefined ) {
282
- return undefined ;
283
- }
284
- const normalized = { ...opts } ;
285
- if ( normalized . intervalms === undefined ) {
286
- // "never run"
287
- normalized . intervalms = defaults . intervalms ;
288
- }
289
- if ( normalized . initialTimestamp === undefined && normalized . intervalms > - 1 ) {
290
- normalized . initialTimestamp = Date . now ( ) + normalized . intervalms ;
291
- } else {
292
- normalized . initialTimestamp = defaults . initialTimestamp ;
293
- }
294
- return normalized as PeriodicOptions ;
295
- }
296
-
297
204
class BackgroundLooper {
298
205
private readonly opts : {
299
206
runDefault : RunFunc ;
300
- retry ?: RetryOptions ;
301
- periodic ?: PeriodicOptions ;
302
207
} ;
303
208
304
209
private started = false ;
@@ -320,25 +225,16 @@ class BackgroundLooper {
320
225
321
226
private lastID : number | undefined ;
322
227
323
- private nextPeriod = - 1 ;
324
-
325
228
constructor (
326
229
opts : {
327
230
runDefault ?: RunFunc ;
328
- retry ?: Partial < RetryOptions > ;
329
- periodic ?: Partial < PeriodicOptions > ;
330
231
} = { } ,
331
232
) {
332
233
this . opts = {
333
234
runDefault : opts . runDefault !== undefined
334
235
? opts . runDefault
335
236
: async ( ) => { throw Error ( 'no default operation provided' ) ; } ,
336
- retry : normalizeRetryOptions ( opts . retry ) ,
337
- periodic : normalizePeriodicOptions ( opts . periodic ) ,
338
237
} ;
339
- if ( this . opts . periodic !== undefined ) {
340
- this . nextPeriod = this . opts . periodic . initialTimestamp ;
341
- }
342
238
}
343
239
344
240
public start ( ) : void {
@@ -432,12 +328,6 @@ class BackgroundLooper {
432
328
this . done . promise . then ( ( ) => 0 ) ,
433
329
this . waitUntilReady . promise . then ( ( ) => 1 ) ,
434
330
] ;
435
- if ( this . opts . periodic !== undefined && this . nextPeriod > - 1 ) {
436
- const msLeft = Math . max ( 0 , this . nextPeriod - Date . now ( ) ) ;
437
- promises . push (
438
- sleep ( msLeft ) . then ( ( ) => 2 ) ,
439
- ) ;
440
- }
441
331
return Promise . race ( promises ) ;
442
332
} ;
443
333
@@ -447,12 +337,6 @@ class BackgroundLooper {
447
337
this . waitUntilReady = createDeferred < void > ( ) ;
448
338
// eslint-disable-next-line no-await-in-loop
449
339
await this . flush ( ) ;
450
- } else if ( winner === 2 ) {
451
- // We reset the period before queueing to avoid any races.
452
- this . nextPeriod = Date . now ( ) + this . opts . periodic ! . intervalms ;
453
- // Rather than running the request directly, we add
454
- // it to the queue. This avoids races.
455
- this . addRequest ( this . opts . runDefault ) ;
456
340
} else {
457
341
// This should not be reachable.
458
342
throw Error ( `unsupported winner ${ winner } ` ) ;
@@ -477,7 +361,7 @@ class BackgroundLooper {
477
361
const [ run , , notify ] = this . requests [ reqID ] ;
478
362
479
363
// eslint-disable-next-line no-await-in-loop
480
- await this . runRequest ( run ) ;
364
+ await run ( ) ;
481
365
482
366
// We leave the request until right before `notify()`
483
367
// for the sake of any calls to `getLastRequest()`.
@@ -487,33 +371,6 @@ class BackgroundLooper {
487
371
this . running = undefined ;
488
372
}
489
373
490
- private async runRequest ( run : RunFunc ) : Promise < void > {
491
- if ( this . opts . retry === undefined ) {
492
- // eslint-disable-next-line no-await-in-loop
493
- await run ( ) ;
494
- return ;
495
- }
496
- let retriesLeft = this . opts . retry . maxRetries ;
497
- const retryIntervalms = this . opts . retry . intervalms ;
498
- let retrying = false ;
499
- do {
500
- try {
501
- // eslint-disable-next-line no-await-in-loop
502
- await run ( ) ;
503
- } catch ( err ) {
504
- if ( retriesLeft < 1 ) {
505
- throw err ; // re-trhow
506
- }
507
- retriesLeft -= 1 ;
508
- logWarning ( `failed while handling request (${ err } )` ) ;
509
- logWarning ( `retrying (${ retriesLeft } attempts left)` ) ;
510
- // eslint-disable-next-line no-await-in-loop
511
- await sleep ( retryIntervalms ) ;
512
- retrying = true ;
513
- }
514
- } while ( ! retrying ) ;
515
- }
516
-
517
374
private getNextID ( ) : RequestID {
518
375
// For nowe there is no way to queue up a request with
519
376
// an ID that did not originate here. So we don't need
0 commit comments