@@ -1061,6 +1061,12 @@ export const layer = Layer.effect( |
1061 | 1061 | next.delete(input.id); |
1062 | 1062 | return next; |
1063 | 1063 | }), |
| 1064 | + // Old deliveries still finishing must not fill a recreated task's queue. |
| 1065 | + Ref.update(webhookQueued, (queued) => { |
| 1066 | + const next = new Map(queued); |
| 1067 | + next.delete(input.id); |
| 1068 | + return next; |
| 1069 | + }), |
1064 | 1070 | ]), |
1065 | 1071 | ), |
1066 | 1072 | Effect.andThen(notifyChanged), |
@@ -1092,15 +1098,21 @@ export const layer = Layer.effect( |
1092 | 1098 | } |
1093 | 1099 | const token = yield* newWebhookToken; |
1094 | 1100 | const now = yield* localNow; |
1095 | | - yield* sql` |
| 1101 | + // Matching created_at keeps a rotation from landing on a task deleted |
| 1102 | + // and recreated under the same id since it was loaded. |
| 1103 | + const updated = yield* sql<{ task_id: string }>` |
1096 | 1104 | UPDATE scheduled_tasks |
1097 | 1105 | SET webhook_token = ${token}, updated_at = ${iso(now)} |
1098 | | - WHERE task_id = ${input.id} |
| 1106 | + WHERE task_id = ${input.id} AND created_at = ${task.createdAt} |
| 1107 | + RETURNING task_id |
1099 | 1108 | `.pipe( |
1100 | 1109 | Effect.mapError((cause) => |
1101 | 1110 | taskError("Could not rotate webhook token.", { taskId: input.id, cause }), |
1102 | 1111 | ), |
1103 | 1112 | ); |
| 1113 | + if (updated.length === 0) { |
| 1114 | + return yield* taskError("Schedule task was deleted or replaced.", { taskId: input.id }); |
| 1115 | + } |
1104 | 1116 | yield* notifyChanged; |
1105 | 1117 | return { task: yield* loadTask(input.id) }; |
1106 | 1118 | }); |
@@ -1341,30 +1353,28 @@ export const layer = Layer.effect( |
1341 | 1353 | } |
1342 | 1354 |
|
1343 | 1355 | const rendered = renderWebhookPrompt(task.prompt, request); |
1344 | | - yield* log("accepted", { |
1345 | | - signatureVerified: signature !== null, |
1346 | | - missing: rendered.missing, |
1347 | | - renderedPrompt: rendered.prompt, |
1348 | | - }); |
1349 | | - // Bound the deliveries waiting behind a slow run, so steady traffic to |
1350 | | - // a stuck task cannot pile up parked fibers. |
| 1356 | + // Bound the deliveries one task holds, so steady traffic to a stuck |
| 1357 | + // task cannot pile up parked fibers. A refused request is not logged, |
| 1358 | + // so it cannot push real deliveries out of the log. |
1351 | 1359 | const queued = yield* Ref.modify(webhookQueued, (counts) => { |
1352 | 1360 | const count = counts.get(task.id) ?? 0; |
1353 | 1361 | return count >= WEBHOOK_MAX_QUEUED_PER_TASK |
1354 | 1362 | ? ([false, counts] as const) |
1355 | 1363 | : ([true, new Map(counts).set(task.id, count + 1)] as const); |
1356 | 1364 | }); |
1357 | | - if (!queued) { |
1358 | | - yield* markDeliveryFailed(deliveryId, "Too many deliveries are waiting for this task."); |
1359 | | - return { _tag: "rate_limited" as const }; |
1360 | | - } |
| 1365 | + if (!queued) return { _tag: "rate_limited" as const }; |
1361 | 1366 | const release = Ref.update(webhookQueued, (counts) => { |
1362 | 1367 | const next = new Map(counts); |
1363 | 1368 | const count = (next.get(task.id) ?? 1) - 1; |
1364 | 1369 | if (count <= 0) next.delete(task.id); |
1365 | 1370 | else next.set(task.id, count); |
1366 | 1371 | return next; |
1367 | 1372 | }); |
| 1373 | + yield* log("accepted", { |
| 1374 | + signatureVerified: signature !== null, |
| 1375 | + missing: rendered.missing, |
| 1376 | + renderedPrompt: rendered.prompt, |
| 1377 | + }).pipe(Effect.onError(() => release)); |
1368 | 1378 | const permit = yield* webhookPermit(task.id); |
1369 | 1379 | yield* runTask(task, "webhook", { deliveryId, prompt: rendered.prompt }).pipe( |
1370 | 1380 | Effect.flatMap((completed) => |
|