|
| 1 | +import axios from "axios" |
| 2 | +import { getWorkspaces } from "coder/site/src/api/api" |
| 3 | +import { Workspace, WorkspacesResponse, WorkspaceBuild } from "coder/site/src/api/typesGenerated" |
| 4 | +import { formatDistanceToNowStrict } from "date-fns" |
| 5 | +import * as vscode from "vscode" |
| 6 | +import { Storage } from "./storage" |
| 7 | + |
| 8 | +interface NotifiedWorkspace { |
| 9 | + workspace: Workspace |
| 10 | + wasNotified: boolean |
| 11 | + impendingActionDeadline: string |
| 12 | +} |
| 13 | + |
| 14 | +type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>> |
| 15 | + |
| 16 | +type WorkspaceWithDeadline = Workspace & { latest_build: WithRequired<WorkspaceBuild, "deadline"> } |
| 17 | +type WorkspaceWithDeletingAt = WithRequired<Workspace, "deleting_at"> |
| 18 | + |
| 19 | +export class WorkspaceAction { |
| 20 | + // We use this same interval in the Dashboard to poll for updates on the Workspaces page. |
| 21 | + #POLL_INTERVAL: number = 1000 * 5 |
| 22 | + #fetchWorkspacesInterval?: ReturnType<typeof setInterval> |
| 23 | + |
| 24 | + #ownedWorkspaces: Workspace[] = [] |
| 25 | + #workspacesApproachingAutostop: NotifiedWorkspace[] = [] |
| 26 | + #workspacesApproachingDeletion: NotifiedWorkspace[] = [] |
| 27 | + |
| 28 | + private constructor( |
| 29 | + private readonly vscodeProposed: typeof vscode, |
| 30 | + private readonly storage: Storage, |
| 31 | + ownedWorkspaces: Workspace[], |
| 32 | + ) { |
| 33 | + this.#ownedWorkspaces = ownedWorkspaces |
| 34 | + |
| 35 | + // seed initial lists |
| 36 | + this.updateNotificationLists() |
| 37 | + |
| 38 | + this.notifyAll() |
| 39 | + |
| 40 | + // set up polling so we get current workspaces data |
| 41 | + this.pollGetWorkspaces() |
| 42 | + } |
| 43 | + |
| 44 | + static async init(vscodeProposed: typeof vscode, storage: Storage) { |
| 45 | + // fetch all workspaces owned by the user and set initial public class fields |
| 46 | + let ownedWorkspacesResponse: WorkspacesResponse |
| 47 | + try { |
| 48 | + ownedWorkspacesResponse = await getWorkspaces({ q: "owner:me" }) |
| 49 | + } catch (error) { |
| 50 | + let status |
| 51 | + if (axios.isAxiosError(error)) { |
| 52 | + status = error.response?.status |
| 53 | + } |
| 54 | + if (status !== 401) { |
| 55 | + storage.writeToCoderOutputChannel( |
| 56 | + `Failed to fetch owned workspaces. Some workspace notifications may be missing: ${error}`, |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + ownedWorkspacesResponse = { workspaces: [], count: 0 } |
| 61 | + } |
| 62 | + return new WorkspaceAction(vscodeProposed, storage, ownedWorkspacesResponse.workspaces) |
| 63 | + } |
| 64 | + |
| 65 | + updateNotificationLists() { |
| 66 | + this.#workspacesApproachingAutostop = this.#ownedWorkspaces |
| 67 | + .filter(this.filterWorkspacesImpendingAutostop) |
| 68 | + .map((workspace) => |
| 69 | + this.transformWorkspaceObjects(workspace, this.#workspacesApproachingAutostop, workspace.latest_build.deadline), |
| 70 | + ) |
| 71 | + |
| 72 | + this.#workspacesApproachingDeletion = this.#ownedWorkspaces |
| 73 | + .filter(this.filterWorkspacesImpendingDeletion) |
| 74 | + .map((workspace) => |
| 75 | + this.transformWorkspaceObjects(workspace, this.#workspacesApproachingDeletion, workspace.deleting_at), |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + filterWorkspacesImpendingAutostop(workspace: Workspace): workspace is WorkspaceWithDeadline { |
| 80 | + // a workspace is eligible for autostop if the workspace is running and it has a deadline |
| 81 | + if (workspace.latest_build.status !== "running" || !workspace.latest_build.deadline) { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + const hourMilli = 1000 * 60 * 60 |
| 86 | + // return workspaces with a deadline that is in 1 hr or less |
| 87 | + return Math.abs(new Date().getTime() - new Date(workspace.latest_build.deadline).getTime()) <= hourMilli |
| 88 | + } |
| 89 | + |
| 90 | + filterWorkspacesImpendingDeletion(workspace: Workspace): workspace is WorkspaceWithDeletingAt { |
| 91 | + if (!workspace.deleting_at) { |
| 92 | + return false |
| 93 | + } |
| 94 | + |
| 95 | + const dayMilli = 1000 * 60 * 60 * 24 |
| 96 | + |
| 97 | + // return workspaces with a deleting_at that is 24 hrs or less |
| 98 | + return Math.abs(new Date().getTime() - new Date(workspace.deleting_at).getTime()) <= dayMilli |
| 99 | + } |
| 100 | + |
| 101 | + transformWorkspaceObjects(workspace: Workspace, workspaceList: NotifiedWorkspace[], deadlineField: string) { |
| 102 | + const wasNotified = workspaceList.find((nw) => nw.workspace.id === workspace.id)?.wasNotified ?? false |
| 103 | + const impendingActionDeadline = formatDistanceToNowStrict(new Date(deadlineField)) |
| 104 | + return { workspace, wasNotified, impendingActionDeadline } |
| 105 | + } |
| 106 | + |
| 107 | + async pollGetWorkspaces() { |
| 108 | + let errorCount = 0 |
| 109 | + this.#fetchWorkspacesInterval = setInterval(async () => { |
| 110 | + try { |
| 111 | + const workspacesResult = await getWorkspaces({ q: "owner:me" }) |
| 112 | + this.#ownedWorkspaces = workspacesResult.workspaces |
| 113 | + this.updateNotificationLists() |
| 114 | + this.notifyAll() |
| 115 | + } catch (error) { |
| 116 | + errorCount++ |
| 117 | + |
| 118 | + let status |
| 119 | + if (axios.isAxiosError(error)) { |
| 120 | + status = error.response?.status |
| 121 | + } |
| 122 | + if (status !== 401) { |
| 123 | + this.storage.writeToCoderOutputChannel( |
| 124 | + `Failed to poll owned workspaces. Some workspace notifications may be missing: ${error}`, |
| 125 | + ) |
| 126 | + } |
| 127 | + if (errorCount === 3) { |
| 128 | + clearInterval(this.#fetchWorkspacesInterval) |
| 129 | + } |
| 130 | + } |
| 131 | + }, this.#POLL_INTERVAL) |
| 132 | + } |
| 133 | + |
| 134 | + notifyAll() { |
| 135 | + this.notifyImpendingAutostop() |
| 136 | + this.notifyImpendingDeletion() |
| 137 | + } |
| 138 | + |
| 139 | + notifyImpendingAutostop() { |
| 140 | + this.#workspacesApproachingAutostop?.forEach((notifiedWorkspace: NotifiedWorkspace) => { |
| 141 | + if (notifiedWorkspace.wasNotified) { |
| 142 | + // don't message the user; we've already messaged |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + // we display individual notifications for each workspace as VS Code |
| 147 | + // intentionally strips new lines from the message text |
| 148 | + // https://github.com/Microsoft/vscode/issues/48900 |
| 149 | + this.vscodeProposed.window.showInformationMessage( |
| 150 | + `${notifiedWorkspace.workspace.name} is scheduled to shut down in ${notifiedWorkspace.impendingActionDeadline}.`, |
| 151 | + ) |
| 152 | + notifiedWorkspace.wasNotified = true |
| 153 | + }) |
| 154 | + } |
| 155 | + |
| 156 | + notifyImpendingDeletion() { |
| 157 | + this.#workspacesApproachingDeletion?.forEach((notifiedWorkspace: NotifiedWorkspace) => { |
| 158 | + if (notifiedWorkspace.wasNotified) { |
| 159 | + // don't message the user; we've already messaged |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + // we display individual notifications for each workspace as VS Code |
| 164 | + // intentionally strips new lines from the message text |
| 165 | + // https://github.com/Microsoft/vscode/issues/48900 |
| 166 | + this.vscodeProposed.window.showInformationMessage( |
| 167 | + `${notifiedWorkspace.workspace.name} is scheduled for deletion in ${notifiedWorkspace.impendingActionDeadline}.`, |
| 168 | + ) |
| 169 | + notifiedWorkspace.wasNotified = true |
| 170 | + }) |
| 171 | + } |
| 172 | + |
| 173 | + cleanupWorkspaceActions() { |
| 174 | + clearInterval(this.#fetchWorkspacesInterval) |
| 175 | + } |
| 176 | +} |
0 commit comments