src/vs/server/node/serverLifetimeService.ts
145 LOC · 109 covered · 36 uncovered · 18 ranges · 15 concepts · 4 introducers · 12 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
serverLifetimeService.ts ×8
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable, IDisposable, toDisposable } from '../../base/common/lifecycle.js';
import { createDecorator } from '../../platform/instantiation/common/instantiation.js';
import { ILogService } from '../../platform/log/common/log.js';
export const IServerLifetimeService = createDecorator<IServerLifetimeService>('serverLifetimeService');
export const SHUTDOWN_TIMEOUT = 5 * 60 * 1000;
/** Options controlling the auto-shutdown behaviour. */
export interface IServerLifetimeOptions {
/** When `false` (default), the server never auto-shuts down. */
readonly enableAutoShutdown?: boolean;
/** When `true`, skip the 5-minute grace period on non-initial shutdowns. */
readonly shutdownWithoutDelay?: boolean;
}
/**
* Tracks active consumers (extension hosts, agent sessions, etc.) that keep
* the server alive. When auto-shutdown is enabled, the service manages a
* shutdown timer and fires {@link onDidShutdownRequested} when it is time for
* the process to exit.
*/
export interface IServerLifetimeService {
readonly _serviceBrand: undefined;
/**
* Marks a consumer as active. The server will not auto-shutdown until the
* returned {@link IDisposable} is disposed.
*/
active(consumer: string): IDisposable;
/**
* Delays the auto-shutdown timer. If the server is currently in a shutdown
* timeout (all consumers inactive), the timer is reset.
*/
delay(): void;
/** Whether any consumer is currently active. */
readonly hasActiveConsumers: boolean;
}
export class ServerLifetimeService extends Disposable implements IServerLifetimeService {
declare readonly _serviceBrand: undefined;
private readonly _consumers = new Map<string, number>();
private _totalCount = 0;
private _shutdownTimer: ReturnType<typeof setTimeout> | undefined;
constructor(
@ILogService private readonly _logService: ILogService,
) {
super();
if (this._options.enableAutoShutdown) {
// Start initial shutdown timer (no clients connected yet)
this._scheduleShutdown(true);
}
get hasActiveConsumers(): boolean {
}
active(consumer: string): IDisposable {
const current = this._consumers.get(consumer) ?? 0;
this._consumers.set(consumer, current + 1);
this._totalCount++;
this._logService.debug(`ServerLifetime: consumer '${consumer}' active (total: ${this._totalCount})`);
if (wasEmpty) {
this._cancelShutdown();
}
let disposed = false;
return toDisposable(() => {
if (disposed) {
return;
}
const count = this._consumers.get(consumer);
if (count !== undefined) {
if (count <= 1) {
this._consumers.delete(consumer);
} else {
}
this._totalCount--;
this._logService.debug(`ServerLifetime: consumer '${consumer}' inactive (total: ${this._totalCount})`);
if (this._totalCount === 0 && this._options.enableAutoShutdown) {
this._scheduleShutdown(false);
}
}
delay(): void {
if (this._shutdownTimer) {
this._logService.debug('ServerLifetime: delay requested, resetting shutdown timer');
this._cancelShutdown();
this._scheduleShutdown(false);
}
}
private _scheduleShutdown(initial: boolean): void {
if (this._options.shutdownWithoutDelay && !initial) {
this._tryShutdown();
} else {
this._logService.debug('ServerLifetime: scheduling shutdown timer');
this._shutdownTimer = setTimeout(() => {
this._shutdownTimer = undefined;
this._tryShutdown();
}, SHUTDOWN_TIMEOUT);
}
}
private _tryShutdown(): void {
if (this._totalCount > 0) {
this._logService.debug('ServerLifetime: consumer became active, aborting shutdown');
return;
}
console.log('All consumers inactive, shutting down');
this._logService.info('ServerLifetime: all consumers inactive, shutting down');
this.dispose();
process.exit(0);
}
private _cancelShutdown(): void {
this._logService.debug('ServerLifetime: cancelling shutdown timer');
clearTimeout(this._shutdownTimer);
this._shutdownTimer = undefined;
}