1
>
/*---------------------------------------------------------------------------------------------
serverLifetimeService.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { Disposable, IDisposable, toDisposable } from '../../base/common/lifecycle.js';
7
>
import { createDecorator } from '../../platform/instantiation/common/instantiation.js';
8
>
import { ILogService } from '../../platform/log/common/log.js';
9
>
10
>
export const IServerLifetimeService = createDecorator<IServerLifetimeService>('serverLifetimeService');
11
>
12
>
export const SHUTDOWN_TIMEOUT = 5 * 60 * 1000;
13
>
14
>
/** Options controlling the auto-shutdown behaviour. */
15
>
export interface IServerLifetimeOptions {
16
>
/** When `false` (default), the server never auto-shuts down. */
17
>
readonly enableAutoShutdown?: boolean;
18
>
/** When `true`, skip the 5-minute grace period on non-initial shutdowns. */
19
>
readonly shutdownWithoutDelay?: boolean;
20
>
}
21
>
22
>
/**
23
>
* Tracks active consumers (extension hosts, agent sessions, etc.) that keep
24
>
* the server alive. When auto-shutdown is enabled, the service manages a
25
>
* shutdown timer and fires {@link onDidShutdownRequested} when it is time for
26
>
* the process to exit.
27
>
*/
28
>
export interface IServerLifetimeService {
29
>
readonly _serviceBrand: undefined;
30
>
31
>
/**
32
>
* Marks a consumer as active. The server will not auto-shutdown until the
33
>
* returned {@link IDisposable} is disposed.
34
>
*/
35
>
active(consumer: string): IDisposable;
36
>
37
>
/**
38
>
* Delays the auto-shutdown timer. If the server is currently in a shutdown
39
>
* timeout (all consumers inactive), the timer is reset.
40
>
*/
41
>
delay(): void;
42
>
43
>
/** Whether any consumer is currently active. */
44
>
readonly hasActiveConsumers: boolean;
45
>
}
46
>
47
>
export class ServerLifetimeService extends Disposable implements IServerLifetimeService {
48
>
declare readonly _serviceBrand: undefined;
49
>
50
>
private readonly _consumers = new Map<string, number>();
51
>
private _totalCount = 0;
52
>
private _shutdownTimer: ReturnType<typeof setTimeout> | undefined;
53
>
54
>
constructor(
55
private readonly _options: IServerLifetimeOptions,
56
@ILogService private readonly _logService: ILogService,