Skip to content

Commit 111f38a

Browse files
committed
added debug info
1 parent fe0606a commit 111f38a

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

packages/cloudflare/src/api/durable-objects/queue.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { error } from "@opennextjs/aws/adapters/logger.js";
1+
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
22
import type { QueueMessage } from "@opennextjs/aws/types/overrides";
33
import {
44
FatalError,
@@ -46,7 +46,10 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
4646
this.sql = ctx.storage.sql;
4747

4848
// We restore the state
49-
ctx.blockConcurrencyWhile(() => this.initState());
49+
ctx.blockConcurrencyWhile(async () => {
50+
debug(`Restoring the state of the durable object`);
51+
await this.initState();
52+
});
5053

5154
this.maxRevalidations = env.MAX_REVALIDATION_BY_DURABLE_OBJECT
5255
? parseInt(env.MAX_REVALIDATION_BY_DURABLE_OBJECT)
@@ -63,6 +66,8 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
6366
this.maxRevalidationAttempts = env.MAX_REVALIDATION_ATTEMPTS
6467
? parseInt(env.MAX_REVALIDATION_ATTEMPTS)
6568
: DEFAULT_MAX_REVALIDATION_ATTEMPTS;
69+
70+
debug(`Durable object initialized`);
6671
}
6772

6873
async revalidate(msg: QueueMessage) {
@@ -77,11 +82,17 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
7782
if (this.checkSyncTable(msg)) return;
7883

7984
if (this.ongoingRevalidations.size >= this.maxRevalidations) {
85+
debug(
86+
`The maximum number of revalidations (${this.maxRevalidations}) is reached. Blocking until one of the revalidations finishes.`
87+
);
8088
const ongoingRevalidations = this.ongoingRevalidations.values();
8189
// When there is more than the max revalidations, we block concurrency until one of the revalidations finishes
8290
// We still await the promise to ensure the revalidation is completed
8391
// This is fine because the queue itself run inside a waitUntil
84-
await this.ctx.blockConcurrencyWhile(() => Promise.race(ongoingRevalidations));
92+
await this.ctx.blockConcurrencyWhile(async () => {
93+
debug(`Waiting for one of the revalidations to finish`);
94+
await Promise.race(ongoingRevalidations);
95+
});
8596
}
8697

8798
const revalidationPromise = this.executeRevalidation(msg);
@@ -95,6 +106,7 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
95106

96107
private async executeRevalidation(msg: QueueMessage) {
97108
try {
109+
debug(`Revalidating ${msg.MessageBody.host}${msg.MessageBody.url}`);
98110
const {
99111
MessageBody: { host, url },
100112
} = msg;
@@ -173,11 +185,13 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
173185
? [nextEventToRetry, ...expiredEvents]
174186
: expiredEvents;
175187
for (const event of allEventsToRetry) {
188+
debug(`Retrying revalidation for ${event.msg.MessageBody.host}${event.msg.MessageBody.url}`);
176189
await this.executeRevalidation(event.msg);
177190
}
178191
}
179192

180193
async addToFailedState(msg: QueueMessage) {
194+
debug(`Adding ${msg.MessageBody.host}${msg.MessageBody.url} to the failed state`);
181195
const existingFailedState = this.routeInFailedState.get(msg.MessageDeduplicationId);
182196
let nextAlarm = Date.now() + this.revalidationRetryInterval;
183197

packages/cloudflare/src/cli/build/open-next/compileDurableObjects.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import fs from "node:fs";
22
import { createRequire } from "node:module";
33
import path from "node:path";
44

5-
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
6-
import { build } from "esbuild";
5+
import { type BuildOptions, esbuildSync, getPackagePath } from "@opennextjs/aws/build/helper.js";
76

87
export function compileDurableObjects(buildOpts: BuildOptions) {
98
const _require = createRequire(import.meta.url);
@@ -26,16 +25,19 @@ export function compileDurableObjects(buildOpts: BuildOptions) {
2625

2726
const BUILD_ID = fs.readFileSync(path.join(baseManifestPath, "BUILD_ID"), "utf-8");
2827

29-
return build({
30-
entryPoints,
31-
bundle: true,
32-
platform: "node",
33-
format: "esm",
34-
outdir: path.join(buildOpts.buildDir, "durable-objects"),
35-
external: ["cloudflare:workers"],
36-
define: {
37-
"process.env.__NEXT_PREVIEW_MODE_ID": `"${previewModeId}"`,
38-
"process.env.__NEXT_BUILD_ID": `"${BUILD_ID}"`,
28+
return esbuildSync(
29+
{
30+
entryPoints,
31+
bundle: true,
32+
platform: "node",
33+
format: "esm",
34+
outdir: path.join(buildOpts.buildDir, "durable-objects"),
35+
external: ["cloudflare:workers"],
36+
define: {
37+
"process.env.__NEXT_PREVIEW_MODE_ID": `"${previewModeId}"`,
38+
"process.env.__NEXT_BUILD_ID": `"${BUILD_ID}"`,
39+
},
3940
},
40-
});
41+
buildOpts
42+
);
4143
}

0 commit comments

Comments
 (0)