Skip to content

Commit cfe3e33

Browse files
committed
sqlite - implement a way to join shutdown phase with a long running operation
1 parent 5893222 commit cfe3e33

8 files changed

Lines changed: 153 additions & 95 deletions

File tree

‎src/vs/platform/lifecycle/common/lifecycle.ts‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,44 @@ import { isThenable } from 'vs/base/common/async';
1111
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
1212

1313
/**
14-
* An event that is send out when the window is about to close. Clients have a chance to veto the closing by either calling veto
15-
* with a boolean "true" directly or with a promise that resolves to a boolean. Returning a promise is useful
16-
* in cases of long running operations on shutdown.
14+
* An event that is send out when the window is about to close. Clients have a chance to veto
15+
* the closing by either calling veto with a boolean "true" directly or with a promise that
16+
* resolves to a boolean. Returning a promise is useful in cases of long running operations
17+
* on shutdown.
1718
*
18-
* Note: It is absolutely important to avoid long running promises on this call. Please try hard to return
19-
* a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
19+
* Note: It is absolutely important to avoid long running promises on this call. Please try hard
20+
* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
2021
*/
21-
export interface ShutdownEvent {
22+
export interface WillShutdownEvent {
2223

2324
/**
24-
* Allows to veto the shutdown. The veto can be a long running operation.
25+
* Allows to veto the shutdown. The veto can be a long running operation but it
26+
* will block the application from closing.
2527
*/
2628
veto(value: boolean | Thenable<boolean>): void;
2729

30+
/**
31+
* The reason why Code will be shutting down.
32+
*/
33+
reason: ShutdownReason;
34+
}
35+
36+
/**
37+
* An event that is send out when the window closes. Clients have a chance to join the closing
38+
* by providing a promise from the join method. Returning a promise is useful in cases of long
39+
* running operations on shutdown.
40+
*
41+
* Note: It is absolutely important to avoid long running promises on this call. Please try hard
42+
* to return a boolean directly. Returning a promise has quite an impact on the shutdown sequence!
43+
*/
44+
export interface ShutdownEvent {
45+
46+
/**
47+
* Allows to join the shutdown. The promise can be a long running operation but it
48+
* will block the application from closing.
49+
*/
50+
join(promise: Thenable<void>): void;
51+
2852
/**
2953
* The reason why Code is shutting down.
3054
*/
@@ -51,6 +75,7 @@ export const enum StartupKind {
5175
ReloadedWindow = 3,
5276
ReopenedWindow = 4,
5377
}
78+
5479
export function StartupKindToString(startupKind: StartupKind): string {
5580
switch (startupKind) {
5681
case StartupKind.NewWindow: return 'NewWindow';
@@ -65,6 +90,7 @@ export const enum LifecyclePhase {
6590
Running = 3,
6691
Eventually = 4
6792
}
93+
6894
export function LifecyclePhaseToString(phase: LifecyclePhase) {
6995
switch (phase) {
7096
case LifecyclePhase.Starting: return 'Starting';
@@ -92,25 +118,25 @@ export interface ILifecycleService {
92118
*/
93119
readonly phase: LifecyclePhase;
94120

95-
/**
96-
* Returns a promise that resolves when a certain lifecycle phase
97-
* has started.
98-
*/
99-
when(phase: LifecyclePhase): Thenable<void>;
100-
101121
/**
102122
* Fired before shutdown happens. Allows listeners to veto against the
103123
* shutdown.
104124
*/
105-
readonly onWillShutdown: Event<ShutdownEvent>;
125+
readonly onWillShutdown: Event<WillShutdownEvent>;
106126

107127
/**
108128
* Fired when no client is preventing the shutdown from happening. Can be used to dispose heavy resources
109129
* like running processes. Can also be used to save UI state to storage.
110130
*
111131
* The event carries a shutdown reason that indicates how the shutdown was triggered.
112132
*/
113-
readonly onShutdown: Event<ShutdownReason>;
133+
readonly onShutdown: Event<ShutdownEvent>;
134+
135+
/**
136+
* Returns a promise that resolves when a certain lifecycle phase
137+
* has started.
138+
*/
139+
when(phase: LifecyclePhase): Thenable<void>;
114140
}
115141

116142
export const NullLifecycleService: ILifecycleService = {

‎src/vs/platform/lifecycle/electron-browser/lifecycleService.ts‎

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { TPromise } from 'vs/base/common/winjs.base';
77
import { toErrorMessage } from 'vs/base/common/errorMessage';
8-
import { ILifecycleService, ShutdownEvent, ShutdownReason, StartupKind, LifecyclePhase, handleVetos, LifecyclePhaseToString } from 'vs/platform/lifecycle/common/lifecycle';
8+
import { ILifecycleService, WillShutdownEvent, ShutdownReason, StartupKind, LifecyclePhase, handleVetos, LifecyclePhaseToString, ShutdownEvent } from 'vs/platform/lifecycle/common/lifecycle';
99
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
1010
import { ipcRenderer as ipc } from 'electron';
1111
import { Event, Emitter } from 'vs/base/common/event';
@@ -14,74 +14,95 @@ import { mark } from 'vs/base/common/performance';
1414
import { Barrier } from 'vs/base/common/async';
1515
import { ILogService } from 'vs/platform/log/common/log';
1616
import { INotificationService } from 'vs/platform/notification/common/notification';
17+
import { Disposable } from 'vs/base/common/lifecycle';
18+
import { onUnexpectedError } from 'vs/base/common/errors';
1719

18-
export class LifecycleService implements ILifecycleService {
20+
export class LifecycleService extends Disposable implements ILifecycleService {
1921

20-
private static readonly _lastShutdownReasonKey = 'lifecyle.lastShutdownReason';
22+
private static readonly LAST_SHUTDOWN_REASON_KEY = 'lifecyle.lastShutdownReason';
2123

2224
_serviceBrand: any;
2325

24-
private readonly _onWillShutdown = new Emitter<ShutdownEvent>();
25-
private readonly _onShutdown = new Emitter<ShutdownReason>();
26+
private readonly _onWillShutdown = this._register(new Emitter<WillShutdownEvent>());
27+
get onWillShutdown(): Event<WillShutdownEvent> { return this._onWillShutdown.event; }
28+
29+
private readonly _onShutdown = this._register(new Emitter<ShutdownEvent>());
30+
get onShutdown(): Event<ShutdownEvent> { return this._onShutdown.event; }
31+
2632
private readonly _startupKind: StartupKind;
33+
get startupKind(): StartupKind { return this._startupKind; }
2734

2835
private _phase: LifecyclePhase = LifecyclePhase.Starting;
36+
get phase(): LifecyclePhase { return this._phase; }
37+
2938
private _phaseWhen = new Map<LifecyclePhase, Barrier>();
3039

3140
constructor(
32-
@INotificationService private readonly _notificationService: INotificationService,
33-
@IWindowService private readonly _windowService: IWindowService,
34-
@IStorageService private readonly _storageService: IStorageService,
35-
@ILogService private readonly _logService: ILogService
41+
@INotificationService private notificationService: INotificationService,
42+
@IWindowService private windowService: IWindowService,
43+
@IStorageService private storageService: IStorageService,
44+
@ILogService private logService: ILogService
3645
) {
37-
const lastShutdownReason = this._storageService.getInteger(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
38-
this._storageService.remove(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
46+
super();
47+
48+
this._startupKind = this.resolveStartupKind();
49+
50+
this.registerListeners();
51+
}
52+
53+
private resolveStartupKind(): StartupKind {
54+
const lastShutdownReason = this.storageService.getInteger(LifecycleService.LAST_SHUTDOWN_REASON_KEY, StorageScope.WORKSPACE);
55+
this.storageService.remove(LifecycleService.LAST_SHUTDOWN_REASON_KEY, StorageScope.WORKSPACE);
56+
57+
let startupKind: StartupKind;
3958
if (lastShutdownReason === ShutdownReason.RELOAD) {
40-
this._startupKind = StartupKind.ReloadedWindow;
59+
startupKind = StartupKind.ReloadedWindow;
4160
} else if (lastShutdownReason === ShutdownReason.LOAD) {
42-
this._startupKind = StartupKind.ReopenedWindow;
61+
startupKind = StartupKind.ReopenedWindow;
4362
} else {
44-
this._startupKind = StartupKind.NewWindow;
63+
startupKind = StartupKind.NewWindow;
4564
}
4665

47-
this._logService.trace(`lifecycle: starting up (startup kind: ${this._startupKind})`);
66+
this.logService.trace(`lifecycle: starting up (startup kind: ${this._startupKind})`);
4867

49-
this._registerListeners();
68+
return startupKind;
5069
}
5170

52-
private _registerListeners(): void {
53-
const windowId = this._windowService.getCurrentWindowId();
71+
private registerListeners(): void {
72+
const windowId = this.windowService.getCurrentWindowId();
5473

5574
// Main side indicates that window is about to unload, check for vetos
5675
ipc.on('vscode:onBeforeUnload', (event, reply: { okChannel: string, cancelChannel: string, reason: ShutdownReason }) => {
57-
this._logService.trace(`lifecycle: onBeforeUnload (reason: ${reply.reason})`);
76+
this.logService.trace(`lifecycle: onBeforeUnload (reason: ${reply.reason})`);
5877

5978
// store shutdown reason to retrieve next startup
60-
this._storageService.store(LifecycleService._lastShutdownReasonKey, JSON.stringify(reply.reason), StorageScope.WORKSPACE);
79+
this.storageService.store(LifecycleService.LAST_SHUTDOWN_REASON_KEY, JSON.stringify(reply.reason), StorageScope.WORKSPACE);
6180

6281
// trigger onWillShutdown events and veto collecting
63-
this.onBeforeUnload(reply.reason).then(veto => {
82+
this.handleWillShutdown(reply.reason).then(veto => {
6483
if (veto) {
65-
this._logService.trace('lifecycle: onBeforeUnload prevented via veto');
66-
this._storageService.remove(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
84+
this.logService.trace('lifecycle: onBeforeUnload prevented via veto');
85+
this.storageService.remove(LifecycleService.LAST_SHUTDOWN_REASON_KEY, StorageScope.WORKSPACE);
6786
ipc.send(reply.cancelChannel, windowId);
6887
} else {
69-
this._logService.trace('lifecycle: onBeforeUnload continues without veto');
88+
this.logService.trace('lifecycle: onBeforeUnload continues without veto');
7089
ipc.send(reply.okChannel, windowId);
7190
}
7291
});
7392
});
7493

7594
// Main side indicates that we will indeed shutdown
7695
ipc.on('vscode:onWillUnload', (event, reply: { replyChannel: string, reason: ShutdownReason }) => {
77-
this._logService.trace(`lifecycle: onWillUnload (reason: ${reply.reason})`);
96+
this.logService.trace(`lifecycle: onWillUnload (reason: ${reply.reason})`);
7897

79-
this._onShutdown.fire(reply.reason);
80-
ipc.send(reply.replyChannel, windowId);
98+
// trigger onShutdown events and joining
99+
return this.handleShutdown(reply.reason).then(() => {
100+
ipc.send(reply.replyChannel, windowId);
101+
});
81102
});
82103
}
83104

84-
private onBeforeUnload(reason: ShutdownReason): TPromise<boolean> {
105+
private handleWillShutdown(reason: ShutdownReason): TPromise<boolean> {
85106
const vetos: (boolean | Thenable<boolean>)[] = [];
86107

87108
this._onWillShutdown.fire({
@@ -91,11 +112,28 @@ export class LifecycleService implements ILifecycleService {
91112
reason
92113
});
93114

94-
return handleVetos(vetos, err => this._notificationService.error(toErrorMessage(err)));
115+
return handleVetos(vetos, err => {
116+
this.notificationService.error(toErrorMessage(err));
117+
onUnexpectedError(err);
118+
});
95119
}
96120

97-
get phase(): LifecyclePhase {
98-
return this._phase;
121+
private handleShutdown(reason: ShutdownReason): Thenable<void> {
122+
const joiners: Thenable<void>[] = [];
123+
124+
this._onShutdown.fire({
125+
join(promise) {
126+
if (promise) {
127+
joiners.push(promise);
128+
}
129+
},
130+
reason
131+
});
132+
133+
return TPromise.join(joiners).then(() => void 0, err => {
134+
this.notificationService.error(toErrorMessage(err));
135+
onUnexpectedError(err);
136+
});
99137
}
100138

101139
set phase(value: LifecyclePhase) {
@@ -107,7 +145,7 @@ export class LifecycleService implements ILifecycleService {
107145
return;
108146
}
109147

110-
this._logService.trace(`lifecycle: phase changed (value: ${value})`);
148+
this.logService.trace(`lifecycle: phase changed (value: ${value})`);
111149

112150
this._phase = value;
113151
mark(`LifecyclePhase/${LifecyclePhaseToString(value)}`);
@@ -131,16 +169,4 @@ export class LifecycleService implements ILifecycleService {
131169

132170
return barrier.wait();
133171
}
134-
135-
get startupKind(): StartupKind {
136-
return this._startupKind;
137-
}
138-
139-
get onWillShutdown(): Event<ShutdownEvent> {
140-
return this._onWillShutdown.event;
141-
}
142-
143-
get onShutdown(): Event<ShutdownReason> {
144-
return this._onShutdown.event;
145-
}
146172
}

‎src/vs/platform/lifecycle/electron-main/lifecycleMain.ts‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ICodeWindow } from 'vs/platform/windows/electron-main/windows';
1313
import { ReadyState } from 'vs/platform/windows/common/windows';
1414
import { handleVetos } from 'vs/platform/lifecycle/common/lifecycle';
1515
import { isMacintosh, isWindows } from 'vs/base/common/platform';
16+
import { Disposable } from 'vs/base/common/lifecycle';
1617

1718
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
1819

@@ -80,41 +81,39 @@ export interface ILifecycleService {
8081
kill(code?: number): void;
8182
}
8283

83-
export class LifecycleService implements ILifecycleService {
84+
export class LifecycleService extends Disposable implements ILifecycleService {
8485

8586
_serviceBrand: any;
8687

8788
private static readonly QUIT_FROM_RESTART_MARKER = 'quit.from.restart'; // use a marker to find out if the session was restarted
8889

89-
private windowToCloseRequest: { [windowId: string]: boolean };
90-
private quitRequested: boolean;
90+
private windowToCloseRequest: { [windowId: string]: boolean } = Object.create(null);
91+
private quitRequested = false;
9192
private pendingQuitPromise: TPromise<boolean>;
9293
private pendingQuitPromiseComplete: TValueCallback<boolean>;
93-
private oneTimeListenerTokenGenerator: number;
94-
private _wasRestarted: boolean;
95-
private windowCounter: number;
94+
private oneTimeListenerTokenGenerator = 0;
95+
private windowCounter = 0;
9696

97-
private _onBeforeShutdown = new Emitter<void>();
98-
onBeforeShutdown: Event<void> = this._onBeforeShutdown.event;
97+
private _wasRestarted: boolean = false;
98+
get wasRestarted(): boolean { return this._wasRestarted; }
9999

100-
private _onShutdown = new Emitter<void>();
101-
onShutdown: Event<void> = this._onShutdown.event;
100+
private readonly _onBeforeShutdown = this._register(new Emitter<void>());
101+
readonly onBeforeShutdown: Event<void> = this._onBeforeShutdown.event;
102102

103-
private _onBeforeWindowClose = new Emitter<ICodeWindow>();
104-
onBeforeWindowClose: Event<ICodeWindow> = this._onBeforeWindowClose.event;
103+
private readonly _onShutdown = this._register(new Emitter<void>());
104+
readonly onShutdown: Event<void> = this._onShutdown.event;
105105

106-
private _onBeforeWindowUnload = new Emitter<IWindowUnloadEvent>();
107-
onBeforeWindowUnload: Event<IWindowUnloadEvent> = this._onBeforeWindowUnload.event;
106+
private readonly _onBeforeWindowClose = this._register(new Emitter<ICodeWindow>());
107+
readonly onBeforeWindowClose: Event<ICodeWindow> = this._onBeforeWindowClose.event;
108+
109+
private readonly _onBeforeWindowUnload = this._register(new Emitter<IWindowUnloadEvent>());
110+
readonly onBeforeWindowUnload: Event<IWindowUnloadEvent> = this._onBeforeWindowUnload.event;
108111

109112
constructor(
110113
@ILogService private logService: ILogService,
111114
@IStateService private stateService: IStateService
112115
) {
113-
this.windowToCloseRequest = Object.create(null);
114-
this.quitRequested = false;
115-
this.oneTimeListenerTokenGenerator = 0;
116-
this._wasRestarted = false;
117-
this.windowCounter = 0;
116+
super();
118117

119118
this.handleRestarted();
120119
}
@@ -127,10 +126,6 @@ export class LifecycleService implements ILifecycleService {
127126
}
128127
}
129128

130-
get wasRestarted(): boolean {
131-
return this._wasRestarted;
132-
}
133-
134129
get isQuitRequested(): boolean {
135130
return !!this.quitRequested;
136131
}

0 commit comments

Comments
 (0)