diff --git a/actor/actor.go b/actor/actor.go index c14d6550..a4df241e 100644 --- a/actor/actor.go +++ b/actor/actor.go @@ -42,6 +42,10 @@ type Server interface { // SaveState is impl by ServerImplBase, It saves the state cache of this actor instance to state store component by calling api of daprd. // Save state is called at two places: 1. On invocation of this actor instance. 2. When new actor starts. SaveState() error + // Activate called when actor created by actor manager + Activate() error + // Deactivate called before actor removed by actor manager + Deactivate() error WithContext() ServerContext } @@ -64,6 +68,10 @@ type ServerContext interface { // SaveState is impl by ServerImplBase, It saves the state cache of this actor instance to state store component by calling api of daprd. // Save state is called at two places: 1. On invocation of this actor instance. 2. When new actor starts. SaveState(context.Context) error + // Activate called when actor created by actor manager + Activate() error + // Deactivate called before actor removed by actor manager + Deactivate() error } type ReminderCallee interface { @@ -131,6 +139,16 @@ func (b *ServerImplBase) SaveState() error { return b.ctx.SaveState(context.Background()) } +// Activate when actor created by actor manager +func (b *ServerImplBase) Activate() error { + return nil +} + +// Deactivate before actor removed by actor manager +func (b *ServerImplBase) Deactivate() error { + return nil +} + // Deprecated: Use ServerImplBaseCtx instead. func (b *ServerImplBase) WithContext() *ServerImplBaseCtx { b.ctx.lock.RLock() @@ -179,6 +197,16 @@ func (b *ServerImplBaseCtx) SaveState(ctx context.Context) error { return nil } +// Activate when actor created by actor manager +func (b *ServerImplBaseCtx) Activate() error { + return nil +} + +// Deactivate before actor removed by actor manager +func (b *ServerImplBaseCtx) Deactivate() error { + return nil +} + // Deprecated: StateManager is deprecated in favour of StateManagerContext. type StateManager interface { // Add is to add new state store with @stateName and @value diff --git a/actor/manager/container.go b/actor/manager/container.go index 29619db4..54df9050 100644 --- a/actor/manager/container.go +++ b/actor/manager/container.go @@ -30,6 +30,7 @@ type ActorContainer interface { Invoke(methodName string, param []byte) ([]reflect.Value, actorErr.ActorErr) //nolint:staticcheck // SA1019 Deprecated: use ActorContainerContext instead. GetActor() actor.Server + Deactivate() error } type ActorContainerContext interface { @@ -80,8 +81,12 @@ func NewDefaultActorContainerContext(ctx context.Context, actorID string, impl a daprClient, _ := dapr.NewClient() // create state manager for this new actor impl.SetStateManager(state.NewActorStateManagerContext(impl.Type(), actorID, state.NewDaprStateAsyncProvider(daprClient))) + err := impl.Activate() + if err != nil { + return nil, actorErr.ErrSaveStateFailed + } // save state of this actor - err := impl.SaveState(ctx) + err = impl.SaveState(ctx) if err != nil { return nil, actorErr.ErrSaveStateFailed } @@ -118,6 +123,10 @@ func (d *DefaultActorContainerContext) Invoke(ctx context.Context, methodName st return returnValue, actorErr.Success } +func (d *DefaultActorContainer) Deactivate() error { + return d.actor.Deactivate() +} + func (d *DefaultActorContainerContext) GetActor() actor.ServerContext { return d.actor } diff --git a/actor/manager/manager.go b/actor/manager/manager.go index 87696255..010bd149 100644 --- a/actor/manager/manager.go +++ b/actor/manager/manager.go @@ -164,12 +164,12 @@ func (m *DefaultActorManagerContext) InvokeMethod(ctx context.Context, actorID, return rspData, actorErr.Success } -// DeactivateActor removes actor from actor manager. func (m *DefaultActorManagerContext) DeactivateActor(_ context.Context, actorID string) actorErr.ActorErr { - _, ok := m.activeActors.Load(actorID) + actor, ok := m.activeActors.Load(actorID) if !ok { return actorErr.ErrActorIDNotFound } + actor.(ActorContainer).Deactivate() m.activeActors.Delete(actorID) return actorErr.Success } diff --git a/actor/mock/mock_factory_impl.go b/actor/mock/mock_factory_impl.go index 61a463e4..67640a5f 100644 --- a/actor/mock/mock_factory_impl.go +++ b/actor/mock/mock_factory_impl.go @@ -28,6 +28,14 @@ type ActorImpl struct { actor.ServerImplBase } +func (t *ActorImpl) Activate() error { + return nil +} + +func (t *ActorImpl) Deactivate() error { + return nil +} + func (t *ActorImpl) Type() string { return "testActorType" } @@ -70,6 +78,14 @@ type NotReminderCalleeActor struct { actor.ServerImplBaseCtx } +func (t *NotReminderCalleeActor) Activate() error { + return nil +} + +func (t *NotReminderCalleeActor) Deactivate() error { + return nil +} + func (t *NotReminderCalleeActor) Type() string { return "testActorNotReminderCalleeType" } diff --git a/actor/mock/mock_server.go b/actor/mock/mock_server.go index 882dca9e..c23c65cc 100644 --- a/actor/mock/mock_server.go +++ b/actor/mock/mock_server.go @@ -152,6 +152,14 @@ func (mr *MockServerMockRecorder) Type() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Type", reflect.TypeOf((*MockServer)(nil).Type)) } +func (m *MockServer) Activate() error { + return nil +} + +func (m *MockServer) Deactivate() error { + return nil +} + // WithContext mocks base method. func (m *MockServer) WithContext() actor.ServerContext { m.ctrl.T.Helper()