You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rivetkit-typescript/packages/rivetkit/src/actor/instance.ts
+29-13Lines changed: 29 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -150,7 +150,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
150
150
#stopCalled =false;
151
151
152
152
getisStopping(){
153
-
returnthis.#stopCalled||this.#sleepCalled;
153
+
returnthis.#stopCalled;
154
154
}
155
155
156
156
#persistChanged =false;
@@ -1294,8 +1294,6 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1294
1294
1295
1295
#assertReady(allowStoppingState: boolean=false){
1296
1296
if(!this.#ready)thrownewerrors.InternalError("Actor not ready");
1297
-
if(!allowStoppingState&&this.#sleepCalled)
1298
-
thrownewerrors.InternalError("Actor is going to sleep");
1299
1297
if(!allowStoppingState&&this.#stopCalled)
1300
1298
thrownewerrors.InternalError("Actor is stopping");
1301
1299
}
@@ -1930,7 +1928,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1930
1928
1931
1929
if(canSleep){
1932
1930
this.#sleepTimeout =setTimeout(()=>{
1933
-
this._sleep();
1931
+
this._startSleep();
1934
1932
},this.#config.options.sleepTimeout);
1935
1933
}
1936
1934
}
@@ -1958,21 +1956,33 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1958
1956
returntrue;
1959
1957
}
1960
1958
1961
-
/** Puts an actor to sleep. This should just start the sleep sequence, most shutdown logic should be in _stop (which is called by the ActorDriver when sleeping). */
1962
-
_sleep(){
1959
+
/**
1960
+
* Puts an actor to sleep. This should just start the sleep sequence, most shutdown logic should be in _stop (which is called by the ActorDriver when sleeping).
1961
+
*
1962
+
* For the engine, this will:
1963
+
* 1. Publish EventActorIntent with ActorIntentSleep (via driver.startSleep)
1964
+
* 2. Engine runner will wait for CommandStopActor
1965
+
* 3. Engine runner will call _onStop and wait for it to finish
1966
+
* 4. Engine runner will publish EventActorStateUpdate with ActorStateSTop
1967
+
**/
1968
+
_startSleep(){
1969
+
// IMPORTANT: #sleepCalled should have no effect on the actor's
1970
+
// behavior aside from preventing calling _startSleep twice. Wait for
1971
+
// `_onStop` before putting in a stopping state.
1972
+
if(this.#sleepCalled){
1973
+
this.#rLog.warn({msg: "already sleeping actor"});
1974
+
return;
1975
+
}
1976
+
this.#sleepCalled =true;
1977
+
1978
+
// NOTE: Publishes ActorIntentSleep
1963
1979
constsleep=this.#actorDriver.startSleep?.bind(
1964
1980
this.#actorDriver,
1965
1981
this.#actorId,
1966
1982
);
1967
1983
invariant(this.#sleepingSupported,"sleeping not supported");
1968
1984
invariant(sleep,"no sleep on driver");
1969
1985
1970
-
if(this.#sleepCalled){
1971
-
this.#rLog.warn({msg: "already sleeping actor"});
1972
-
return;
1973
-
}
1974
-
this.#sleepCalled =true;
1975
-
1976
1986
this.#rLog.info({msg: "actor sleeping"});
1977
1987
1978
1988
// Schedule sleep to happen on the next tick. This allows for any action that calls _sleep to complete.
@@ -1985,7 +1995,13 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
1985
1995
}
1986
1996
1987
1997
// MARK: Stop
1988
-
async_stop(){
1998
+
/**
1999
+
* For the engine:
2000
+
* 1. Engine runner receives CommandStopActor
2001
+
* 2. Engine runner calls _onStop and waits for it to finish
2002
+
* 3. Engine runner publishes EventActorStateUpdate with ActorStateSTop
0 commit comments