Skip to content

Commit 459692e

Browse files
committed
Start heartbeat as soon as possible
1 parent dffc37c commit 459692e

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/heartbeat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class HeartbeatManager extends Disposable {
110110
}
111111
}));
112112

113-
this.logService.info(`Heartbeat manager for workspace ${this.connectionInfo.workspaceId} (${this.connectionInfo.instanceId}) - ${this.connectionInfo.gitpodHost} started`);
113+
this.logService.info(`Heartbeat manager for workspace ${this.connectionInfo.workspaceId} - ${this.connectionInfo.gitpodHost} started`);
114114

115115
// Start heartbeating interval
116116
this.sendHeartBeat();

src/remoteSession.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,43 @@ export class RemoteSession extends Disposable {
6666
this.usePublicApi = await this.experiments.getUsePublicAPI(this.connectionInfo.gitpodHost);
6767
this.logService.info(`Going to use ${this.usePublicApi ? 'public' : 'server'} API`);
6868

69-
let instanceId: string;
7069
if (this.usePublicApi) {
7170
this.workspaceState = new WorkspaceState(this.connectionInfo.workspaceId, this.sessionService, this.logService);
72-
await this.workspaceState.initialize();
73-
if (!this.workspaceState.instanceId || !this.workspaceState.isWorkspaceRunning) {
74-
throw new NoRunningInstanceError(this.connectionInfo.workspaceId, this.workspaceState.workspaceData.phase);
75-
}
71+
this.workspaceState.initialize()
72+
.then(() => {
73+
if (!this.workspaceState!.instanceId || !this.workspaceState!.isWorkspaceRunning) {
74+
vscode.commands.executeCommand('workbench.action.remote.close');
75+
return;
76+
}
77+
const instanceId = this.workspaceState!.instanceId;
78+
if (instanceId !== this.connectionInfo.instanceId) {
79+
this.logService.info(`Updating workspace ${this.connectionInfo.workspaceId} latest instance id ${this.connectionInfo.instanceId} => ${instanceId}`);
80+
this.connectionInfo.instanceId = instanceId;
81+
}
82+
83+
const { sshDestStr } = getGitpodRemoteWindowConnectionInfo(this.context)!;
84+
this.context.globalState.update(`${SSH_DEST_KEY}${sshDestStr}`, { ...this.connectionInfo } as SSHConnectionParams);
85+
});
7686

7787
this._register(this.workspaceState.onWorkspaceWillStop(async () => {
7888
await this.remoteService.saveRestartInfo();
7989
vscode.commands.executeCommand('workbench.action.remote.close');
8090
}));
81-
instanceId = this.workspaceState.instanceId;
8291
} else {
8392
const workspaceInfo = await withServerApi(this.sessionService.getGitpodToken(), this.connectionInfo.gitpodHost, service => service.server.getWorkspace(this.connectionInfo.workspaceId), this.logService);
8493
if (!workspaceInfo.latestInstance || workspaceInfo.latestInstance?.status?.phase === 'stopping' || workspaceInfo.latestInstance?.status?.phase === 'stopped') {
8594
throw new NoRunningInstanceError(this.connectionInfo.workspaceId, workspaceInfo.latestInstance?.status?.phase);
8695
}
87-
instanceId = workspaceInfo.latestInstance.id;
88-
}
96+
const instanceId = workspaceInfo.latestInstance.id;
97+
if (instanceId !== this.connectionInfo.instanceId) {
98+
this.logService.info(`Updating workspace ${this.connectionInfo.workspaceId} latest instance id ${this.connectionInfo.instanceId} => ${instanceId}`);
99+
this.connectionInfo.instanceId = instanceId;
100+
}
89101

90-
if (instanceId !== this.connectionInfo.instanceId) {
91-
this.logService.info(`Updating workspace ${this.connectionInfo.workspaceId} latest instance id ${this.connectionInfo.instanceId} => ${instanceId}`);
92-
this.connectionInfo.instanceId = instanceId;
102+
const { sshDestStr } = getGitpodRemoteWindowConnectionInfo(this.context)!;
103+
this.context.globalState.update(`${SSH_DEST_KEY}${sshDestStr}`, { ...this.connectionInfo } as SSHConnectionParams);
93104
}
94105

95-
const { sshDestStr } = getGitpodRemoteWindowConnectionInfo(this.context)!;
96-
await this.context.globalState.update(`${SSH_DEST_KEY}${sshDestStr}`, { ...this.connectionInfo } as SSHConnectionParams);
97-
98106
this.heartbeatManager = new HeartbeatManager(this.connectionInfo, this.workspaceState, this.sessionService, this.logService, this.telemetryService);
99107

100108
this.remoteService.initializeRemoteExtensions();

src/workspaceState.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,29 @@ export class WorkspaceState extends Disposable {
2323
readonly onWorkspaceStopped = onceEvent(filterEvent(this.onWorkspaceStateChanged, () => this.isWorkspaceStopped));
2424

2525
public get isWorkspaceStopping() {
26+
if (!this._workspaceData) {
27+
return false;
28+
}
29+
2630
const phase = this._workspaceStatus?.instance?.status?.phase;
2731
return phase === WorkspaceInstanceStatus_Phase.STOPPING;
2832
}
2933

3034
public get isWorkspaceStopped() {
35+
if (!this._workspaceData) {
36+
return false;
37+
}
38+
3139
const phase = this._workspaceStatus?.instance?.status?.phase;
3240
return phase === WorkspaceInstanceStatus_Phase.STOPPED;
3341
}
3442

3543
public get isWorkspaceRunning() {
44+
if (!this._workspaceData) {
45+
// If not initialized yet assume it's running
46+
return true;
47+
}
48+
3649
const phase = this._workspaceStatus?.instance?.status?.phase;
3750
return phase === WorkspaceInstanceStatus_Phase.RUNNING;
3851
}
@@ -64,27 +77,25 @@ export class WorkspaceState extends Disposable {
6477

6578
public async initialize() {
6679
const ws = await this.sessionService.getAPI().getWorkspace(this.workspaceId);
67-
this._workspaceStatus = ws!.status;
6880
this._workspaceData = rawWorkspaceToWorkspaceData(ws);
69-
this.logService.trace(`WorkspaceState: initial state`, this._workspaceData.phase);
81+
this.checkWorkspaceState(ws.status!);
7082
}
7183

72-
private async checkWorkspaceState(workspaceState: WorkspaceStatus) {
73-
if (!this._workspaceStatus) {
74-
this.logService.error(`WorkspaceState not initialized`);
84+
private checkWorkspaceState(workspaceState: WorkspaceStatus) {
85+
if (!this._workspaceData) {
7586
return;
7687
}
7788

7889
const phase = workspaceState.instance?.status?.phase;
79-
const oldPhase = this._workspaceStatus.instance?.status?.phase;
90+
const oldPhase = this._workspaceStatus?.instance?.status?.phase;
8091
this._workspaceStatus = workspaceState;
8192

8293
this._workspaceData!.workspaceUrl = workspaceState.instance!.status!.url;
8394
this._workspaceData!.phase = WorkspaceInstanceStatus_Phase[workspaceState.instance!.status!.phase ?? WorkspaceInstanceStatus_Phase.UNSPECIFIED].toLowerCase() as WorkspacePhase;
8495
this._workspaceData!.lastUsed = workspaceState.instance!.createdAt!.toDate();
8596
this._workspaceData!.recentFolders = workspaceState.instance!.status!.recentFolders;
8697

87-
if (phase && oldPhase && phase !== oldPhase) {
98+
if (typeof phase !== undefined && phase !== oldPhase) {
8899
this.logService.trace(`WorkspaceState: update state`, this._workspaceData!.phase);
89100
this._onWorkspaceStateChanged.fire();
90101
}

0 commit comments

Comments
 (0)