Skip to content

feat: add VS code notifications for autostop #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getBuildInfo,
getTemplate,
getWorkspace,
getWorkspaces,
getWorkspaceBuildLogs,
getWorkspaceByOwnerAndName,
startWorkspace,
Expand Down Expand Up @@ -126,6 +127,44 @@ export class Remote {
this.registerLabelFormatter(remoteAuthority, this.storage.workspace.owner_name, this.storage.workspace.name),
)

const notifyWorkspacesEligibleForAutostop = () => {
const eligibleWorkspaces = this.storage.ownedWorkspaces?.filter((workspace: Workspace) => {
if (workspace.latest_build.transition !== "start" || !workspace.latest_build.deadline) {
return false
}

const hoursMilli = 1000 * 60 * 60
// return workspaces with a deadline that is in 1 hr or less
return Math.abs(new Date().getTime() - new Date(workspace.latest_build.deadline).getTime()) <= hoursMilli
})

eligibleWorkspaces?.forEach((workspace: Workspace) => {
if (this.storage.workspaceIdsEligibleForAutostop?.includes(workspace.id)) {
// don't message the user; we've already messaged
return
}
// we display individual notifications for each workspace as VS Code
// intentionally strips new lines from the message text
// https://github.com/Microsoft/vscode/issues/48900
this.vscodeProposed.window.showInformationMessage(`${workspace.name} is scheduled to shut down in 1 hour.`)
this.storage.workspaceIdsEligibleForAutostop?.push(workspace.id)
})
}

let errorCount = 0
const fetchWorkspacesInterval = setInterval(async () => {
try {
const workspacesResult = await getWorkspaces({ q: "owner:me" })
this.storage.ownedWorkspaces = workspacesResult.workspaces
notifyWorkspacesEligibleForAutostop()
} catch (error) {
if (errorCount === 3) {
clearInterval(fetchWorkspacesInterval)
}
errorCount++
}
}, 1000 * 5)

let buildComplete: undefined | (() => void)
if (this.storage.workspace.latest_build.status === "stopped") {
this.vscodeProposed.window.withProgress(
Expand Down Expand Up @@ -427,6 +466,7 @@ export class Remote {
return {
dispose: () => {
eventSource.close()
clearInterval(fetchWorkspacesInterval)
disposables.forEach((d) => d.dispose())
},
}
Expand Down
2 changes: 2 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import * as vscode from "vscode"

export class Storage {
public workspace?: Workspace
public ownedWorkspaces?: Workspace[] = []
public workspaceIdsEligibleForAutostop?: string[] = []

constructor(
private readonly output: vscode.OutputChannel,
Expand Down
11 changes: 8 additions & 3 deletions src/workspacesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ export class WorkspaceProvider implements vscode.TreeDataProvider<vscode.TreeIte
.then((workspaces) => {
const workspacesTreeItem: WorkspaceTreeItem[] = []
workspaces.workspaces.forEach((workspace) => {
const showMetadata = this.getWorkspacesQuery === WorkspaceQuery.Mine
if (showMetadata) {
const isOwnedWorkspace = this.getWorkspacesQuery === WorkspaceQuery.Mine
if (isOwnedWorkspace) {
// update ownedWorkspaces list in storage such that we can display to the user
// notifications about their own workspaces
this.storage.ownedWorkspaces?.push(workspace)

// Show metadata for workspaces owned by the user
const agents = extractAgents(workspace)
agents.forEach((agent) => this.monitorMetadata(agent.id)) // monitor metadata for all agents
}
const treeItem = new WorkspaceTreeItem(
workspace,
this.getWorkspacesQuery === WorkspaceQuery.All,
showMetadata,
isOwnedWorkspace,
)
workspacesTreeItem.push(treeItem)
})
Expand Down