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 ) {
@@ -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
0 commit comments