Skip to content

Commit

Permalink
refactor: cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey committed Sep 11, 2024
1 parent 380849f commit 5af2c80
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions actors/pid.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (pid *PID) Stop(ctx context.Context, cid *PID) error {
// IsRunning returns true when the actor is alive ready to process messages and false
// when the actor is stopped or not started at all
func (pid *PID) IsRunning() bool {
return pid != nil && pid != NoSender && pid.running.Load()
return pid.running.Load()
}

// ActorSystem returns the actor system
Expand Down Expand Up @@ -943,12 +943,11 @@ func (pid *PID) RemoteReSpawn(ctx context.Context, host string, port int, name s
// that can be configured. All child actors will be gracefully shutdown.
func (pid *PID) Shutdown(ctx context.Context) error {
pid.stopLocker.Lock()
defer pid.stopLocker.Unlock()

pid.logger.Info("Shutdown process has started...")

if !pid.running.Load() {
pid.logger.Infof("Actor=%s is offline. Maybe it has been passivated or stopped already", pid.Address().String())
pid.stopLocker.Unlock()
return nil
}

Expand All @@ -959,6 +958,7 @@ func (pid *PID) Shutdown(ctx context.Context) error {

if err := pid.doStop(ctx); err != nil {
pid.logger.Errorf("failed to cleanly stop actor=(%s)", pid.ID())
pid.stopLocker.Unlock()
return err
}

Expand All @@ -969,6 +969,7 @@ func (pid *PID) Shutdown(ctx context.Context) error {
})
}

pid.stopLocker.Unlock()
pid.logger.Infof("Actor=%s successfully shutdown", pid.ID())
return nil
}
Expand Down Expand Up @@ -1029,20 +1030,16 @@ func (pid *PID) init(ctx context.Context) error {
pid.logger.Info("Initialization process has started...")

cancelCtx, cancel := context.WithTimeout(ctx, pid.initTimeout.Load())
defer cancel()

// create a new retrier that will try a maximum of `initMaxRetries` times, with
// an initial delay of 100 ms and a maximum delay of 1 second
retrier := retry.NewRetrier(int(pid.initMaxRetries.Load()), 100*time.Millisecond, time.Second)
if err := retrier.RunContext(cancelCtx, pid.actor.PreStart); err != nil {
e := ErrInitFailure(err)
cancel()
return e
}

pid.fieldsLocker.Lock()
pid.running.Store(true)
pid.fieldsLocker.Unlock()

pid.logger.Info("Initialization process successfully completed.")

if pid.eventsStream != nil {
Expand All @@ -1052,6 +1049,7 @@ func (pid *PID) init(ctx context.Context) error {
})
}

cancel()
return nil
}

Expand Down

0 comments on commit 5af2c80

Please sign in to comment.