MCPcopy
hub / github.com/coder/mux / createCoderArchiveHook

Function createCoderArchiveHook

src/node/runtime/coderLifecycleHooks.ts:54–126  ·  view source on GitHub ↗
(options: {
  coderService: CoderService;
  getArchiveBehavior: () => CoderWorkspaceArchiveBehavior;
  timeoutMs?: number;
})

Source from the content-addressed store, hash-verified

52}
53
54export function createCoderArchiveHook(options: {
55 coderService: CoderService;
56 getArchiveBehavior: () => CoderWorkspaceArchiveBehavior;
57 timeoutMs?: number;
58}): BeforeArchiveHook {
59 const timeoutMs = options.timeoutMs ?? DEFAULT_STOP_TIMEOUT_MS;
60
61 return async ({ workspaceId, workspaceMetadata }): Promise<Result<void>> => {
62 const runtimeConfig = workspaceMetadata.runtimeConfig;
63 if (!isSSHRuntime(runtimeConfig) || !runtimeConfig.coder) {
64 return Ok(undefined);
65 }
66
67 const coder = runtimeConfig.coder;
68
69 // Important safety invariant:
70 // Only stop/delete Coder workspaces that mux created (dedicated workspaces). If the user
71 // connected mux to an existing Coder workspace, archiving in mux should *not* manage their
72 // environment.
73 if (coder.existingWorkspace === true) {
74 return Ok(undefined);
75 }
76
77 const workspaceName = coder.workspaceName?.trim();
78 if (!workspaceName) {
79 return Ok(undefined);
80 }
81
82 const archiveBehavior = options.getArchiveBehavior();
83 if (archiveBehavior === "keep") {
84 return Ok(undefined);
85 }
86
87 if (archiveBehavior === "delete") {
88 log.debug("Deleting Coder workspace before mux archive", {
89 workspaceId,
90 coderWorkspaceName: workspaceName,
91 });
92
93 try {
94 await options.coderService.deleteWorkspace(workspaceName);
95 return Ok(undefined);
96 } catch (error) {
97 return Err(
98 `Failed to delete Coder workspace "${workspaceName}": ${getErrorMessage(error)}`
99 );
100 }
101 }
102
103 // Best-effort: skip the stop call if the control-plane already thinks the workspace is down.
104 const status = await options.coderService.getWorkspaceStatus(workspaceName, {
105 timeoutMs: DEFAULT_STATUS_TIMEOUT_MS,
106 });
107
108 if (isAlreadyStoppedOrGone(status)) {
109 return Ok(undefined);
110 }
111

Callers 2

constructorMethod · 0.90

Calls 10

isSSHRuntimeFunction · 0.90
OkFunction · 0.90
ErrFunction · 0.90
getErrorMessageFunction · 0.90
isAlreadyStoppedOrGoneFunction · 0.85
getArchiveBehaviorMethod · 0.80
debugMethod · 0.80
getWorkspaceStatusMethod · 0.80
stopWorkspaceMethod · 0.80
deleteWorkspaceMethod · 0.65

Tested by

no test coverage detected