1- import { error } from "@opennextjs/aws/adapters/logger.js" ;
1+ import { debug , error } from "@opennextjs/aws/adapters/logger.js" ;
22import type { QueueMessage } from "@opennextjs/aws/types/overrides" ;
33import {
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
0 commit comments