@@ -13,14 +13,18 @@ import * as errors from "./errors";
1313import { processMessage } from "./protocol/message/mod" ;
1414import { instanceLogger , logger } from "./log" ;
1515import type { ActionContext } from "./action" ;
16- import { Lock , deadline } from "./utils" ;
16+ import { DeadlineError , Lock , deadline } from "./utils" ;
1717import { Schedule } from "./schedule" ;
1818import type * as wsToServer from "@/actor/protocol/message/to-server" ;
1919import { CachedSerializer } from "./protocol/serde" ;
2020import { ActorInspector } from "@/inspector/actor" ;
2121import { ActorContext } from "./context" ;
2222import invariant from "invariant" ;
23- import type { PersistedActor , PersistedConn , PersistedScheduleEvents } from "./persisted" ;
23+ import type {
24+ PersistedActor ,
25+ PersistedConn ,
26+ PersistedScheduleEvents ,
27+ } from "./persisted" ;
2428
2529/**
2630 * Options for the `_saveState` method.
@@ -160,9 +164,6 @@ export class ActorInstance<S, CP, CS, V> {
160164
161165 // TODO: Exit process if this errors
162166 if ( this . #varsEnabled) {
163- // TODO: Move to config
164- const CREATE_VARS_TIMEOUT = 5000 ; // 5 seconds
165-
166167 let vars : V | undefined = undefined ;
167168 if ( "createVars" in this . #config) {
168169 const dataOrPromise = this . #config. createVars (
@@ -175,7 +176,10 @@ export class ActorInstance<S, CP, CS, V> {
175176 this . #actorDriver. getContext ( this . #actorId) ,
176177 ) ;
177178 if ( dataOrPromise instanceof Promise ) {
178- vars = await deadline ( dataOrPromise , CREATE_VARS_TIMEOUT ) ;
179+ vars = await deadline (
180+ dataOrPromise ,
181+ this . #config. options . lifecycle . createVarsTimeout ,
182+ ) ;
179183 } else {
180184 vars = dataOrPromise ;
181185 }
@@ -214,10 +218,10 @@ export class ActorInstance<S, CP, CS, V> {
214218 ar : args ,
215219 } ;
216220
217- this . actorContext . log . info ( "scheduling event" , {
218- event : eventId ,
219- timestamp,
220- action : fn
221+ this . actorContext . log . info ( "scheduling event" , {
222+ event : eventId ,
223+ timestamp,
224+ action : fn ,
221225 } ) ;
222226
223227 // Insert event in to index
@@ -239,7 +243,10 @@ export class ActorInstance<S, CP, CS, V> {
239243
240244 async onAlarm ( ) {
241245 const now = Date . now ( ) ;
242- this . actorContext . log . debug ( "alarm triggered" , { now, events : this . #persist. e . length } ) ;
246+ this . actorContext . log . debug ( "alarm triggered" , {
247+ now,
248+ events : this . #persist. e . length ,
249+ } ) ;
243250
244251 // Remove events from schedule that we're about to run
245252 const runIndex = this . #persist. e . findIndex ( ( x ) => x . t <= now ) ;
@@ -248,7 +255,9 @@ export class ActorInstance<S, CP, CS, V> {
248255 return ;
249256 }
250257 const scheduleEvents = this . #persist. e . splice ( 0 , runIndex + 1 ) ;
251- this . actorContext . log . debug ( "running events" , { count : scheduleEvents . length } ) ;
258+ this . actorContext . log . debug ( "running events" , {
259+ count : scheduleEvents . length ,
260+ } ) ;
252261
253262 // Set alarm for next event
254263 if ( this . #persist. e . length > 0 ) {
@@ -258,13 +267,13 @@ export class ActorInstance<S, CP, CS, V> {
258267 // Iterate by event key in order to ensure we call the events in order
259268 for ( const event of scheduleEvents ) {
260269 try {
261- this . actorContext . log . info ( "running action for event" , {
262- event : event . e ,
263- timestamp : event . t ,
264- action : event . a ,
265- args : event . ar
270+ this . actorContext . log . info ( "running action for event" , {
271+ event : event . e ,
272+ timestamp : event . t ,
273+ action : event . a ,
274+ args : event . ar ,
266275 } ) ;
267-
276+
268277 // Look up function
269278 const fn : unknown = this . #config. actions [ event . a ] ;
270279 if ( ! fn ) throw new Error ( `Missing action for alarm ${ event . a } ` ) ;
@@ -586,7 +595,6 @@ export class ActorInstance<S, CP, CS, V> {
586595 ) : Promise < CS > {
587596 // Authenticate connection
588597 let connState : CS | undefined = undefined ;
589- const PREPARE_CONNECT_TIMEOUT = 5000 ; // 5 seconds
590598
591599 const onBeforeConnectOpts = {
592600 request,
@@ -612,7 +620,10 @@ export class ActorInstance<S, CP, CS, V> {
612620 onBeforeConnectOpts ,
613621 ) ;
614622 if ( dataOrPromise instanceof Promise ) {
615- connState = await deadline ( dataOrPromise , PREPARE_CONNECT_TIMEOUT ) ;
623+ connState = await deadline (
624+ dataOrPromise ,
625+ this . #config. options . lifecycle . createConnStateTimeout ,
626+ ) ;
616627 } else {
617628 connState = dataOrPromise ;
618629 }
@@ -676,12 +687,14 @@ export class ActorInstance<S, CP, CS, V> {
676687 this . inspector . onConnChange ( this . #connections) ;
677688
678689 // Handle connection
679- const CONNECT_TIMEOUT = 5000 ; // 5 seconds
680690 if ( this . #config. onConnect ) {
681691 try {
682692 const result = this . #config. onConnect ( this . actorContext , conn ) ;
683693 if ( result instanceof Promise ) {
684- deadline ( result , CONNECT_TIMEOUT ) . catch ( ( error ) => {
694+ deadline (
695+ result ,
696+ this . #config. options . lifecycle . onConnectTimeout ,
697+ ) . catch ( ( error ) => {
685698 logger ( ) . error ( "error in `onConnect`, closing socket" , {
686699 error,
687700 } ) ;
@@ -842,13 +855,22 @@ export class ActorInstance<S, CP, CS, V> {
842855 // TODO: Manually call abortable for better error handling
843856 // Call the function on this object with those arguments
844857 try {
858+ // Log when we start executing the action
859+ logger ( ) . debug ( "executing action" , { actionName : rpcName , args } ) ;
860+
845861 const outputOrPromise = rpcFunction . call ( undefined , ctx , ...args ) ;
846862 let output : unknown ;
847863 if ( outputOrPromise instanceof Promise ) {
864+ // Log that we're waiting for an async action
865+ logger ( ) . debug ( "awaiting async action" , { actionName : rpcName } ) ;
866+
848867 output = await deadline (
849868 outputOrPromise ,
850869 this . #config. options . action . timeout ,
851870 ) ;
871+
872+ // Log that async action completed
873+ logger ( ) . debug ( "async action completed" , { actionName : rpcName } ) ;
852874 } else {
853875 output = outputOrPromise ;
854876 }
@@ -863,7 +885,13 @@ export class ActorInstance<S, CP, CS, V> {
863885 output ,
864886 ) ;
865887 if ( processedOutput instanceof Promise ) {
888+ logger ( ) . debug ( "awaiting onBeforeActionResponse" , {
889+ actionName : rpcName ,
890+ } ) ;
866891 output = await processedOutput ;
892+ logger ( ) . debug ( "onBeforeActionResponse completed" , {
893+ actionName : rpcName ,
894+ } ) ;
867895 } else {
868896 output = processedOutput ;
869897 }
@@ -874,11 +902,22 @@ export class ActorInstance<S, CP, CS, V> {
874902 }
875903 }
876904
905+ // Log the output before returning
906+ logger ( ) . debug ( "action completed" , {
907+ actionName : rpcName ,
908+ outputType : typeof output ,
909+ isPromise : output instanceof Promise ,
910+ } ) ;
911+
877912 return output ;
878913 } catch ( error ) {
879- if ( error instanceof DOMException && error . name === "TimeoutError" ) {
914+ if ( error instanceof DeadlineError ) {
880915 throw new errors . ActionTimedOut ( ) ;
881916 }
917+ logger ( ) . error ( "action error" , {
918+ actionName : rpcName ,
919+ error : stringifyError ( error ) ,
920+ } ) ;
882921 throw error ;
883922 } finally {
884923 this . #savePersistThrottled( ) ;
0 commit comments