@sixb/cli 0.0.1

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.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +275 -0
  3. package/dist/commands/access-token-utils.d.ts +9 -0
  4. package/dist/commands/access-token-utils.d.ts.map +1 -0
  5. package/dist/commands/api.d.ts +13 -0
  6. package/dist/commands/api.d.ts.map +1 -0
  7. package/dist/commands/app.d.ts +9 -0
  8. package/dist/commands/app.d.ts.map +1 -0
  9. package/dist/commands/atlas.d.ts +9 -0
  10. package/dist/commands/atlas.d.ts.map +1 -0
  11. package/dist/commands/auth.d.ts +8 -0
  12. package/dist/commands/auth.d.ts.map +1 -0
  13. package/dist/commands/build.d.ts +6 -0
  14. package/dist/commands/build.d.ts.map +1 -0
  15. package/dist/commands/check.d.ts +5 -0
  16. package/dist/commands/check.d.ts.map +1 -0
  17. package/dist/commands/db-migrate.d.ts +5 -0
  18. package/dist/commands/db-migrate.d.ts.map +1 -0
  19. package/dist/commands/dev.d.ts +12 -0
  20. package/dist/commands/dev.d.ts.map +1 -0
  21. package/dist/commands/init.d.ts +3 -0
  22. package/dist/commands/init.d.ts.map +1 -0
  23. package/dist/commands/lake-check.d.ts +5 -0
  24. package/dist/commands/lake-check.d.ts.map +1 -0
  25. package/dist/commands/lake-cleanup.d.ts +16 -0
  26. package/dist/commands/lake-cleanup.d.ts.map +1 -0
  27. package/dist/commands/orchestrator.d.ts +6 -0
  28. package/dist/commands/orchestrator.d.ts.map +1 -0
  29. package/dist/commands/rules.d.ts +6 -0
  30. package/dist/commands/rules.d.ts.map +1 -0
  31. package/dist/commands/scheduler.d.ts +6 -0
  32. package/dist/commands/scheduler.d.ts.map +1 -0
  33. package/dist/commands/service-account.d.ts +14 -0
  34. package/dist/commands/service-account.d.ts.map +1 -0
  35. package/dist/commands/token.d.ts +14 -0
  36. package/dist/commands/token.d.ts.map +1 -0
  37. package/dist/commands/typegen.d.ts +5 -0
  38. package/dist/commands/typegen.d.ts.map +1 -0
  39. package/dist/commands/worker-group.d.ts +8 -0
  40. package/dist/commands/worker-group.d.ts.map +1 -0
  41. package/dist/commands/worker.d.ts +8 -0
  42. package/dist/commands/worker.d.ts.map +1 -0
  43. package/dist/index.d.ts +3 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/lib/api-client.d.ts +26 -0
  46. package/dist/lib/api-client.d.ts.map +1 -0
  47. package/dist/lib/browser-topology.d.ts +62 -0
  48. package/dist/lib/browser-topology.d.ts.map +1 -0
  49. package/dist/lib/errors.d.ts +19 -0
  50. package/dist/lib/errors.d.ts.map +1 -0
  51. package/dist/lib/health.d.ts +25 -0
  52. package/dist/lib/health.d.ts.map +1 -0
  53. package/dist/lib/loadSixb.d.ts +49 -0
  54. package/dist/lib/loadSixb.d.ts.map +1 -0
  55. package/dist/lib/production-roles.d.ts +65 -0
  56. package/dist/lib/production-roles.d.ts.map +1 -0
  57. package/dist/lib/production.d.ts +29 -0
  58. package/dist/lib/production.d.ts.map +1 -0
  59. package/dist/lib/public-origin.d.ts +17 -0
  60. package/dist/lib/public-origin.d.ts.map +1 -0
  61. package/dist/lib/runtime.d.ts +76 -0
  62. package/dist/lib/runtime.d.ts.map +1 -0
  63. package/dist/lib/shareable-providers.d.ts +20 -0
  64. package/dist/lib/shareable-providers.d.ts.map +1 -0
  65. package/dist/lib/storage-migration.d.ts +40 -0
  66. package/dist/lib/storage-migration.d.ts.map +1 -0
  67. package/dist/lib/typegen.d.ts +5 -0
  68. package/dist/lib/typegen.d.ts.map +1 -0
  69. package/dist/lib/worker-registry.d.ts +30 -0
  70. package/dist/lib/worker-registry.d.ts.map +1 -0
  71. package/dist/tsconfig.json +5 -0
  72. package/dist/ui/index.d.ts +164 -0
  73. package/dist/ui/index.d.ts.map +1 -0
  74. package/package.json +68 -0
  75. package/src/commands/access-token-utils.ts +63 -0
  76. package/src/commands/api.tsx +111 -0
  77. package/src/commands/app.tsx +91 -0
  78. package/src/commands/atlas.tsx +75 -0
  79. package/src/commands/auth.tsx +96 -0
  80. package/src/commands/build.tsx +90 -0
  81. package/src/commands/check.tsx +58 -0
  82. package/src/commands/db-migrate.tsx +27 -0
  83. package/src/commands/dev.tsx +141 -0
  84. package/src/commands/init.tsx +18 -0
  85. package/src/commands/lake-check.tsx +20 -0
  86. package/src/commands/lake-cleanup.tsx +63 -0
  87. package/src/commands/orchestrator.tsx +87 -0
  88. package/src/commands/rules.tsx +81 -0
  89. package/src/commands/scheduler.tsx +77 -0
  90. package/src/commands/service-account.tsx +304 -0
  91. package/src/commands/token.tsx +143 -0
  92. package/src/commands/typegen.tsx +18 -0
  93. package/src/commands/worker-group.tsx +120 -0
  94. package/src/commands/worker.tsx +96 -0
  95. package/src/index.tsx +390 -0
  96. package/src/lib/api-client.ts +132 -0
  97. package/src/lib/browser-topology.ts +292 -0
  98. package/src/lib/errors.ts +40 -0
  99. package/src/lib/health.ts +138 -0
  100. package/src/lib/loadSixb.ts +175 -0
  101. package/src/lib/production-roles.ts +48 -0
  102. package/src/lib/production.ts +104 -0
  103. package/src/lib/public-origin.ts +46 -0
  104. package/src/lib/runtime.ts +346 -0
  105. package/src/lib/shareable-providers.ts +67 -0
  106. package/src/lib/storage-migration.ts +120 -0
  107. package/src/lib/typegen.ts +12 -0
  108. package/src/lib/worker-registry.ts +191 -0
  109. package/src/ui/index.tsx +953 -0
