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

added GetPID to registry #139

Merged
merged 3 commits into from
Dec 30, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ Any event that fulfills the `actor.LogEvent` interface will be logged to the def
message and the attributes of the event set by the `actor.LogEvent` `log()` method.

### List of internal system events
* `actor.ActorInitializedEvent`, an actor has been initialized but did not processed its `actor.Started message`
* `actor.ActorStartedEvent`, an actor has started
* `actor.ActorStoppedEvent`, an actor has stopped
* `actor.DeadLetterEvent`, a message was not delivered to an actor
Expand Down
10 changes: 10 additions & 0 deletions actor/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func newTickReceiver(wg *sync.WaitGroup) Producer {
}
}

func TestRegistryGetPID(t *testing.T) {
e, _ := NewEngine(nil)
expectedPID1 := e.SpawnFunc(func(c *Context) {}, "foo", WithID("1"))
expectedPID2 := e.SpawnFunc(func(c *Context) {}, "foo", WithID("2"))
pid := e.Registry.GetPID("foo", "1")
assert.True(t, pid.Equals(expectedPID1))
pid = e.Registry.GetPID("foo", "2")
assert.True(t, pid.Equals(expectedPID2))
}

func TestSendToNilPID(t *testing.T) {
e, _ := NewEngine(nil)
e.Send(nil, "foo")
Expand Down
11 changes: 11 additions & 0 deletions actor/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func (e ActorStartedEvent) Log() (slog.Level, string, []any) {
return slog.LevelInfo, "Actor started", []any{"pid", e.PID}
}

// ActorInitializedEvent is broadcasted over the eventStream before an actor
// received and processed its started event.
type ActorInitializedEvent struct {
PID *PID
Timestamp time.Time
}

func (e ActorInitializedEvent) Log() (slog.Level, string, []any) {
return slog.LevelDebug, "Actor initialized", []any{"pid", e.PID}
}

// ActorStoppedEvent is broadcasted over the eventStream each time
// a process is terminated.
type ActorStoppedEvent struct {
Expand Down
1 change: 1 addition & 0 deletions actor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (p *process) Start() {
}()
p.context.message = Initialized{}
applyMiddleware(recv.Receive, p.Opts.Middleware...)(p.context)
p.context.engine.BroadcastEvent(ActorInitializedEvent{PID: p.pid, Timestamp: time.Now()})

p.context.message = Started{}
applyMiddleware(recv.Receive, p.Opts.Middleware...)(p.context)
Expand Down
10 changes: 10 additions & 0 deletions actor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ func newRegistry(e *Engine) *Registry {
}
}

// GetPID returns the process id associated for the given kind and its id.
// GetPID returns nil if the process was not found.
func (r *Registry) GetPID(kind, id string) *PID {
proc := r.getByID(kind + pidSeparator + id)
if proc != nil {
return proc.PID()
}
return nil
}

// Remove removes the given PID from the registry.
func (r *Registry) Remove(pid *PID) {
r.mu.Lock()
Expand Down