@@ -13,14 +13,18 @@ import * as errors from "./errors";
13
13
import { processMessage } from "./protocol/message/mod" ;
14
14
import { instanceLogger , logger } from "./log" ;
15
15
import type { ActionContext } from "./action" ;
16
- import { Lock , deadline } from "./utils" ;
16
+ import { DeadlineError , Lock , deadline } from "./utils" ;
17
17
import { Schedule } from "./schedule" ;
18
18
import type * as wsToServer from "@/actor/protocol/message/to-server" ;
19
19
import { CachedSerializer } from "./protocol/serde" ;
20
20
import { ActorInspector } from "@/inspector/actor" ;
21
21
import { ActorContext } from "./context" ;
22
22
import invariant from "invariant" ;
23
- import type { PersistedActor , PersistedConn , PersistedScheduleEvents } from "./persisted" ;
23
+ import type {
24
+ PersistedActor ,
25
+ PersistedConn ,
26
+ PersistedScheduleEvents ,
27
+ } from "./persisted" ;
24
28
25
29
/**
26
30
* Options for the `_saveState` method.
@@ -160,9 +164,6 @@ export class ActorInstance<S, CP, CS, V> {
160
164
161
165
// TODO: Exit process if this errors
162
166
if ( this . #varsEnabled) {
163
- // TODO: Move to config
164
- const CREATE_VARS_TIMEOUT = 5000 ; // 5 seconds
165
-
166
167
let vars : V | undefined = undefined ;
167
168
if ( "createVars" in this . #config) {
168
169
const dataOrPromise = this . #config. createVars (
@@ -175,7 +176,10 @@ export class ActorInstance<S, CP, CS, V> {
175
176
this . #actorDriver. getContext ( this . #actorId) ,
176
177
) ;
177
178
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
+ ) ;
179
183
} else {
180
184
vars = dataOrPromise ;
181
185
}
@@ -214,10 +218,10 @@ export class ActorInstance<S, CP, CS, V> {
214
218
ar : args ,
215
219
} ;
216
220
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 ,
221
225
} ) ;
222
226
223
227
// Insert event in to index
@@ -239,7 +243,10 @@ export class ActorInstance<S, CP, CS, V> {
239
243
240
244
async onAlarm ( ) {
241
245
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
+ } ) ;
243
250
244
251
// Remove events from schedule that we're about to run
245
252
const runIndex = this . #persist. e . findIndex ( ( x ) => x . t <= now ) ;
@@ -248,7 +255,9 @@ export class ActorInstance<S, CP, CS, V> {
248
255
return ;
249
256
}
250
257
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
+ } ) ;
252
261
253
262
// Set alarm for next event
254
263
if ( this . #persist. e . length > 0 ) {
@@ -258,13 +267,13 @@ export class ActorInstance<S, CP, CS, V> {
258
267
// Iterate by event key in order to ensure we call the events in order
259
268
for ( const event of scheduleEvents ) {
260
269
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 ,
266
275
} ) ;
267
-
276
+
268
277
// Look up function
269
278
const fn : unknown = this . #config. actions [ event . a ] ;
270
279
if ( ! fn ) throw new Error ( `Missing action for alarm ${ event . a } ` ) ;
@@ -586,7 +595,6 @@ export class ActorInstance<S, CP, CS, V> {
586
595
) : Promise < CS > {
587
596
// Authenticate connection
588
597
let connState : CS | undefined = undefined ;
589
- const PREPARE_CONNECT_TIMEOUT = 5000 ; // 5 seconds
590
598
591
599
const onBeforeConnectOpts = {
592
600
request,
@@ -612,7 +620,10 @@ export class ActorInstance<S, CP, CS, V> {
612
620
onBeforeConnectOpts ,
613
621
) ;
614
622
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
+ ) ;
616
627
} else {
617
628
connState = dataOrPromise ;
618
629
}
@@ -676,12 +687,14 @@ export class ActorInstance<S, CP, CS, V> {
676
687
this . inspector . onConnChange ( this . #connections) ;
677
688
678
689
// Handle connection
679
- const CONNECT_TIMEOUT = 5000 ; // 5 seconds
680
690
if ( this . #config. onConnect ) {
681
691
try {
682
692
const result = this . #config. onConnect ( this . actorContext , conn ) ;
683
693
if ( result instanceof Promise ) {
684
- deadline ( result , CONNECT_TIMEOUT ) . catch ( ( error ) => {
694
+ deadline (
695
+ result ,
696
+ this . #config. options . lifecycle . onConnectTimeout ,
697
+ ) . catch ( ( error ) => {
685
698
logger ( ) . error ( "error in `onConnect`, closing socket" , {
686
699
error,
687
700
} ) ;
@@ -842,13 +855,22 @@ export class ActorInstance<S, CP, CS, V> {
842
855
// TODO: Manually call abortable for better error handling
843
856
// Call the function on this object with those arguments
844
857
try {
858
+ // Log when we start executing the action
859
+ logger ( ) . debug ( "executing action" , { actionName : rpcName , args } ) ;
860
+
845
861
const outputOrPromise = rpcFunction . call ( undefined , ctx , ...args ) ;
846
862
let output : unknown ;
847
863
if ( outputOrPromise instanceof Promise ) {
864
+ // Log that we're waiting for an async action
865
+ logger ( ) . debug ( "awaiting async action" , { actionName : rpcName } ) ;
866
+
848
867
output = await deadline (
849
868
outputOrPromise ,
850
869
this . #config. options . action . timeout ,
851
870
) ;
871
+
872
+ // Log that async action completed
873
+ logger ( ) . debug ( "async action completed" , { actionName : rpcName } ) ;
852
874
} else {
853
875
output = outputOrPromise ;
854
876
}
@@ -863,7 +885,13 @@ export class ActorInstance<S, CP, CS, V> {
863
885
output ,
864
886
) ;
865
887
if ( processedOutput instanceof Promise ) {
888
+ logger ( ) . debug ( "awaiting onBeforeActionResponse" , {
889
+ actionName : rpcName ,
890
+ } ) ;
866
891
output = await processedOutput ;
892
+ logger ( ) . debug ( "onBeforeActionResponse completed" , {
893
+ actionName : rpcName ,
894
+ } ) ;
867
895
} else {
868
896
output = processedOutput ;
869
897
}
@@ -874,11 +902,22 @@ export class ActorInstance<S, CP, CS, V> {
874
902
}
875
903
}
876
904
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
+
877
912
return output ;
878
913
} catch ( error ) {
879
- if ( error instanceof DOMException && error . name === "TimeoutError" ) {
914
+ if ( error instanceof DeadlineError ) {
880
915
throw new errors . ActionTimedOut ( ) ;
881
916
}
917
+ logger ( ) . error ( "action error" , {
918
+ actionName : rpcName ,
919
+ error : stringifyError ( error ) ,
920
+ } ) ;
882
921
throw error ;
883
922
} finally {
884
923
this . #savePersistThrottled( ) ;
0 commit comments