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 ,
@@ -45,7 +45,10 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
4545 this . sql = ctx . storage . sql ;
4646
4747 // We restore the state
48- ctx . blockConcurrencyWhile ( ( ) => this . initState ( ) ) ;
48+ ctx . blockConcurrencyWhile ( async ( ) => {
49+ debug ( `Restoring the state of the durable object` ) ;
50+ await this . initState ( ) ;
51+ } ) ;
4952
5053 this . maxRevalidations = env . MAX_REVALIDATION_BY_DURABLE_OBJECT
5154 ? parseInt ( env . MAX_REVALIDATION_BY_DURABLE_OBJECT )
@@ -62,6 +65,8 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
6265 this . maxRevalidationAttempts = env . MAX_REVALIDATION_ATTEMPTS
6366 ? parseInt ( env . MAX_REVALIDATION_ATTEMPTS )
6467 : DEFAULT_MAX_REVALIDATION_ATTEMPTS ;
68+
69+ debug ( `Durable object initialized` ) ;
6570 }
6671
6772 async revalidate ( msg : QueueMessage ) {
@@ -72,11 +77,17 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
7277 if ( this . routeInFailedState . has ( msg . MessageDeduplicationId ) ) return ;
7378
7479 if ( this . ongoingRevalidations . size >= this . maxRevalidations ) {
80+ debug (
81+ `The maximum number of revalidations (${ this . maxRevalidations } ) is reached. Blocking until one of the revalidations finishes.`
82+ ) ;
7583 const ongoingRevalidations = this . ongoingRevalidations . values ( ) ;
7684 // When there is more than the max revalidations, we block concurrency until one of the revalidations finishes
7785 // We still await the promise to ensure the revalidation is completed
7886 // This is fine because the queue itself run inside a waitUntil
79- await this . ctx . blockConcurrencyWhile ( ( ) => Promise . race ( ongoingRevalidations ) ) ;
87+ await this . ctx . blockConcurrencyWhile ( async ( ) => {
88+ debug ( `Waiting for one of the revalidations to finish` ) ;
89+ await Promise . race ( ongoingRevalidations ) ;
90+ } ) ;
8091 }
8192
8293 const revalidationPromise = this . executeRevalidation ( msg ) ;
@@ -90,6 +101,7 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
90101
91102 private async executeRevalidation ( msg : QueueMessage ) {
92103 try {
104+ debug ( `Revalidating ${ msg . MessageBody . host } ${ msg . MessageBody . url } ` ) ;
93105 const {
94106 MessageBody : { host, url } ,
95107 } = msg ;
@@ -166,12 +178,14 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
166178 ) ;
167179 const allEventsToRetry = nextEventToRetry ? [ nextEventToRetry , ...expiredEvents ] : expiredEvents ;
168180 for ( const event of allEventsToRetry ) {
181+ debug ( `Retrying revalidation for ${ event . msg . MessageBody . host } ${ event . msg . MessageBody . url } ` ) ;
169182 await this . executeRevalidation ( event . msg ) ;
170183 this . routeInFailedState . delete ( event . msg . MessageDeduplicationId ) ;
171184 }
172185 }
173186
174187 async addToFailedState ( msg : QueueMessage ) {
188+ debug ( `Adding ${ msg . MessageBody . host } ${ msg . MessageBody . url } to the failed state` ) ;
175189 const existingFailedState = this . routeInFailedState . get ( msg . MessageDeduplicationId ) ;
176190
177191 let updatedFailedState : FailedState ;
0 commit comments