Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add Activate and Deactivate interface for actor, do sth when create … #332

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions actor/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion actor/manager/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions actor/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions actor/mock/mock_factory_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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"
}
8 changes: 8 additions & 0 deletions actor/mock/mock_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading