|
| 1 | +importaxiosfrom"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*asvscodefrom"vscode" |
| 6 | +import{Storage}from"./storage" |
| 7 | + |
| 8 | +interfaceNotifiedWorkspace{ |
| 9 | +workspace:Workspace |
| 10 | +wasNotified:boolean |
| 11 | +impendingActionDeadline:string |
| 12 | +} |
| 13 | + |
| 14 | +typeWithRequired<T,KextendskeyofT>=T&Required<Pick<T,K>> |
| 15 | + |
| 16 | +typeWorkspaceWithDeadline=Workspace&{latest_build:WithRequired<WorkspaceBuild,"deadline">} |
| 17 | +typeWorkspaceWithDeletingAt=WithRequired<Workspace,"deleting_at"> |
| 18 | + |
| 19 | +exportclassWorkspaceAction{ |
| 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<typeofsetInterval> |
| 23 | + |
| 24 | + #ownedWorkspaces:Workspace[]=[] |
| 25 | + #workspacesApproachingAutostop:NotifiedWorkspace[]=[] |
| 26 | + #workspacesApproachingDeletion:NotifiedWorkspace[]=[] |
| 27 | + |
| 28 | +privateconstructor( |
| 29 | +privatereadonlyvscodeProposed:typeofvscode, |
| 30 | +privatereadonlystorage: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 | +staticasyncinit(vscodeProposed:typeofvscode,storage:Storage){ |
| 45 | +// fetch all workspaces owned by the user and set initial public class fields |
| 46 | +letownedWorkspacesResponse:WorkspacesResponse |
| 47 | +try{ |
| 48 | +ownedWorkspacesResponse=awaitgetWorkspaces({q:"owner:me"}) |
| 49 | +}catch(error){ |
| 50 | +letstatus |
| 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 | +returnnewWorkspaceAction(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 isWorkspaceWithDeadline{ |
| 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 | +returnfalse |
| 83 | +} |
| 84 | + |
| 85 | +consthourMilli=1000*60*60 |
| 86 | +// return workspaces with a deadline that is in 1 hr or less |
| 87 | +returnMath.abs(newDate().getTime()-newDate(workspace.latest_build.deadline).getTime())<=hourMilli |
| 88 | +} |
| 89 | + |
| 90 | +filterWorkspacesImpendingDeletion(workspace:Workspace):workspace isWorkspaceWithDeletingAt{ |
| 91 | +if(!workspace.deleting_at){ |
| 92 | +returnfalse |
| 93 | +} |
| 94 | + |
| 95 | +constdayMilli=1000*60*60*24 |
| 96 | + |
| 97 | +// return workspaces with a deleting_at that is 24 hrs or less |
| 98 | +returnMath.abs(newDate().getTime()-newDate(workspace.deleting_at).getTime())<=dayMilli |
| 99 | +} |
| 100 | + |
| 101 | +transformWorkspaceObjects(workspace:Workspace,workspaceList:NotifiedWorkspace[],deadlineField:string){ |
| 102 | +constwasNotified=workspaceList.find((nw)=>nw.workspace.id===workspace.id)?.wasNotified??false |
| 103 | +constimpendingActionDeadline=formatDistanceToNowStrict(newDate(deadlineField)) |
| 104 | +return{ workspace, wasNotified, impendingActionDeadline} |
| 105 | +} |
| 106 | + |
| 107 | +asyncpollGetWorkspaces(){ |
| 108 | +leterrorCount=0 |
| 109 | +this.#fetchWorkspacesInterval=setInterval(async()=>{ |
| 110 | +try{ |
| 111 | +constworkspacesResult=awaitgetWorkspaces({q:"owner:me"}) |
| 112 | +this.#ownedWorkspaces=workspacesResult.workspaces |
| 113 | +this.updateNotificationLists() |
| 114 | +this.notifyAll() |
| 115 | +}catch(error){ |
| 116 | +errorCount++ |
| 117 | + |
| 118 | +letstatus |
| 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 | +} |