Skip to content

Commit

Permalink
add Activate and Deactivate interface for actor, do sth when create o…
Browse files Browse the repository at this point in the history
…r remove actors
  • Loading branch information
wXwcoder committed Nov 10, 2022
1 parent c2dfec6 commit 378d309
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions actor/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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
}

type ReminderCallee interface {
Expand Down
11 changes: 10 additions & 1 deletion actor/manager/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type ActorContainer interface {
Invoke(methodName string, param []byte) ([]reflect.Value, actorErr.ActorErr)
GetActor() actor.Server
Deactivate() error
}

// DefaultActorContainer contains actor instance and methods type info generated from actor.
Expand All @@ -43,8 +44,12 @@ func NewDefaultActorContainer(actorID string, impl actor.Server, serializer code
daprClient, _ := dapr.NewClient()
// create state manager for this new actor
impl.SetStateManager(state.NewActorStateManager(impl.Type(), actorID, state.NewDaprStateAsyncProvider(daprClient)))
err := impl.Activate()
if err != nil {
return nil, actorErr.ErrSaveStateFailed
}
// save state of this actor
err := impl.SaveState()
err = impl.SaveState()
if err != nil {
return nil, actorErr.ErrSaveStateFailed
}
Expand Down Expand Up @@ -84,3 +89,7 @@ func (d *DefaultActorContainer) Invoke(methodName string, param []byte) ([]refle
returnValue := methodType.method.Func.Call(argsValues)
return returnValue, actorErr.Success
}

func (d *DefaultActorContainer) Deactivate() error {
return d.actor.Deactivate()
}
3 changes: 2 additions & 1 deletion actor/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ func (m *DefaultActorManager) InvokeMethod(actorID, methodName string, request [

// DeactivateActor removes actor from actor manager.
func (m *DefaultActorManager) DeactivateActor(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 @@ -27,6 +27,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 All @@ -46,6 +54,14 @@ type NotReminderCalleeActor struct {
actor.ServerImplBase
}

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.

0 comments on commit 378d309

Please sign in to comment.