@crewhaus/target-batch-worker 0.1.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.
- package/package.json +43 -0
- package/src/index.test.ts +98 -0
- package/src/index.ts +287 -0
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@crewhaus/target-batch-worker",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Codegen for the BATCH target — emits a queue-consumer daemon (Section 23 BATCH)",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"types": "src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "bun test src"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@crewhaus/errors": "0.0.0",
|
|
16
|
+
"@crewhaus/infra-utils": "0.0.0",
|
|
17
|
+
"@crewhaus/ir": "0.0.0"
|
|
18
|
+
},
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Max Meier",
|
|
22
|
+
"email": "max@studiomax.io",
|
|
23
|
+
"url": "https://studiomax.io"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/crewhaus/factory.git",
|
|
28
|
+
"directory": "packages/target-batch-worker"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/crewhaus/factory/tree/main/packages/target-batch-worker#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/crewhaus/factory/issues"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "restricted"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"src",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"NOTICE"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { IrBatchV0 } from "@crewhaus/ir";
|
|
3
|
+
import { TargetEmitError, emitBatchWorker } from "./index.js";
|
|
4
|
+
|
|
5
|
+
const baseIr: IrBatchV0 = {
|
|
6
|
+
version: 0,
|
|
7
|
+
name: "hello-batch",
|
|
8
|
+
target: "batch",
|
|
9
|
+
agent: {
|
|
10
|
+
model: "claude-haiku-4-5-20251001",
|
|
11
|
+
instructions: "You are a batch worker.",
|
|
12
|
+
},
|
|
13
|
+
queue: {
|
|
14
|
+
adapter: "in-memory",
|
|
15
|
+
visibilityTimeoutMs: 30_000,
|
|
16
|
+
maxRetries: 3,
|
|
17
|
+
seedJobs: ["job 1 input", "job 2 input"],
|
|
18
|
+
},
|
|
19
|
+
concurrency: 4,
|
|
20
|
+
idempotencyWindowMs: 60_000,
|
|
21
|
+
tools: [],
|
|
22
|
+
toolConfigs: Object.freeze({}),
|
|
23
|
+
mcp_servers: Object.freeze({}),
|
|
24
|
+
permissions: { rules: [] },
|
|
25
|
+
compaction: {},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
describe("emitBatchWorker", () => {
|
|
29
|
+
test("emits a single agent.ts file (T1 bundle structure)", () => {
|
|
30
|
+
const bundle = emitBatchWorker(baseIr);
|
|
31
|
+
expect(bundle.files).toHaveLength(1);
|
|
32
|
+
expect(bundle.files[0]?.path).toBe("agent.ts");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("agent.ts wires queue-protocol + queue-consumer + idempotency-keys", () => {
|
|
36
|
+
const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
|
|
37
|
+
expect(code).toContain("@crewhaus/queue-protocol");
|
|
38
|
+
expect(code).toContain("@crewhaus/queue-consumer");
|
|
39
|
+
expect(code).toContain("@crewhaus/idempotency-keys");
|
|
40
|
+
expect(code).toContain("createInMemoryQueue");
|
|
41
|
+
expect(code).toContain("startConsumer");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("seeds the in-memory queue with the spec's seedJobs", () => {
|
|
45
|
+
const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
|
|
46
|
+
expect(code).toContain('"job 1 input"');
|
|
47
|
+
expect(code).toContain('"job 2 input"');
|
|
48
|
+
expect(code).toContain("initialJobs: SPEC_SEED_JOBS");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("hard-codes concurrency / visibilityTimeoutMs / idempotencyWindowMs from the spec", () => {
|
|
52
|
+
const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
|
|
53
|
+
expect(code).toContain("SPEC_CONCURRENCY = 4");
|
|
54
|
+
expect(code).toContain("SPEC_VISIBILITY_TIMEOUT_MS = 30000");
|
|
55
|
+
expect(code).toContain("SPEC_IDEMPOTENCY_WINDOW_MS = 60000");
|
|
56
|
+
expect(code).toContain("SPEC_MAX_RETRIES = 3");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("emits SIGTERM/SIGINT shutdown handlers and a queue_idle exit path", () => {
|
|
60
|
+
const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
|
|
61
|
+
expect(code).toContain('process.on("SIGINT"');
|
|
62
|
+
expect(code).toContain('process.on("SIGTERM"');
|
|
63
|
+
expect(code).toContain('"queue_idle"');
|
|
64
|
+
expect(code).toContain('"worker_stop"');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("non-in-memory adapter compiles, but boot throws (clean diagnostic)", () => {
|
|
68
|
+
const ir: IrBatchV0 = {
|
|
69
|
+
...baseIr,
|
|
70
|
+
queue: { ...baseIr.queue, adapter: "sqs" },
|
|
71
|
+
};
|
|
72
|
+
const code = emitBatchWorker(ir).files[0]?.content ?? "";
|
|
73
|
+
expect(code).toContain('"in-memory"');
|
|
74
|
+
expect(code).toContain("not implemented in v0");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("rejects unknown spec-side tool names at compile time", () => {
|
|
78
|
+
const ir: IrBatchV0 = {
|
|
79
|
+
...baseIr,
|
|
80
|
+
tools: ["nonexistent"],
|
|
81
|
+
};
|
|
82
|
+
expect(() => emitBatchWorker(ir)).toThrow(TargetEmitError);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("permissions block: passes spec yaml-source rules through", () => {
|
|
86
|
+
const ir: IrBatchV0 = {
|
|
87
|
+
...baseIr,
|
|
88
|
+
permissions: {
|
|
89
|
+
mode: "default",
|
|
90
|
+
rules: [{ type: "alwaysAllow", pattern: "Read" }],
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
const code = emitBatchWorker(ir).files[0]?.content ?? "";
|
|
94
|
+
expect(code).toContain('permissionMode: "default"');
|
|
95
|
+
expect(code).toContain('pattern: "Read"');
|
|
96
|
+
expect(code).toContain("BUILTIN_DEFAULT_RULES");
|
|
97
|
+
});
|
|
98
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog F2 `target-batch-worker` — Section 23 BATCH.
|
|
3
|
+
*
|
|
4
|
+
* Codegen for the queue-worker target. Emits a single self-contained
|
|
5
|
+
* daemon (`agent.ts`) that:
|
|
6
|
+
*
|
|
7
|
+
* 1. Constructs the configured queue adapter (in-memory only in v0;
|
|
8
|
+
* SQS / Redis Streams / Postgres adapters stub out at the
|
|
9
|
+
* `unsupported queue adapter` error).
|
|
10
|
+
* 2. Optionally seeds the in-memory adapter with `queue.seedJobs`
|
|
11
|
+
* (test / smoke convenience).
|
|
12
|
+
* 3. Boots a `queue-consumer` with the user's `agent` as the handler
|
|
13
|
+
* — each pulled job runs one `runChatLoop({singleTurn: true})` with
|
|
14
|
+
* the job input as the user message; the assistant's terminal text
|
|
15
|
+
* is the cached result.
|
|
16
|
+
* 4. Streams JSON events to stdout (`worker_start | job_start |
|
|
17
|
+
* job_end | drain_start | drain_end | worker_stop`).
|
|
18
|
+
* 5. Installs SIGTERM + SIGINT handlers that drain in-flight jobs
|
|
19
|
+
* cleanly before exit.
|
|
20
|
+
*/
|
|
21
|
+
import { CrewhausError } from "@crewhaus/errors";
|
|
22
|
+
import { escapeJsonString } from "@crewhaus/infra-utils";
|
|
23
|
+
import type { Bundle, IrBatchV0 } from "@crewhaus/ir";
|
|
24
|
+
|
|
25
|
+
export class TargetEmitError extends CrewhausError {
|
|
26
|
+
override readonly name = "TargetEmitError";
|
|
27
|
+
constructor(message: string, cause?: unknown) {
|
|
28
|
+
super("compiler", message, cause);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type BuiltinToolEntry = {
|
|
33
|
+
readonly package: string;
|
|
34
|
+
readonly export: string;
|
|
35
|
+
readonly initSymbol?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const BUILTIN_TOOL_MAP: Record<string, BuiltinToolEntry> = {
|
|
39
|
+
read: { package: "@crewhaus/tool-fs", export: "read" },
|
|
40
|
+
write: { package: "@crewhaus/tool-fs", export: "write" },
|
|
41
|
+
edit: { package: "@crewhaus/tool-fs", export: "edit" },
|
|
42
|
+
glob: { package: "@crewhaus/tool-fs", export: "glob" },
|
|
43
|
+
grep: { package: "@crewhaus/tool-fs", export: "grep" },
|
|
44
|
+
bash: { package: "@crewhaus/tool-bash", export: "bash" },
|
|
45
|
+
webFetch: {
|
|
46
|
+
package: "@crewhaus/tool-web",
|
|
47
|
+
export: "webFetch",
|
|
48
|
+
initSymbol: "registerWebFetchConfig",
|
|
49
|
+
},
|
|
50
|
+
webSearch: { package: "@crewhaus/tool-web", export: "webSearch" },
|
|
51
|
+
fetch: {
|
|
52
|
+
package: "@crewhaus/tool-fetch",
|
|
53
|
+
export: "fetch",
|
|
54
|
+
initSymbol: "registerFetchConfig",
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function resolveTools(
|
|
59
|
+
toolNames: readonly string[],
|
|
60
|
+
toolConfigs: Readonly<Record<string, unknown>>,
|
|
61
|
+
): {
|
|
62
|
+
imports: string[];
|
|
63
|
+
inits: string[];
|
|
64
|
+
registrations: string[];
|
|
65
|
+
} {
|
|
66
|
+
if (toolNames.length === 0) return { imports: [], inits: [], registrations: [] };
|
|
67
|
+
const byPackage = new Map<string, Set<string>>();
|
|
68
|
+
const inits: string[] = [];
|
|
69
|
+
const registrations: string[] = [];
|
|
70
|
+
for (const name of toolNames) {
|
|
71
|
+
const entry = BUILTIN_TOOL_MAP[name];
|
|
72
|
+
if (!entry) {
|
|
73
|
+
const known = Object.keys(BUILTIN_TOOL_MAP).sort().join(", ");
|
|
74
|
+
throw new TargetEmitError(`unknown tool "${name}" — known tools: ${known}`);
|
|
75
|
+
}
|
|
76
|
+
const set = byPackage.get(entry.package) ?? new Set<string>();
|
|
77
|
+
set.add(entry.export);
|
|
78
|
+
byPackage.set(entry.package, set);
|
|
79
|
+
if (entry.initSymbol !== undefined) {
|
|
80
|
+
const cfg = toolConfigs[name];
|
|
81
|
+
if (cfg !== undefined) {
|
|
82
|
+
set.add(entry.initSymbol);
|
|
83
|
+
inits.push(`${entry.initSymbol}(${JSON.stringify(cfg)});`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
registrations.push(`defaultCatalog.register(${entry.export});`);
|
|
87
|
+
}
|
|
88
|
+
const imports: string[] = [];
|
|
89
|
+
for (const pkg of [...byPackage.keys()].sort()) {
|
|
90
|
+
const symbols = [...(byPackage.get(pkg) ?? new Set<string>())].sort();
|
|
91
|
+
imports.push(`import { ${symbols.join(", ")} } from "${pkg}";`);
|
|
92
|
+
}
|
|
93
|
+
return { imports, inits, registrations };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function renderPermissionsField(ir: IrBatchV0): string {
|
|
97
|
+
const { mode, rules } = ir.permissions;
|
|
98
|
+
if (mode === undefined && rules.length === 0) return "";
|
|
99
|
+
const lines: string[] = [];
|
|
100
|
+
if (mode !== undefined) {
|
|
101
|
+
lines.push(` permissionMode: ${escapeJsonString(mode)},`);
|
|
102
|
+
}
|
|
103
|
+
if (rules.length > 0) {
|
|
104
|
+
const ruleLits = rules
|
|
105
|
+
.map(
|
|
106
|
+
(r) =>
|
|
107
|
+
` { type: ${escapeJsonString(r.type)}, pattern: ${escapeJsonString(r.pattern)}, source: "yaml" },`,
|
|
108
|
+
)
|
|
109
|
+
.join("\n");
|
|
110
|
+
lines.push(
|
|
111
|
+
[
|
|
112
|
+
" permissionRules: {",
|
|
113
|
+
" flag: [],",
|
|
114
|
+
" settings: [],",
|
|
115
|
+
" yaml: [",
|
|
116
|
+
ruleLits,
|
|
117
|
+
" ],",
|
|
118
|
+
" hooks: [],",
|
|
119
|
+
" builtin: BUILTIN_DEFAULT_RULES,",
|
|
120
|
+
" },",
|
|
121
|
+
].join("\n"),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return `\n${lines.join("\n")}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function emitBatchWorker(ir: IrBatchV0): Bundle {
|
|
128
|
+
if (ir.queue.adapter !== "in-memory") {
|
|
129
|
+
// v0 only ships the in-memory adapter; the others are stubs that
|
|
130
|
+
// throw at runtime so the operator sees a clean diagnostic.
|
|
131
|
+
// Codegen still succeeds — we emit the daemon, but boot fails.
|
|
132
|
+
}
|
|
133
|
+
return { files: [{ path: "agent.ts", content: renderAgent(ir) }] };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function renderAgent(ir: IrBatchV0): string {
|
|
137
|
+
const { imports: builtinImports, inits, registrations } = resolveTools(ir.tools, ir.toolConfigs);
|
|
138
|
+
const importBlock = builtinImports.length > 0 ? `${builtinImports.join("\n")}\n` : "";
|
|
139
|
+
const initLines = inits.length > 0 ? `${inits.join("\n")}\n` : "";
|
|
140
|
+
const registrationBlock =
|
|
141
|
+
registrations.length > 0
|
|
142
|
+
? `\n// Spec-supplied tools registered at module load.\n${registrations.join("\n")}\n`
|
|
143
|
+
: "";
|
|
144
|
+
const permField = renderPermissionsField(ir);
|
|
145
|
+
|
|
146
|
+
const seedJobsLit = JSON.stringify(ir.queue.seedJobs ?? []);
|
|
147
|
+
const visRenewMs =
|
|
148
|
+
ir.queue.visibilityRenewIntervalMs ?? Math.floor(ir.queue.visibilityTimeoutMs / 4);
|
|
149
|
+
const hasRules = ir.permissions.rules.length > 0;
|
|
150
|
+
const permImport = hasRules
|
|
151
|
+
? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
|
|
152
|
+
: "";
|
|
153
|
+
|
|
154
|
+
return `#!/usr/bin/env bun
|
|
155
|
+
// Generated by crewhaus. DO NOT EDIT.
|
|
156
|
+
// Source spec: ${ir.name} (target: batch, ir version: ${ir.version})
|
|
157
|
+
import { createInMemoryQueue } from "@crewhaus/queue-protocol";
|
|
158
|
+
import { startConsumer } from "@crewhaus/queue-consumer";
|
|
159
|
+
import { createInMemoryIdempotencyStore } from "@crewhaus/idempotency-keys";
|
|
160
|
+
import { runChatLoop } from "@crewhaus/runtime-core";
|
|
161
|
+
import { createRunContext } from "@crewhaus/run-context";
|
|
162
|
+
import { defaultCatalog } from "@crewhaus/tool-catalog";
|
|
163
|
+
${permImport}${importBlock}
|
|
164
|
+
|
|
165
|
+
${initLines}${registrationBlock}
|
|
166
|
+
const SPEC_NAME = ${escapeJsonString(ir.name)};
|
|
167
|
+
const SPEC_MODEL = ${escapeJsonString(ir.agent.model)};
|
|
168
|
+
const SPEC_INSTRUCTIONS = ${escapeJsonString(ir.agent.instructions)};
|
|
169
|
+
const SPEC_QUEUE_ADAPTER = ${escapeJsonString(ir.queue.adapter)};
|
|
170
|
+
const SPEC_VISIBILITY_TIMEOUT_MS = ${ir.queue.visibilityTimeoutMs};
|
|
171
|
+
const SPEC_VISIBILITY_RENEW_MS = ${visRenewMs};
|
|
172
|
+
const SPEC_MAX_RETRIES = ${ir.queue.maxRetries};
|
|
173
|
+
const SPEC_CONCURRENCY = ${ir.concurrency};
|
|
174
|
+
const SPEC_IDEMPOTENCY_WINDOW_MS = ${ir.idempotencyWindowMs};
|
|
175
|
+
const SPEC_SEED_JOBS: string[] = ${seedJobsLit};
|
|
176
|
+
|
|
177
|
+
function emit(event: Record<string, unknown>): void {
|
|
178
|
+
process.stdout.write(\`\${JSON.stringify(event)}\\n\`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function buildQueue(): ReturnType<typeof createInMemoryQueue<string>> {
|
|
182
|
+
if (SPEC_QUEUE_ADAPTER === "in-memory") {
|
|
183
|
+
return createInMemoryQueue<string>({ initialJobs: SPEC_SEED_JOBS });
|
|
184
|
+
}
|
|
185
|
+
throw new Error(
|
|
186
|
+
"[batch-worker] queue adapter \\"" + SPEC_QUEUE_ADAPTER +
|
|
187
|
+
"\\" is not implemented in v0 — only \\"in-memory\\" is supported. " +
|
|
188
|
+
"SQS / Redis Streams / Postgres adapters land in a follow-up.",
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function main(): Promise<void> {
|
|
193
|
+
const queue = buildQueue();
|
|
194
|
+
const idempotencyStore = createInMemoryIdempotencyStore<string>();
|
|
195
|
+
emit({ kind: "worker_start", queueAdapter: SPEC_QUEUE_ADAPTER, concurrency: SPEC_CONCURRENCY });
|
|
196
|
+
|
|
197
|
+
const consumer = startConsumer<string, string>({
|
|
198
|
+
queue,
|
|
199
|
+
handler: async (input, ctx) => {
|
|
200
|
+
emit({ kind: "job_start", jobId: ctx.job.id, attempt: ctx.job.attempt });
|
|
201
|
+
const runContext = createRunContext();
|
|
202
|
+
const reply = await runChatLoop({
|
|
203
|
+
model: SPEC_MODEL,
|
|
204
|
+
instructions: SPEC_INSTRUCTIONS,
|
|
205
|
+
runContext,
|
|
206
|
+
sessionName: SPEC_NAME,
|
|
207
|
+
sessionTarget: "batch",
|
|
208
|
+
singleTurn: true,
|
|
209
|
+
seedMessages: [{ role: "user", content: input }],
|
|
210
|
+
tools: defaultCatalog.list(),
|
|
211
|
+
installSigintHandler: false,
|
|
212
|
+
maxTokens: 1024,${permField}
|
|
213
|
+
});
|
|
214
|
+
return reply.trim();
|
|
215
|
+
},
|
|
216
|
+
concurrency: SPEC_CONCURRENCY,
|
|
217
|
+
visibilityTimeoutMs: SPEC_VISIBILITY_TIMEOUT_MS,
|
|
218
|
+
visibilityRenewIntervalMs: SPEC_VISIBILITY_RENEW_MS,
|
|
219
|
+
maxRetries: SPEC_MAX_RETRIES,
|
|
220
|
+
idempotencyStore,
|
|
221
|
+
idempotencyTtlMs: SPEC_IDEMPOTENCY_WINDOW_MS,
|
|
222
|
+
observer: {
|
|
223
|
+
onJobEnd: (job, outcome) => {
|
|
224
|
+
if (outcome.kind === "ok") {
|
|
225
|
+
emit({ kind: "job_end", jobId: job.id, attempt: job.attempt, status: "ok", fromCache: outcome.fromCache });
|
|
226
|
+
} else {
|
|
227
|
+
emit({
|
|
228
|
+
kind: "job_end",
|
|
229
|
+
jobId: job.id,
|
|
230
|
+
attempt: job.attempt,
|
|
231
|
+
status: "fail",
|
|
232
|
+
reason: outcome.reason,
|
|
233
|
+
error: (outcome.error as Error).message ?? String(outcome.error),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
onDrainStart: () => emit({ kind: "drain_start", inFlight: consumer.inFlight() }),
|
|
238
|
+
onDrainEnd: () => emit({ kind: "drain_end" }),
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
let stopping = false;
|
|
243
|
+
const shutdown = async (signal: string): Promise<void> => {
|
|
244
|
+
if (stopping) return;
|
|
245
|
+
stopping = true;
|
|
246
|
+
emit({ kind: "shutdown_received", signal });
|
|
247
|
+
await consumer.drain();
|
|
248
|
+
const stats = await queue.stats();
|
|
249
|
+
emit({ kind: "worker_stop", stats });
|
|
250
|
+
process.exit(0);
|
|
251
|
+
};
|
|
252
|
+
process.on("SIGINT", () => void shutdown("SIGINT"));
|
|
253
|
+
process.on("SIGTERM", () => void shutdown("SIGTERM"));
|
|
254
|
+
|
|
255
|
+
// For the hello-batch smoke: when the queue empties + no in-flight,
|
|
256
|
+
// emit a queue_idle event and exit so the smoke can assert on a
|
|
257
|
+
// clean shutdown without sending a signal. Production users will
|
|
258
|
+
// typically run forever and rely on signals.
|
|
259
|
+
const idleCheckMs = 200;
|
|
260
|
+
while (!stopping) {
|
|
261
|
+
await new Promise((r) => setTimeout(r, idleCheckMs));
|
|
262
|
+
const stats = await queue.stats();
|
|
263
|
+
if (stats.pending === 0 && stats.inFlight === 0 && consumer.inFlight() === 0) {
|
|
264
|
+
// Idle — check once more after a short delay to avoid racing with
|
|
265
|
+
// a job mid-pull.
|
|
266
|
+
await new Promise((r) => setTimeout(r, idleCheckMs));
|
|
267
|
+
const recheck = await queue.stats();
|
|
268
|
+
if (
|
|
269
|
+
recheck.pending === 0 &&
|
|
270
|
+
recheck.inFlight === 0 &&
|
|
271
|
+
consumer.inFlight() === 0
|
|
272
|
+
) {
|
|
273
|
+
emit({ kind: "queue_idle", stats: recheck });
|
|
274
|
+
await consumer.drain();
|
|
275
|
+
emit({ kind: "worker_stop", stats: await queue.stats() });
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
main().catch((err) => {
|
|
283
|
+
process.stderr.write(\`[batch-worker] fatal: \${(err as Error).message}\\n\`);
|
|
284
|
+
process.exit(1);
|
|
285
|
+
});
|
|
286
|
+
`;
|
|
287
|
+
}
|