Skip to content

Commit f497367

Browse files
committed
Notify when workspace stops running
1 parent 127e579 commit f497367

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/workspaceMonitor.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,37 @@ export class WorkspaceMonitor implements vscode.Disposable {
3939
const watchUrl = new URL(`${url}/api/v2/workspaces/${workspace.id}/watch`)
4040
this.storage.writeToCoderOutputChannel(`Monitoring ${watchUrl}`)
4141

42-
this.eventSource = new EventSource(watchUrl.toString(), {
42+
const eventSource = new EventSource(watchUrl.toString(), {
4343
headers: {
4444
"Coder-Session-Token": token,
4545
},
4646
})
4747

48-
this.eventSource.addEventListener("data", (event) => {
48+
eventSource.addEventListener("data", (event) => {
4949
try {
5050
const newWorkspaceData = JSON.parse(event.data) as Workspace
5151
this.update(newWorkspaceData)
52+
this.maybeNotify(newWorkspaceData)
5253
this.onChange.fire(newWorkspaceData)
5354
} catch (error) {
5455
this.notifyError(error)
5556
}
5657
})
5758

58-
this.eventSource.addEventListener("error", (error) => {
59+
eventSource.addEventListener("error", (error) => {
5960
this.notifyError(error)
6061
})
6162

63+
// Store so we can close in dispose().
64+
this.eventSource = eventSource
65+
6266
this.updateStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 999)
6367
this.updateStatusBarItem.name = "Coder Workspace Update"
6468
this.updateStatusBarItem.text = "$(fold-up) Update Workspace"
6569
this.updateStatusBarItem.command = "coder.workspace.update"
6670

6771
this.update(workspace) // Set initial state.
72+
this.maybeNotify(workspace)
6873
}
6974

7075
/**
@@ -81,9 +86,13 @@ export class WorkspaceMonitor implements vscode.Disposable {
8186
private update(workspace: Workspace) {
8287
this.updateContext(workspace)
8388
this.updateStatusBar(workspace)
89+
}
90+
91+
private maybeNotify(workspace: Workspace) {
8492
this.maybeNotifyOutdated(workspace)
8593
this.maybeNotifyAutostop(workspace)
8694
this.maybeNotifyDeletion(workspace)
95+
this.maybeNotifyNotRunning(workspace)
8796
}
8897

8998
private maybeNotifyAutostop(workspace: Workspace) {
@@ -111,6 +120,23 @@ export class WorkspaceMonitor implements vscode.Disposable {
111120
}
112121
}
113122

123+
private maybeNotifyNotRunning(workspace: Workspace) {
124+
if (workspace.latest_build.status !== "running") {
125+
vscode.window.showInformationMessage(
126+
"Your workspace is no longer running!",
127+
{
128+
detail: "Reloading the window to reconnect.",
129+
},
130+
"Reload Window",
131+
).then((action) => {
132+
if (!action) {
133+
return
134+
}
135+
vscode.commands.executeCommand("workbench.action.reloadWindow")
136+
})
137+
}
138+
}
139+
114140
private isImpending(target: string, notifyTime: number): boolean {
115141
const nowTime = new Date().getTime()
116142
const targetTime = new Date(target).getTime()

0 commit comments

Comments
 (0)