@@ -0,0 +1,304 @@
1
+ import {
2
+ createAuthServiceAccount,
3
+ createAuthServiceAccountAccessToken,
4
+ disableAuthServiceAccount,
5
+ type ListAuthServiceAccountsResponse,
6
+ listAuthServiceAccountAccessTokens,
7
+ listAuthServiceAccounts,
8
+ revokeAuthServiceAccountAccessToken,
9
+ } from "@sixb/client"
10
+ import { createCliSixbClient, resolveApiClientConfig, unwrapSixbApiResult } from "../lib/api-client"
11
+ import { KeyValueResultView, renderStatic, SecretResultView, TableResultView } from "../ui"
12
+ import {
13
+ formatDate,
14
+ formatGroups,
15
+ normalizeGroupIds,
16
+ resolveExpiration,
17
+ } from "./access-token-utils"
18
+
19
+ type ServiceAccount = ListAuthServiceAccountsResponse["serviceAccounts"][number]
20
+
21
+ export interface ServiceAccountCommandOptions {
22
+ readonly positionals?: readonly string[]
23
+ readonly apiUrl?: string
24
+ readonly token?: string
25
+ readonly id?: string
26
+ readonly name?: string
27
+ readonly description?: string
28
+ readonly expiresAt?: string
29
+ readonly expiresIn?: string
30
+ readonly groupIds?: readonly string[]
31
+ readonly json?: boolean
32
+ }
33
+
34
+ export async function runServiceAccount(options: ServiceAccountCommandOptions = {}) {
35
+ const command = options.positionals?.[0] ?? "list"
36
+ if (command === "token") {
37
+ await runServiceAccountToken(options)
38
+ return
39
+ }
40
+
41
+ if (command === "list") {
42
+ await listServiceAccounts(options)
43
+ return
44
+ }
45
+
46
+ if (command === "create") {
47
+ await createServiceAccount(options)
48
+ return
49
+ }
50
+
51
+ if (command === "disable") {
52
+ await disableServiceAccount(options)
53
+ return
54
+ }
55
+
56
+ throw new Error("Usage: sixb service-account <list|create|disable|token>")
57
+ }
58
+
59
+ async function listServiceAccounts(options: ServiceAccountCommandOptions) {
60
+ const client = createCliSixbClient(resolveApiClientConfig(options))
61
+ const result = unwrapSixbApiResult(await listAuthServiceAccounts({ client }))
62
+
63
+ if (options.json) {
64
+ console.log(JSON.stringify(result, null, 2))
65
+ return
66
+ }
67
+
68
+ await renderStatic(
69
+ <TableResultView
70
+ title="Service accounts"
71
+ subtitle={`${result.serviceAccounts.length} visible`}
72
+ headers={["Name", "ID", "Status", "Groups", "Updated"]}
73
+ rows={result.serviceAccounts.map(serviceAccountRow)}
74
+ emptyMessage="No service accounts."
75
+ />
76
+ )
77
+ }
78
+
79
+ async function createServiceAccount(options: ServiceAccountCommandOptions) {
80
+ const name = options.name?.trim() || options.positionals?.[1]?.trim()
81
+ if (!name) {
82
+ throw new Error("Usage: sixb service-account create --name <name> [--id <id>] [--group <id>]")
83
+ }
84
+
85
+ const id = options.id?.trim()
86
+ const description = options.description?.trim()
87
+ const groupIds = normalizeGroupIds(options.groupIds)
88
+ const client = createCliSixbClient(resolveApiClientConfig(options))
89
+ const result = unwrapSixbApiResult(
90
+ await createAuthServiceAccount({
91
+ client,
92
+ body: {
93
+ ...(id ? { id } : {}),
94
+ name,
95
+ ...(description ? { description } : {}),
96
+ ...(groupIds.length > 0 ? { groupIds } : {}),
97
+ },
98
+ })
99
+ )
100
+
101
+ if (options.json) {
102
+ console.log(JSON.stringify(result, null, 2))
103
+ return
104
+ }
105
+
106
+ await renderStatic(
107
+ <KeyValueResultView
108
+ title="Created service account"
109
+ items={[
110
+ { label: "Name", value: result.serviceAccount.name },
111
+ { label: "ID", value: result.serviceAccount.id },
112
+ { label: "Status", value: result.serviceAccount.status },
113
+ { label: "Groups", value: formatGroups(result.serviceAccount.groupIds) },
114
+ ]}
115
+ />
116
+ )
117
+ }
118
+
119
+ async function disableServiceAccount(options: ServiceAccountCommandOptions) {
120
+ const serviceAccountId = options.id?.trim() || options.positionals?.[1]?.trim()
121
+ if (!serviceAccountId) {
122
+ throw new Error("Usage: sixb service-account disable <service-account-id>")
123
+ }
124
+
125
+ const client = createCliSixbClient(resolveApiClientConfig(options))
126
+ const result = unwrapSixbApiResult(
127
+ await disableAuthServiceAccount({
128
+ client,
129
+ path: { serviceAccountId },
130
+ })
131
+ )
132
+
133
+ if (options.json) {
134
+ console.log(JSON.stringify(result, null, 2))
135
+ return
136
+ }
137
+
138
+ await renderStatic(
139
+ <KeyValueResultView
140
+ title="Disabled service account"
141
+ items={[
142
+ { label: "Name", value: result.serviceAccount.name },
143
+ { label: "ID", value: result.serviceAccount.id },
144
+ { label: "Status", value: result.serviceAccount.status },
145
+ ]}
146
+ />
147
+ )
148
+ }
149
+
150
+ async function runServiceAccountToken(options: ServiceAccountCommandOptions) {
151
+ const action = options.positionals?.[1] ?? "list"
152
+ if (action === "list") {
153
+ await listServiceAccountTokens(options)
154
+ return
155
+ }
156
+
157
+ if (action === "create") {
158
+ await createServiceAccountToken(options)
159
+ return
160
+ }
161
+
162
+ if (action === "revoke") {
163
+ await revokeServiceAccountToken(options)
164
+ return
165
+ }
166
+
167
+ throw new Error("Usage: sixb service-account token <list|create|revoke> <service-account-id>")
168
+ }
169
+
170
+ async function listServiceAccountTokens(options: ServiceAccountCommandOptions) {
171
+ const serviceAccountId = requireServiceAccountId(
172
+ options,
173
+ "Usage: sixb service-account token list <service-account-id>"
174
+ )
175
+ const client = createCliSixbClient(resolveApiClientConfig(options))
176
+ const result = unwrapSixbApiResult(
177
+ await listAuthServiceAccountAccessTokens({
178
+ client,
179
+ path: { serviceAccountId },
180
+ })
181
+ )
182
+
183
+ if (options.json) {
184
+ console.log(JSON.stringify(result, null, 2))
185
+ return
186
+ }
187
+
188
+ await renderStatic(
189
+ <TableResultView
190
+ title="Service-account tokens"
191
+ subtitle={serviceAccountId}
192
+ headers={["Name", "ID", "Status", "Groups", "Last used", "Expires"]}
193
+ rows={result.accessTokens.map((token) => [
194
+ token.name,
195
+ token.id,
196
+ token.status,
197
+ formatGroups(token.groupIds),
198
+ formatDate(token.lastUsedAt),
199
+ formatDate(token.expiresAt),
200
+ ])}
201
+ emptyMessage="No service-account tokens."
202
+ />
203
+ )
204
+ }
205
+
206
+ async function createServiceAccountToken(options: ServiceAccountCommandOptions) {
207
+ const serviceAccountId = requireServiceAccountId(
208
+ options,
209
+ "Usage: sixb service-account token create <service-account-id> --name <name>"
210
+ )
211
+ const name = options.name?.trim() || options.positionals?.[3]?.trim()
212
+ if (!name) {
213
+ throw new Error("Usage: sixb service-account token create <service-account-id> --name <name>")
214
+ }
215
+
216
+ const expiresAt = resolveExpiration(options)
217
+ const groupIds = normalizeGroupIds(options.groupIds)
218
+ const client = createCliSixbClient(resolveApiClientConfig(options))
219
+ const result = unwrapSixbApiResult(
220
+ await createAuthServiceAccountAccessToken({
221
+ client,
222
+ path: { serviceAccountId },
223
+ body: {
224
+ name,
225
+ expiresAt,
226
+ ...(groupIds.length > 0 ? { groupIds } : {}),
227
+ },
228
+ })
229
+ )
230
+
231
+ if (options.json) {
232
+ console.log(JSON.stringify(result, null, 2))
233
+ return
234
+ }
235
+
236
+ await renderStatic(
237
+ <SecretResultView
238
+ title="Created service-account token"
239
+ subtitle={serviceAccountId}
240
+ items={[
241
+ { label: "Name", value: result.accessToken.name },
242
+ { label: "ID", value: result.accessToken.id },
243
+ { label: "Expires", value: formatDate(result.accessToken.expiresAt) },
244
+ { label: "Groups", value: formatGroups(result.accessToken.groupIds) },
245
+ ]}
246
+ secret={result.tokenValue}
247
+ />
248
+ )
249
+ }
250
+
251
+ async function revokeServiceAccountToken(options: ServiceAccountCommandOptions) {
252
+ const serviceAccountId = requireServiceAccountId(
253
+ options,
254
+ "Usage: sixb service-account token revoke <service-account-id> <token-id>"
255
+ )
256
+ const tokenId = options.id?.trim() || options.positionals?.[3]?.trim()
257
+ if (!tokenId) {
258
+ throw new Error("Usage: sixb service-account token revoke <service-account-id> <token-id>")
259
+ }
260
+
261
+ const client = createCliSixbClient(resolveApiClientConfig(options))
262
+ const result = unwrapSixbApiResult(
263
+ await revokeAuthServiceAccountAccessToken({
264
+ client,
265
+ path: { serviceAccountId, tokenId },
266
+ })
267
+ )
268
+
269
+ if (options.json) {
270
+ console.log(JSON.stringify(result, null, 2))
271
+ return
272
+ }
273
+
274
+ await renderStatic(
275
+ <KeyValueResultView
276
+ title="Revoked service-account token"
277
+ subtitle={serviceAccountId}
278
+ items={[
279
+ { label: "Name", value: result.accessToken.name },
280
+ { label: "ID", value: result.accessToken.id },
281
+ { label: "Status", value: result.accessToken.status },
282
+ ]}
283
+ />
284
+ )
285
+ }
286
+
287
+ function requireServiceAccountId(options: ServiceAccountCommandOptions, usage: string): string {
288
+ const serviceAccountId = options.positionals?.[2]?.trim()
289
+ if (!serviceAccountId) {
290
+ throw new Error(usage)
291
+ }
292
+
293
+ return serviceAccountId
294
+ }
295
+
296
+ function serviceAccountRow(serviceAccount: ServiceAccount): readonly string[] {
297
+ return [
298
+ serviceAccount.name,
299
+ serviceAccount.id,
300
+ serviceAccount.status,
301
+ formatGroups(serviceAccount.groupIds),
302
+ formatDate(serviceAccount.updatedAt),
303
+ ]
304
+ }
@@ -0,0 +1,143 @@
1
+ import {
2
+ createAuthPersonalAccessToken,
3
+ listAuthAccessTokens,
4
+ revokeAuthAccessToken,
5
+ } from "@sixb/client"
6
+ import { createCliSixbClient, resolveApiClientConfig, unwrapSixbApiResult } from "../lib/api-client"
7
+ import { KeyValueResultView, renderStatic, SecretResultView, TableResultView } from "../ui"
8
+ import {
9
+ formatDate,
10
+ formatGroups,
11
+ normalizeGroupIds,
12
+ resolveExpiration,
13
+ } from "./access-token-utils"
14
+
15
+ export interface TokenCommandOptions {
16
+ readonly action?: string
17
+ readonly positionals?: readonly string[]
18
+ readonly apiUrl?: string
19
+ readonly token?: string
20
+ readonly id?: string
21
+ readonly name?: string
22
+ readonly expiresAt?: string
23
+ readonly expiresIn?: string
24
+ readonly groupIds?: readonly string[]
25
+ readonly json?: boolean
26
+ }
27
+
28
+ export async function runToken(options: TokenCommandOptions = {}) {
29
+ const action = options.action ?? "list"
30
+ if (action === "list") {
31
+ await listTokens(options)
32
+ return
33
+ }
34
+
35
+ if (action === "create") {
36
+ await createToken(options)
37
+ return
38
+ }
39
+
40
+ if (action === "revoke") {
41
+ await revokeToken(options)
42
+ return
43
+ }
44
+
45
+ throw new Error("Usage: sixb token <list|create|revoke>")
46
+ }
47
+
48
+ async function listTokens(options: TokenCommandOptions) {
49
+ const client = createCliSixbClient(resolveApiClientConfig(options))
50
+ const result = unwrapSixbApiResult(await listAuthAccessTokens({ client }))
51
+
52
+ if (options.json) {
53
+ console.log(JSON.stringify(result, null, 2))
54
+ return
55
+ }
56
+
57
+ await renderStatic(
58
+ <TableResultView
59
+ title="Personal access tokens"
60
+ subtitle={`${result.accessTokens.length} visible`}
61
+ headers={["Name", "ID", "Status", "Groups", "Last used", "Expires"]}
62
+ rows={result.accessTokens.map((token) => [
63
+ token.name,
64
+ token.id,
65
+ token.status,
66
+ formatGroups(token.groupIds),
67
+ formatDate(token.lastUsedAt),
68
+ formatDate(token.expiresAt),
69
+ ])}
70
+ emptyMessage="No personal access tokens."
71
+ />
72
+ )
73
+ }
74
+
75
+ async function createToken(options: TokenCommandOptions) {
76
+ const name = options.name?.trim() || options.positionals?.[1]?.trim()
77
+ if (!name) {
78
+ throw new Error("Usage: sixb token create --name <name> [--expires-in 90d] [--group <id>]")
79
+ }
80
+
81
+ const expiresAt = resolveExpiration(options)
82
+ const groupIds = normalizeGroupIds(options.groupIds)
83
+ const client = createCliSixbClient(resolveApiClientConfig(options))
84
+ const result = unwrapSixbApiResult(
85
+ await createAuthPersonalAccessToken({
86
+ client,
87
+ body: {
88
+ name,
89
+ expiresAt,
90
+ ...(groupIds.length > 0 ? { groupIds } : {}),
91
+ },
92
+ })
93
+ )
94
+
95
+ if (options.json) {
96
+ console.log(JSON.stringify(result, null, 2))
97
+ return
98
+ }
99
+
100
+ await renderStatic(
101
+ <SecretResultView
102
+ title="Created personal access token"
103
+ items={[
104
+ { label: "Name", value: result.accessToken.name },
105
+ { label: "ID", value: result.accessToken.id },
106
+ { label: "Expires", value: formatDate(result.accessToken.expiresAt) },
107
+ { label: "Groups", value: formatGroups(result.accessToken.groupIds) },
108
+ ]}
109
+ secret={result.tokenValue}
110
+ />
111
+ )
112
+ }
113
+
114
+ async function revokeToken(options: TokenCommandOptions) {
115
+ const tokenId = options.id?.trim() || options.positionals?.[1]?.trim()
116
+ if (!tokenId) {
117
+ throw new Error("Usage: sixb token revoke <token-id>")
118
+ }
119
+
120
+ const client = createCliSixbClient(resolveApiClientConfig(options))
121
+ const result = unwrapSixbApiResult(
122
+ await revokeAuthAccessToken({
123
+ client,
124
+ path: { tokenId },
125
+ })
126
+ )
127
+
128
+ if (options.json) {
129
+ console.log(JSON.stringify(result, null, 2))
130
+ return
131
+ }
132
+
133
+ await renderStatic(
134
+ <KeyValueResultView
135
+ title="Revoked personal access token"
136
+ items={[
137
+ { label: "Name", value: result.accessToken.name },
138
+ { label: "ID", value: result.accessToken.id },
139
+ { label: "Status", value: result.accessToken.status },
140
+ ]}
141
+ />
142
+ )
143
+ }
@@ -0,0 +1,18 @@
1
+ import { generateProjectTypes } from "../lib/typegen"
2
+ import { renderStatic, TypegenView } from "../ui"
3
+
4
+ export interface TypegenOptions {
5
+ entry?: string
6
+ }
7
+
8
+ export async function runTypegen(options: TypegenOptions = {}) {
9
+ const result = await generateProjectTypes(options)
10
+ await renderStatic(
11
+ <TypegenView
12
+ path={result.path}
13
+ objectTypes={result.entries.length}
14
+ skipped={result.skipped}
15
+ written={result.written}
16
+ />
17
+ )
18
+ }
@@ -0,0 +1,120 @@
1
+ import type { Worker } from "@sixb/core/internal/workers"
2
+ import { type LoadedSixb, loadSixbFromEntry } from "../lib/loadSixb"
3
+ import { resolveRuntimeEntry } from "../lib/production"
4
+ import {
5
+ runUntilSignal,
6
+ stopQuietly,
7
+ stopSixbProviders,
8
+ waitForWorkerFailure,
9
+ } from "../lib/runtime"
10
+ import { assertShareableProviders } from "../lib/shareable-providers"
11
+ import { migrateStorageForRole } from "../lib/storage-migration"
12
+ import {
13
+ assertWorkerInputs,
14
+ createWorkerForType,
15
+ resolveRegisteredWorkerTypes,
16
+ resolveWorkerTypeToStart,
17
+ } from "../lib/worker-registry"
18
+ import { LoadingView, renderCliError, renderPersistent, WorkerGroupView } from "../ui"
19
+
20
+ export interface WorkerGroupOptions {
21
+ entry?: string
22
+ noMigrate?: boolean
23
+ workerTypes?: readonly string[]
24
+ apiPublicOrigin?: string
25
+ }
26
+
27
+ export async function runWorkerGroup(options: WorkerGroupOptions = {}) {
28
+ process.env.NODE_ENV = "production"
29
+
30
+ // Validate explicit worker types before loading the runtime so an unknown type
31
+ // fails fast, just like `sixb worker`.
32
+ const requestedTypes = (options.workerTypes ?? []).map(resolveWorkerTypeToStart)
33
+ const entry = await resolveRuntimeEntry({ entry: options.entry })
34
+
35
+ const app = renderPersistent(
36
+ <LoadingView title="Starting sixb worker group" subtitle={entry} status="Loading runtime" />
37
+ )
38
+
39
+ let sixb: LoadedSixb | null = null
40
+ let workers: Worker[] = []
41
+
42
+ async function stopWorkersAndProviders() {
43
+ await Promise.all(workers.map((worker) => stopQuietly(() => worker.stop())))
44
+ if (sixb) {
45
+ await stopSixbProviders(sixb)
46
+ }
47
+ }
48
+
49
+ try {
50
+ sixb = await loadSixbFromEntry(entry)
51
+
52
+ assertShareableProviders(sixb, "worker-group")
53
+
54
+ const workerTypes =
55
+ requestedTypes.length > 0 ? requestedTypes : resolveRegisteredWorkerTypes(sixb)
56
+
57
+ // Before the `map()` below, which constructs them: one unconstructable type used to throw
58
+ // from inside that map, so the operator heard about the first problem and never learned it
59
+ // had taken every other worker down. And before the migration, which used to run first and
60
+ // leave a schema behind on a command that then refused.
61
+ assertWorkerInputs({
62
+ workerTypes,
63
+ options: { agentApiBaseUrl: options.apiPublicOrigin },
64
+ autoSelected: requestedTypes.length === 0,
65
+ })
66
+
67
+ const migration = await migrateStorageForRole(sixb, {
68
+ role: "worker-group",
69
+ noMigrate: options.noMigrate,
70
+ onStart: () =>
71
+ app.rerender(
72
+ <LoadingView
73
+ title="Starting sixb worker group"
74
+ subtitle={entry}
75
+ status="Migrating storage"
76
+ />
77
+ ),
78
+ })
79
+
80
+ app.rerender(
81
+ <LoadingView title="Starting sixb worker group" subtitle={entry} status="Starting workers" />
82
+ )
83
+
84
+ workers = workerTypes.map((workerType) =>
85
+ createWorkerForType(sixb as LoadedSixb, workerType, {
86
+ agentApiBaseUrl: options.apiPublicOrigin,
87
+ })
88
+ )
89
+ await Promise.all(workers.map((worker) => worker.start()))
90
+
91
+ const warnings =
92
+ workerTypes.length === 0
93
+ ? ["No queue worker types are registered; the worker group process is idle."]
94
+ : []
95
+
96
+ app.rerender(
97
+ <WorkerGroupView
98
+ name={sixb.id}
99
+ workerTypes={workerTypes}
100
+ storage={migration.summary}
101
+ warnings={warnings}
102
+ />
103
+ )
104
+
105
+ await Promise.race([
106
+ runUntilSignal(async () => {
107
+ app.unmount()
108
+ console.log("\nShutting down worker group...")
109
+ await stopWorkersAndProviders()
110
+ sixb = null
111
+ }),
112
+ ...workers.map(waitForWorkerFailure),
113
+ ])
114
+ } catch (error) {
115
+ app.unmount()
116
+ await stopWorkersAndProviders()
117
+ await renderCliError(error)
118
+ process.exit(1)
119
+ }
120
+ }
@@ -0,0 +1,96 @@
1
+ import type { Worker } from "@sixb/core/internal/workers"
2
+ import { SixbCliError } from "../lib/errors"
3
+ import { type LoadedSixb, loadSixbFromEntry } from "../lib/loadSixb"
4
+ import { resolveRuntimeEntry } from "../lib/production"
5
+ import {
6
+ runUntilSignal,
7
+ stopQuietly,
8
+ stopSixbProviders,
9
+ waitForWorkerFailure,
10
+ } from "../lib/runtime"
11
+ import { assertShareableProviders } from "../lib/shareable-providers"
12
+ import { migrateStorageForRole } from "../lib/storage-migration"
13
+ import {
14
+ createWorkerForType,
15
+ resolveWorkerTypeToStart,
16
+ unmetWorkerRequirement,
17
+ } from "../lib/worker-registry"
18
+ import { LoadingView, renderCliError, renderPersistent, WorkerView } from "../ui"
19
+
20
+ export interface WorkerOptions {
21
+ entry?: string
22
+ noMigrate?: boolean
23
+ workerType?: string
24
+ apiPublicOrigin?: string
25
+ }
26
+
27
+ export async function runWorker(options: WorkerOptions = {}) {
28
+ process.env.NODE_ENV = "production"
29
+
30
+ const workerType = resolveWorkerTypeToStart(options.workerType)
31
+ const entry = await resolveRuntimeEntry({ entry: options.entry })
32
+
33
+ const app = renderPersistent(
34
+ <LoadingView title="Starting sixb worker" subtitle={entry} status="Loading runtime" />
35
+ )
36
+
37
+ let sixb: LoadedSixb | null = null
38
+ let worker: Worker | null = null
39
+
40
+ try {
41
+ sixb = await loadSixbFromEntry(entry)
42
+
43
+ assertShareableProviders(sixb, "worker")
44
+
45
+ // Before the migration, which is the first thing here that changes something: this used to
46
+ // bring the schema up to date and then refuse to run.
47
+ const unmet = unmetWorkerRequirement(workerType, {
48
+ agentApiBaseUrl: options.apiPublicOrigin,
49
+ })
50
+ if (unmet) {
51
+ throw new SixbCliError(`[SixbCLI] \`sixb worker ${workerType}\` cannot start: it ${unmet}.`)
52
+ }
53
+
54
+ const migration = await migrateStorageForRole(sixb, {
55
+ role: "worker",
56
+ noMigrate: options.noMigrate,
57
+ onStart: () =>
58
+ app.rerender(
59
+ <LoadingView title="Starting sixb worker" subtitle={entry} status="Migrating storage" />
60
+ ),
61
+ })
62
+
63
+ app.rerender(
64
+ <LoadingView title="Starting sixb worker" subtitle={entry} status="Starting worker" />
65
+ )
66
+
67
+ worker = createWorkerForType(sixb, workerType, {
68
+ agentApiBaseUrl: options.apiPublicOrigin,
69
+ })
70
+ await worker.start()
71
+
72
+ const workerId = `${workerType}-worker-${sixb.id}`
73
+ app.rerender(<WorkerView name={sixb.id} workerId={workerId} storage={migration.summary} />)
74
+
75
+ await Promise.race([
76
+ runUntilSignal(async () => {
77
+ app.unmount()
78
+ console.log("\nShutting down worker...")
79
+ await stopQuietly(() => worker?.stop() ?? Promise.resolve())
80
+ if (sixb) {
81
+ await stopSixbProviders(sixb)
82
+ }
83
+ sixb = null
84
+ }),
85
+ waitForWorkerFailure(worker),
86
+ ])
87
+ } catch (error) {
88
+ app.unmount()
89
+ await stopQuietly(() => worker?.stop() ?? Promise.resolve())
90
+ if (sixb) {
91
+ await stopSixbProviders(sixb)
92
+ }
93
+ await renderCliError(error)
94
+ process.exit(1)
95
+ }
96
+ }