@crombolo/server 0.71.0 → 0.73.0

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.
@@ -9,7 +9,14 @@
9
9
  */
10
10
  import { fileURLToPath } from 'node:url'
11
11
  import { Outcome, type OutcomeStatus } from '@crombolo/core'
12
- import type { DialectName, NodeSchemaState, WorkspaceScope } from '@crombolo/data'
12
+ import {
13
+ type Db,
14
+ type DialectName,
15
+ type NodeSchemaState,
16
+ WorkspaceProcessRepository,
17
+ WorkspaceRepository,
18
+ type WorkspaceScope,
19
+ } from '@crombolo/data'
13
20
  import type {
14
21
  PublishedCache,
15
22
  PublishedContent,
@@ -31,6 +38,8 @@ interface Running {
31
38
  waiting: Map<number, (answer: ProcessRender) => void>
32
39
  usedAt: number
33
40
  rss: number
41
+ /** Whether it said it was ready. */
42
+ started: boolean
34
43
  }
35
44
 
36
45
  export interface WorkspaceProcessOptions {
@@ -46,16 +55,28 @@ export interface WorkspaceProcessOptions {
46
55
  idleMs: number
47
56
  memoryMb: number
48
57
  renderMs: number
58
+ /** Story 396: told of each process started and stopped, and of each that failed. */
59
+ reports?: WorkspaceProcessReports
60
+ }
61
+
62
+ /** Where a node tells the site of its workspaces' processes (story 396). */
63
+ export interface WorkspaceProcessReports {
64
+ started(workspace: { key: string }): void
65
+ stopped(workspace: { key: string }): void
66
+ failed(workspace: { key: string; name: string }, reason: string): void
49
67
  }
50
68
 
51
69
  export function createWorkspaceProcesses(options: WorkspaceProcessOptions) {
52
70
  const running = new Map<string, Running>()
53
71
  let next = 1
54
72
 
55
- const stop = (key: string, why: string) => {
73
+ /** Stops a workspace's process; `failed` when it crashed, hung or never came up. */
74
+ const stop = (key: string, why: string, failed = false) => {
56
75
  const one = running.get(key)
57
76
  if (!one) return
58
77
  running.delete(key)
78
+ options.reports?.stopped(one.workspace)
79
+ if (failed) options.reports?.failed(one.workspace, why)
59
80
  for (const answer of one.waiting.values())
60
81
  answer({ type: 'rendered', id: 0, status: 'error', error: why })
61
82
  one.waiting.clear()
@@ -84,6 +105,7 @@ export function createWorkspaceProcesses(options: WorkspaceProcessOptions) {
84
105
  workspace,
85
106
  usedAt: Date.now(),
86
107
  rss: 0,
108
+ started: false,
87
109
  waiting: new Map(),
88
110
  ready: new Promise<void>((resolve) => {
89
111
  ready = resolve
@@ -94,17 +116,23 @@ export function createWorkspaceProcesses(options: WorkspaceProcessOptions) {
94
116
  stderr: 'ignore',
95
117
  env: { ...process.env },
96
118
  ipc(message: ChildAnswer) {
97
- if (message.type === 'ready') ready()
98
- else if (message.type === 'memory') one.rss = message.rss
119
+ if (message.type === 'ready') {
120
+ one.started = true
121
+ ready()
122
+ } else if (message.type === 'memory') one.rss = message.rss
99
123
  else {
100
124
  const answer = one.waiting.get(message.id)
101
125
  one.waiting.delete(message.id)
102
126
  answer?.(message)
103
127
  }
104
128
  },
105
- onExit() {
129
+ onExit(_proc, exitCode, signal) {
106
130
  if (running.get(workspace.key) === one)
107
- stop(workspace.key, `The workspace "${workspace.name}" stopped while it was rendering.`)
131
+ stop(
132
+ workspace.key,
133
+ `The workspace "${workspace.name}" stopped on its own (${signal ?? `exit code ${exitCode}`}).`,
134
+ true,
135
+ )
108
136
  },
109
137
  }),
110
138
  }
@@ -121,6 +149,7 @@ export function createWorkspaceProcesses(options: WorkspaceProcessOptions) {
121
149
  }
122
150
  one.proc.send(init)
123
151
  running.set(workspace.key, one)
152
+ options.reports?.started(workspace)
124
153
  sweep()
125
154
  return one
126
155
  }
@@ -137,10 +166,13 @@ export function createWorkspaceProcesses(options: WorkspaceProcessOptions) {
137
166
  const limit = new Promise<ProcessRender>((resolve) => {
138
167
  const t = setTimeout(() => {
139
168
  if (!one.waiting.has(id)) return
140
- // Over its time: this render is answered, and its process is stopped.
169
+ // Over its time: this render is answered, and its process is stopped, as failed.
141
170
  stop(
142
171
  workspace.key,
143
- `The workspace "${workspace.name}" took longer than ${Math.round(options.renderMs / 1000)} seconds to render this page.`,
172
+ one.started
173
+ ? `The workspace "${workspace.name}" took longer than ${Math.round(options.renderMs / 1000)} seconds to render this page.`
174
+ : `The workspace "${workspace.name}" did not start within ${Math.round(options.renderMs / 1000)} seconds.`,
175
+ true,
144
176
  )
145
177
  resolve({
146
178
  type: 'rendered',
@@ -232,6 +264,48 @@ export function createWorkspaceProcesses(options: WorkspaceProcessOptions) {
232
264
 
233
265
  export type WorkspaceProcesses = ReturnType<typeof createWorkspaceProcesses>
234
266
 
267
+ /** How many failures in the hour put a workspace on the list (story 396). */
268
+ export const PROCESS_FAILURES_REPORTED = 3
269
+
270
+ /** A node not seen within this long runs nothing, as far as the list goes. */
271
+ const NODE_SEEN_WITHIN_MS = 60_000
272
+
273
+ /**
274
+ * The workspaces with a process running on a live node, and those that failed three
275
+ * or more times in the last hour, with who to tell (story 396).
276
+ */
277
+ export async function workspaceProcessList(db: Db) {
278
+ const reports = await new WorkspaceProcessRepository(db).report({
279
+ seenWithinMs: NODE_SEEN_WITHIN_MS,
280
+ })
281
+ const workspaces = new WorkspaceRepository(db)
282
+ const out = []
283
+ for (const report of reports) {
284
+ const failing = report.failures >= PROCESS_FAILURES_REPORTED
285
+ if (!failing && report.nodes.length === 0) continue
286
+ const workspace = await workspaces.byKey(report.key)
287
+ const owner = workspace
288
+ ? await db.q
289
+ .selectFrom('user_account')
290
+ .select('user_name')
291
+ .where('key', '=', workspace.ownerKey)
292
+ .executeTakeFirst()
293
+ : undefined
294
+ out.push({
295
+ key: report.key,
296
+ name: workspace?.name ?? null,
297
+ owner: workspace ? { key: workspace.ownerKey, name: owner?.user_name ?? null } : null,
298
+ nodes: report.nodes,
299
+ running: report.nodes.length > 0,
300
+ failing,
301
+ failures: report.failures,
302
+ lastError: report.lastError,
303
+ since: report.since.toISOString(),
304
+ })
305
+ }
306
+ return out
307
+ }
308
+
235
309
  const MAX_ACCESS_SUBSTITUTIONS = 5
236
310
 
237
311
  /** The page to show in place of this one, as `Renderer` settles it; undefined for none. */