Skip to content

Commit cbcedbf

Browse files
committed
added debug info
1 parent e95413c commit cbcedbf

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) {
@@ -73,11 +78,17 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
7378
if (this.routeInFailedState.has(msg.MessageDeduplicationId)) return;
7479

7580
if (this.ongoingRevalidations.size >= this.maxRevalidations) {
81+
debug(
82+
`The maximum number of revalidations (${this.maxRevalidations}) is reached. Blocking until one of the revalidations finishes.`
83+
);
7684
const ongoingRevalidations = this.ongoingRevalidations.values();
7785
// When there is more than the max revalidations, we block concurrency until one of the revalidations finishes
7886
// We still await the promise to ensure the revalidation is completed
7987
// This is fine because the queue itself run inside a waitUntil
80-
await this.ctx.blockConcurrencyWhile(() => Promise.race(ongoingRevalidations));
88+
await this.ctx.blockConcurrencyWhile(async () => {
89+
debug(`Waiting for one of the revalidations to finish`);
90+
await Promise.race(ongoingRevalidations);
91+
});
8192
}
8293

8394
const revalidationPromise = this.executeRevalidation(msg);
@@ -91,6 +102,7 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
91102

92103
private async executeRevalidation(msg: QueueMessage) {
93104
try {
105+
debug(`Revalidating ${msg.MessageBody.host}${msg.MessageBody.url}`);
94106
const {
95107
MessageBody: { host, url },
96108
} = msg;
@@ -169,12 +181,14 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
169181
? [nextEventToRetry, ...expiredEvents]
170182
: expiredEvents;
171183
for (const event of allEventsToRetry) {
184+
debug(`Retrying revalidation for ${event.msg.MessageBody.host}${event.msg.MessageBody.url}`);
172185
await this.executeRevalidation(event.msg);
173186
this.routeInFailedState.delete(event.msg.MessageDeduplicationId);
174187
}
175188
}
176189

177190
async addToFailedState(msg: QueueMessage) {
191+
debug(`Adding ${msg.MessageBody.host}${msg.MessageBody.url} to the failed state`);
178192
const existingFailedState = this.routeInFailedState.get(msg.MessageDeduplicationId);
179193
let nextAlarm = Date.now() + this.revalidationRetryInterval;
180194

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)