-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlogging.ts
More file actions
312 lines (290 loc) · 9.98 KB
/
Copy pathlogging.ts
File metadata and controls
312 lines (290 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Log streaming for dynamically-loaded tenant workers.
*
* A Tail Worker ({@link DynamicWorkerTail}) is attached to every tenant
* worker we load. Whenever the tenant emits a `console.log()` (or throws an
* exception), the runtime invokes `tail()` with the events. We forward those
* events into a per-run {@link LogSession} Durable Object, which fans them
* out to any connected SSE subscribers.
*
* Flow:
*
* tenant worker ──console.log()──▶ runtime ──▶ DynamicWorkerTail.tail()
* │
* ▼ (RPC)
* LogSession.push(entries)
* │
* ▼ (RPC back to each)
* subscriber.push(entry)
* │
* ▼
* worker's local TransformStream → SSE
*/
import {
DurableObject,
RpcTarget,
WorkerEntrypoint,
exports as workersExports,
} from 'cloudflare:workers';
/**
* One log entry streamed to the UI.
*
* `kind` is:
* - `"log"` for `console.log/warn/error/info/debug` output from the tenant
* - `"exception"` for unhandled exceptions
*/
export interface LogEntry {
kind: 'log' | 'exception';
level: string;
message: string;
timestamp: number;
}
/**
* RPC-facing subscriber contract. The HTTP handler creates one of these per
* open SSE connection and registers it with the `LogSession` DO. The DO
* then calls `push()` / `done()` on it as tail events arrive.
*/
export type LogSubscriber = {
push(entries: LogEntry[]): Promise<void>;
done(): Promise<void>;
};
/**
* `cloudflare:workers` `exports` shape after this module is imported. The
* DO class is registered purely via the `migrations` entry in
* `wrangler.jsonc` — no `durable_objects.bindings` needed — and accessed
* through `exports.LogSession.getByName(...)`.
*/
interface WorkersExports {
LogSession: {
getByName(name: string): DurableObjectStub<LogSession>;
};
}
/**
* Look up the `LogSession` Durable Object stub for a given run id.
*
* Uses `cloudflare:workers` `exports` — the DO is registered via the
* `migrations` entry in `wrangler.jsonc` and accessed by name, without a
* `durable_objects.bindings` block.
*/
export function getLogSession(name: string): DurableObjectStub<LogSession> {
return (workersExports as unknown as WorkersExports).LogSession.getByName(name);
}
function normaliseLogMessage(message: unknown): string {
if (Array.isArray(message)) {
return message
.map((entry) => (typeof entry === 'string' ? entry : JSON.stringify(entry)))
.join(' ');
}
return typeof message === 'string' ? message : JSON.stringify(message);
}
/**
* `LogSession` is a Durable Object keyed by a **run id** (the workflow
* instance id). It keeps:
*
* - A buffer of logs that arrived before any subscriber connected, so new
* SSE connections can catch up.
* - A set of RPC subscribers — one per open SSE connection.
* - The tenant source code, so a later workflow `run()` can re-load the
* exact same module if the isolate recycles.
*/
export class LogSession extends DurableObject {
private buffer: LogEntry[] = [];
private subscribers = new Set<LogSubscriber>();
private closed = false;
private source: string | undefined;
async setSource(source: string): Promise<void> {
this.source = source;
}
async getSource(): Promise<string | undefined> {
return this.source;
}
/**
* Append log entries. Called from {@link DynamicWorkerTail}. Also flushes
* to every active subscriber.
*/
async push(entries: LogEntry[]): Promise<void> {
if (entries.length === 0) return;
this.buffer.push(...entries);
if (this.buffer.length > 500) this.buffer = this.buffer.slice(-500);
const dead: LogSubscriber[] = [];
for (const sub of this.subscribers) {
try {
await sub.push(entries);
} catch {
dead.push(sub);
}
}
for (const sub of dead) this.subscribers.delete(sub);
}
/**
* Mark the run as complete. Existing subscribers are told to close.
*/
async close(): Promise<void> {
if (this.closed) return;
this.closed = true;
for (const sub of this.subscribers) {
try {
await sub.done();
} catch {
// ignore
}
}
this.subscribers.clear();
}
/**
* Register a new subscriber. Replays the buffered history immediately,
* then streams new entries as they arrive.
*
* Cap'n Web / Workers RPC disposes stubs received as parameters when the
* call returns, so we explicitly `.dup()` the subscriber here to keep it
* alive across future `push()` calls. See
* https://github.com/cloudflare/capnweb/#automatic-disposal
*/
async subscribe(subscriber: LogSubscriber): Promise<void> {
const kept =
(subscriber as LogSubscriber & { dup?: () => LogSubscriber }).dup?.() ?? subscriber;
if (this.buffer.length > 0) {
try {
await kept.push([...this.buffer]);
} catch {
return; // subscriber already broken
}
}
if (this.closed) {
try {
await kept.done();
} catch {
// ignore
}
return;
}
this.subscribers.add(kept);
}
}
/**
* Props attached to the Tail Worker so it knows which run it's logging for.
*/
export interface DynamicWorkerTailProps {
runId: string;
}
/**
* Streaming Tail Worker attached to every loaded tenant worker.
*
* Unlike the classic `tail()` handler which fires once at the end of an
* invocation with a batch of `TraceItem[]`, `tailStream()` is called at the
* start of the invocation and returns an object whose handlers fire in
* real time as the producer worker emits events. That's what lets us push
* each `console.log` line into the LogSession DO the instant it happens.
*
* Crucially, this also captures logs emitted inside workflow `run()`
* invocations during local dev — the classic `tail()` misses those.
*/
export class DynamicWorkerTail extends WorkerEntrypoint<unknown, DynamicWorkerTailProps> {
override tailStream(
_event: TailStream.TailEvent<TailStream.Onset>
): TailStream.TailEventHandlerType {
const runId = this.ctx.props.runId;
const session = getLogSession(runId);
return {
log: async (ev) => {
const data = ev.event;
await session.push([
{
kind: 'log',
level: data.level,
message: normaliseLogMessage(data.message),
timestamp: ev.timestamp.getTime(),
},
]);
},
exception: async (ev) => {
const data = ev.event;
await session.push([
{
kind: 'exception',
level: 'error',
message: data.name ? `${data.name}: ${data.message}` : data.message,
timestamp: ev.timestamp.getTime(),
},
]);
},
};
}
}
/**
* Helper invoked by the dispatcher's HTTP handler. Builds a `Response`
* whose body streams log entries from the given run in SSE format.
*
* The subscriber wrapper lives in this isolate (not the DO), so its
* underlying `TransformStream` stays alive for the lifetime of the HTTP
* response. We also hook it into `ctx.waitUntil` so the invocation doesn't
* get torn down while the DO still holds an RPC stub pointing at it.
*/
export function streamLogsResponse(runId: string, ctx?: ExecutionContext): Response {
const session = getLogSession(runId);
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
const writer = writable.getWriter();
const encoder = new TextEncoder();
async function writeSse(event: string, data: unknown): Promise<void> {
await writer.write(encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`));
}
class Subscriber extends RpcTarget implements LogSubscriber {
async push(entries: LogEntry[]): Promise<void> {
for (const entry of entries) {
await writeSse('log', entry);
}
}
async done(): Promise<void> {
await writeSse('done', null);
try {
await writer.close();
} catch {
// ignore
}
}
}
// Write an initial comment so the HTTP layer flushes headers + starts
// delivering the body immediately (otherwise some intermediaries buffer
// until the first non-empty chunk).
writer.write(encoder.encode(': connected\n\n')).catch(() => {});
// Hand the subscriber to the DO via RPC. The DO will retain the stub and
// call push()/done() on it as logs arrive.
//
// Keep an explicit local reference to the subscriber so its RPC stub
// isn't released while the response body is still being streamed. Also
// hook this lifetime into `ctx.waitUntil()` so the worker invocation
// isn't torn down while the DO still holds a stub pointing at us.
const subscriber = new Subscriber();
let releaseAlive: () => void;
const aliveUntil = new Promise<void>((resolve) => {
releaseAlive = resolve;
});
ctx?.waitUntil(aliveUntil);
(async () => {
try {
await session.subscribe(subscriber as unknown as LogSubscriber);
} catch {
// ignore
}
})();
// When the consumer disconnects (write fails), release the keepalive so
// waitUntil resolves and the invocation can finish.
const originalWrite = writer.write.bind(writer);
writer.write = (chunk) =>
originalWrite(chunk).catch((err) => {
releaseAlive();
throw err;
});
// Heartbeat: emit a comment line every 15s so intermediaries don't time
// out the connection before logs arrive.
const heartbeat = setInterval(() => {
writer.write(encoder.encode(': heartbeat\n\n')).catch(() => clearInterval(heartbeat));
}, 15000);
return new Response(readable, {
headers: {
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
'x-accel-buffering': 'no',
},
});
}