-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.ts
501 lines (429 loc) · 14.7 KB
/
worker.ts
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import { DurableObject } from "cloudflare:workers";
type QueueEnv = { [name: `QUEUE_${number}`]: Queue };
interface Env extends QueueEnv {
workflow_durable_object: DurableObjectNamespace<WorkflowDurableObject>;
SECRET: string;
}
export type RequestJson = {
url: string;
body?: string | object;
/** defaults to post if body is given, get otherwise */
method?: "GET" | "POST" | "DELETE" | "PUT" | "PATCH";
headers?: { [name: string]: string };
};
type FetchItem = {
durableObjectName: string;
item: RequestJson | null;
index: number;
};
type Update = {
type: "update";
status: { [key: string]: number };
done?: number;
error?: string;
results?: ResponseItem[];
};
type ResponseItem = {
id: number;
headers?: string;
status: number;
error?: string;
result?: any;
done: number;
created_at: number;
};
const tryParseJson = (data: any) => {
try {
return JSON.parse(data);
} catch (e) {
return null;
}
};
const shouldRetry = (status: number, attempts: number): boolean => {
// Retry on server errors (5xx) and certain client errors
if (attempts > 25) return false;
if (status >= 500) return true;
if (status === 429) return true; // Rate limiting
if (status === 408) return true; // Request timeout
return false;
};
export default {
fetch: async (request: Request, env: Env) => {
// Handle OPTIONS request for CORS
if (request.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
try {
const url = new URL(request.url);
const apiKey = request.headers
.get("Authorization")
?.slice("Bearer ".length);
const accept = request.headers.get("Accept");
if (!apiKey || apiKey !== env.SECRET) {
return new Response("Unauthorized", { status: 401 });
}
const array: any[] = await request.json();
if (!Array.isArray(array)) {
return new Response("No array passed as input JSON", { status: 400 });
}
const durableObjectName = crypto.randomUUID();
console.log({ durableObjectName, length: array.length });
// If not, enqueue everything
const messages: Array<MessageSendRequest<FetchItem>> = array.map(
(item, index) => ({
body: { item, index, durableObjectName },
}),
);
const QUEUE_COUNT = 1;
const MAX_BATCH_SIZE = 6;
// depending on the total requests that need to be done, we want to send so many in a batch
const chunkSize =
messages.length / QUEUE_COUNT <= MAX_BATCH_SIZE
? MAX_BATCH_SIZE
: Math.min(100, Math.ceil(messages.length / QUEUE_COUNT));
// Divide all requests over the queues as quickly and smartly as possible
let q = 0;
const promises: Promise<void>[] = [];
for (let i = 0; i < messages.length; i += chunkSize) {
const chunk = messages.slice(i, i + chunkSize);
const queue: Queue = env[`QUEUE_${q}`];
promises.push(queue.sendBatch(chunk));
q = (q + 1) % QUEUE_COUNT;
}
console.log("queueing it up");
// Await for that to be all done
await Promise.all(promises);
console.log("queueing done");
// Create Durable Object
const doId = env.workflow_durable_object.idFromName(durableObjectName);
const do_instance = env.workflow_durable_object.get(doId);
// await the DO until all items are done
const response = await do_instance.fetch(
new Request(url.origin + "/?count=" + array.length, {
method: "GET",
headers: { accept: accept || "text/event-stream" },
}),
);
console.log("responses ready!");
return response;
} catch (e: any) {
console.error("Something went wrong", e);
return new Response("Something went wrong: " + e.message, {
status: 500,
});
}
},
queue: async (batch: MessageBatch<FetchItem>, env: Env): Promise<void> => {
await Promise.all(
batch.messages.map(async (message) => {
// each message is a request
const { index, item, durableObjectName } = message.body;
let done = false;
let status;
let error;
let result;
const responseHeaders: { [name: string]: string } = {};
if (item) {
try {
const { url, body, headers, method } = item;
// execute request
console.log("fetching", url);
const response = await fetch(url, {
method: method ? method : body ? "POST" : "GET",
headers,
body:
typeof body === "string"
? body
: typeof body === "object"
? JSON.stringify(body)
: undefined,
});
done =
response.status === 200 ||
!shouldRetry(response.status, message.attempts);
status = response.status;
const responseType = response.headers
.get("content-type")
?.split("/")[0];
if (responseType && ["image", "video"].includes(responseType)) {
done = true;
console.log("Responsetype is not waht we like");
return;
}
const text = await response.text();
console.log("fech-each done", url, done);
response.headers.forEach(
(value, key) => (responseHeaders[key] = value),
);
if (response.status === 200) {
result = tryParseJson(text) || text || undefined;
} else {
error = text;
}
} catch (e: any) {
done = false;
status = 500;
error = e.message;
}
} else {
status = 200;
done = true;
}
// retry cetain ones, don't retry other ones
// add intermediate and final results of the request onto the durable object
const doId = env.workflow_durable_object.idFromName(durableObjectName);
const do_instance = env.workflow_durable_object.get(doId);
const request = new Request("http://something.com/", {
method: "POST",
body: JSON.stringify({
id: index,
status,
headers: Object.keys(responseHeaders).length
? JSON.stringify(responseHeaders)
: undefined,
error,
result,
created_at: Date.now(),
done: done ? 1 : 0,
} satisfies ResponseItem),
});
// submit the value to the sql DO, circumventing it being overloaded
await fetchWithRetry(do_instance, request);
if (done) {
message.ack();
} else {
// Exponential backoff
// Max 10 minutes
const delaySeconds = Math.min(Math.pow(2, message.attempts), 600);
message.retry({ delaySeconds });
}
}),
);
},
};
const fetchWithRetry = async (
stub: DurableObjectStub,
request: Request,
maxAttempts = 25,
) => {
let attempt = 0;
while (true) {
try {
return await stub.fetch(request);
} catch (e: any) {
attempt++;
if (attempt >= maxAttempts || !e.retryable) {
throw e;
}
// Exponential backoff
await new Promise((resolve) =>
setTimeout(resolve, Math.pow(2, attempt) * 100),
);
}
}
};
export class WorkflowDurableObject extends DurableObject<Env> {
private state: DurableObjectState;
//private env: Env;
private sql: SqlStorage;
constructor(state: DurableObjectState, env: Env) {
super(state, env);
this.state = state;
try {
this.sql = this.state.storage.sql;
// Add error handling for SQL initialization
this.sql.exec(`
CREATE TABLE IF NOT EXISTS responses (
id TEXT PRIMARY KEY,
status INTEGER,
done INTEGER,
error TEXT,
result TEXT,
headers TEXT,
created_at INTEGER
)
`);
} catch (error) {
console.error("Failed to initialize SQL storage:", error);
throw error; // Re-throw to prevent silent failure
}
}
async alarm(): Promise<void> {
// Clean up the DO after all work is done
await this.state.storage.deleteAll();
await this.state.storage.deleteAlarm();
}
async fetch(request: Request): Promise<Response> {
if (request.method === "GET") {
// always stream, for now
const isStream = request.headers.get("accept") === "text/event-stream";
if (!isStream) {
const url = new URL(request.url);
const count = Number(url.searchParams.get("count"));
let results;
let t = 0;
while (t <= 86400) {
// count done
// NB: can be expensive for large responses, so may be better to count statuses and 'done' in a query instead.
results = this.sql
.exec<ResponseItem>(`SELECT * FROM responses ORDER BY id ASC`)
.toArray();
// Count statuses
const status: { [key: string]: number } = {};
results.forEach((r) => {
status[r.status] = (status[r.status] || 0) + 1;
});
const r = results.filter((x) => x.done);
const newUpdateString = JSON.stringify({
type: "update",
status,
done: r.length,
results: r,
} satisfies Update);
const allDone = results!.filter((x) => x.done).length === count;
if (allDone) {
// Set alarm to delete DO after 1 hour
await this.state.storage.setAlarm(Date.now() + 3600000);
break;
}
t = t + 0.5;
await new Promise<void>((resolve) => setTimeout(resolve, 500));
}
const finalResults = this.sql
.exec<ResponseItem>(`SELECT * FROM responses ORDER BY id ASC`)
.toArray();
const resultArray = finalResults
.sort((a, b) => a.id - b.id)
.map((item) => ({
status: item.status,
error: item.error,
headers: tryParseJson(item.headers) || item.headers || undefined,
result: tryParseJson(item.result) || item.result || null,
}));
return new Response(JSON.stringify(resultArray), { status: 200 });
}
// stream
return new Response(
new ReadableStream({
start: async (controller) => {
try {
const url = new URL(request.url);
const count = Number(url.searchParams.get("count"));
const encoder = new TextEncoder();
let results;
let t = 0;
let updateString;
let error: string | undefined =
"Timeout exceeded: max queue time is 1 day";
while (t <= 86400) {
// count done
// NB: can be expensive for large responses, so may be better to count statuses and 'done' in a query instead.
results = this.sql
.exec<ResponseItem>(`SELECT * FROM responses ORDER BY id ASC`)
.toArray();
// Count statuses
const status: { [key: string]: number } = {};
results.forEach((r) => {
status[r.status] = (status[r.status] || 0) + 1;
});
const r = results.filter((x) => x.done);
const newUpdateString = JSON.stringify({
type: "update",
status,
done: r.length,
results: r,
} satisfies Update);
if (updateString !== newUpdateString) {
controller.enqueue(
encoder.encode(
`event: update\ndata: ${newUpdateString}\n\n`,
),
);
}
updateString = newUpdateString;
const allDone = results!.filter((x) => x.done).length === count;
if (allDone) {
// Set alarm to delete DO after 1 hour
await this.state.storage.setAlarm(Date.now() + 3600000);
error = undefined;
break;
}
t = t + 0.5;
await new Promise<void>((resolve) => setTimeout(resolve, 500));
}
const finalResults = this.sql
.exec<ResponseItem>(`SELECT * FROM responses ORDER BY id ASC`)
.toArray();
const resultArray = finalResults
.sort((a, b) => a.id - b.id)
.map((item) => ({
status: item.status,
error: item.error,
headers:
tryParseJson(item.headers) || item.headers || undefined,
result: tryParseJson(item.result) || item.result || null,
}));
// Send final result
controller.enqueue(
encoder.encode(
`event: result\ndata: ${JSON.stringify({
type: "result",
array: resultArray,
})}\n\n`,
),
);
controller.close();
} catch (e: any) {
console.error("crash in stream", e);
const encoder = new TextEncoder();
controller.enqueue(
encoder.encode(
`event: result\ndata: ${JSON.stringify({
type: "update",
error: "Crash in stream" + e.message,
})}\n\n`,
),
);
controller.close();
}
},
}),
{
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
},
);
}
if (request.method === "POST") {
try {
const data: ResponseItem = await request.json();
this.sql.exec(
`INSERT OR REPLACE INTO responses (id, status, done, error, result, headers, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)`,
data.id,
data.status,
data.done,
data.error,
JSON.stringify(data.result),
data.headers ? data.headers : null,
data.created_at,
);
return new Response(JSON.stringify({ success: true }), { status: 200 });
} catch (e) {
return new Response(JSON.stringify({ success: false }), {
status: 500,
});
}
}
return new Response(JSON.stringify({ success: false }), { status: 405 });
}
}