@sixb/deploy-ssh 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/render.ts CHANGED
@@ -1,34 +1,79 @@
1
- import type { DeployRelease } from "@sixb/core/deploy"
2
- import type { DeploymentLayout } from "./layout"
1
+ import type { DeployHttpServiceName, DeployRelease, DeployReleaseService } from "@sixb/core/deploy"
2
+ import type { DeploymentLayout, DeploySlot } from "./layout"
3
3
  import type { ProcessManifest } from "./server/manifest"
4
+ import { INTERRUPT_LEAD_MS, retiringKillTimeoutMs } from "./server/supervisor"
4
5
  import { shellQuote } from "./shell"
5
6
  import { renderUpdatePage } from "./update-page"
6
7
 
7
8
  const GENERATED = "Generated by sixb deploy. Changes are replaced on the next deploy."
8
9
 
10
+ export type SlotPorts = Readonly<
11
+ Record<DeploySlot, Readonly<Record<DeployHttpServiceName, number>>>
12
+ >
13
+
14
+ /** Rewrites a release's HTTP services to listen on the given slot's loopback ports. */
15
+ export function releaseForSlot(
16
+ release: DeployRelease,
17
+ ports: Readonly<Record<DeployHttpServiceName, number>>
18
+ ): DeployRelease {
19
+ return {
20
+ ...release,
21
+ services: release.services.map((service) => {
22
+ if (!service.http || (service.name !== "api" && service.name !== "app")) {
23
+ return service
24
+ }
25
+ const port = ports[service.name]
26
+ return {
27
+ ...service,
28
+ command: {
29
+ ...service.command,
30
+ args: replacePortArg(service.command.args, port),
31
+ },
32
+ http: {
33
+ ...service.http,
34
+ port,
35
+ },
36
+ }
37
+ }),
38
+ }
39
+ }
40
+
41
+ function replacePortArg(args: readonly string[], port: number): string[] {
42
+ const next = [...args]
43
+ const index = next.indexOf("--port")
44
+ if (index !== -1 && index + 1 < next.length) {
45
+ next[index + 1] = String(port)
46
+ }
47
+ return next
48
+ }
49
+
9
50
  /** The supervisor's view of the release: absolute commands, full environments. */
10
51
  export function renderProcessManifest(
11
52
  release: DeployRelease,
12
- layout: DeploymentLayout
53
+ layout: DeploymentLayout,
54
+ options: { readonly projectDir?: string; readonly slot?: DeploySlot } = {}
13
55
  ): ProcessManifest {
14
- const sixb = `${layout.project}/node_modules/.bin/sixb`
56
+ const projectDir = options.projectDir ?? layout.project
57
+ const sixb = `${projectDir}/node_modules/.bin/sixb`
15
58
  return {
16
59
  version: 1,
17
60
  name: release.name,
18
- cwd: layout.project,
61
+ ...(options.slot ? { slot: options.slot } : {}),
62
+ cwd: projectDir,
19
63
  runDir: layout.runDir,
20
64
  logDir: layout.logDir,
21
65
  releasePath: layout.releaseRecord,
22
66
  env: release.env,
23
67
  services: release.services.map((service) => ({
24
68
  name: service.name,
69
+ kind: service.kind,
25
70
  processName: `${release.name}-${service.name}`,
26
71
  command: layout.bun,
27
72
  args:
28
73
  service.command.program === "sixb"
29
74
  ? [sixb, ...service.command.args]
30
75
  : [...service.command.args],
31
- env: service.env,
76
+ env: serviceEnvWithDrain(service),
32
77
  instances: service.instances,
33
78
  killTimeoutMs: service.process.killTimeoutMs,
34
79
  restartDelayMs: service.process.restartDelayMs,
@@ -37,6 +82,25 @@ export function renderProcessManifest(
37
82
  }
38
83
  }
39
84
 
85
+ /**
86
+ * A retiring `api` or `workers` drains until the supervisor's SIGINT, so it finishes its work on
87
+ * its own rather than being cut short. The window follows the service's own kill timeout.
88
+ */
89
+ function serviceEnvWithDrain(service: DeployReleaseService): Record<string, string> {
90
+ if (
91
+ (service.name === "api" || service.name === "workers") &&
92
+ service.env.SIXB_DRAIN_TIMEOUT_MS === undefined
93
+ ) {
94
+ const drainTimeoutMs = retiringKillTimeoutMs(service.process.killTimeoutMs) - INTERRUPT_LEAD_MS
95
+
96
+ return {
97
+ SIXB_DRAIN_TIMEOUT_MS: String(drainTimeoutMs),
98
+ ...service.env,
99
+ }
100
+ }
101
+ return { ...service.env }
102
+ }
103
+
40
104
  /** Pins the server half of this package to the project's Bun and manifest. */
41
105
  export function renderCtl(layout: DeploymentLayout): string {
42
106
  return [
@@ -78,7 +142,7 @@ export function renderUnit(release: DeployRelease, layout: DeploymentLayout): st
78
142
  ].join("\n")
79
143
  }
80
144
 
81
- /** One site per HTTP service, forwarded to the service's loopback port. */
145
+ /** One site per HTTP service, forwarded to the active slot's loopback port. */
82
146
  export function renderCaddySnippet(release: DeployRelease): string {
83
147
  const sites = release.services.flatMap((service) => {
84
148
  if (!service.http) return []
@@ -87,7 +151,10 @@ export function renderCaddySnippet(release: DeployRelease): string {
87
151
  [
88
152
  `${service.http.domain} {`,
89
153
  ...(service.name === "api" ? compressJson() : []),
90
- `\treverse_proxy ${service.http.host}:${service.http.port}`,
154
+ `\treverse_proxy ${service.http.host}:${service.http.port} {`,
155
+ "\t\tlb_try_duration 5s",
156
+ ...(service.name === "api" ? keepWebSocketsThroughReload(service) : []),
157
+ "\t}",
91
158
  ...(pages ? updatePageOnProxyErrors() : []),
92
159
  "}",
93
160
  ].join("\n"),
@@ -96,6 +163,17 @@ export function renderCaddySnippet(release: DeployRelease): string {
96
163
  return [`# ${GENERATED}`, "", sites.join("\n\n"), ""].join("\n")
97
164
  }
98
165
 
166
+ /**
167
+ * A reload closes every WebSocket Caddy proxies unless it is told to wait, and each deploy reloads
168
+ * Caddy before the old API retires. Kept open, a browser's socket stays on the retiring API, which
169
+ * closes it with 1012 so the page reconnects without showing a drop. The wait covers the API's
170
+ * whole retiring window; the API closes its sockets as soon as it starts draining.
171
+ */
172
+ function keepWebSocketsThroughReload(service: DeployReleaseService): string[] {
173
+ const seconds = retiringKillTimeoutMs(service.process.killTimeoutMs) / 1000
174
+ return [`\t\tstream_close_delay ${seconds}s`]
175
+ }
176
+
99
177
  /**
100
178
  * The API never compresses its responses, and an object query page is easily a megabyte of
101
179
  * JSON that compresses about twentyfold. Only complete JSON answers are encoded: the
@@ -117,8 +195,8 @@ function compressJson(): string[] {
117
195
  }
118
196
 
119
197
  /**
120
- * While the app is down — during a deploy, or an outage — a browser asking for a page
121
- * gets the update page instead of Caddy's bare error. Other requests keep the error.
198
+ * While the app is down — during an outage — a browser asking for a page gets the update page
199
+ * instead of Caddy's bare error. Other requests keep the error.
122
200
  */
123
201
  function updatePageOnProxyErrors(): string[] {
124
202
  return [