Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
[#128]: fix: panic if there are no workers
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Jun 26, 2024
2 parents 9b80484 + afb64bd commit 9837866
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ jobs:
- name: Run linter
uses: golangci/golangci-lint-action@v6.0.1 # Action page: <https://github.com/golangci/golangci-lint-action>
with:
version: v1.57 # without patch version
version: v1.59 # without patch version
only-new-issues: false # show only new issues if it's a pull request
args: --timeout=10m --build-tags=race
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["8.2"]
php: ["8.3"]
go: [stable]
os: ["ubuntu-latest"]
steps:
Expand Down
3 changes: 2 additions & 1 deletion pool/static_pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ func Test_StaticPool_RemoveWorker(t *testing.T) {
_, err = p.Exec(ctx, &payload.Payload{Body: []byte("hello"), Context: nil}, make(chan struct{}))
assert.NoError(t, err)

assert.Len(t, p.Workers(), 1)
// after removing all workers, we should have 1 worker + 1 we added
assert.Len(t, p.Workers(), 2)

p.Destroy(ctx)
}
Expand Down
4 changes: 2 additions & 2 deletions pool/static_pool/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func Test_SupervisedPool_RemoveNoWorkers(t *testing.T) {
assert.NoError(t, p.RemoveWorker(ctx))
}

assert.Len(t, p.Workers(), 0)
assert.Len(t, p.Workers(), 1)
p.Destroy(ctx)
}

Expand Down Expand Up @@ -208,7 +208,7 @@ func Test_SupervisedPool_RemoveWorker(t *testing.T) {
_, err = p.Exec(ctx, &payload.Payload{Body: []byte("hello"), Context: nil}, make(chan struct{}))
assert.NoError(t, err)

assert.Len(t, p.Workers(), 1)
assert.Len(t, p.Workers(), 2)

p.Destroy(ctx)
}
Expand Down
10 changes: 10 additions & 0 deletions worker_watcher/worker_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ func (ww *WorkerWatcher) RemoveWorker(ctx context.Context) error {
return err
}

// can't remove the last worker
if atomic.LoadUint64(&ww.numWorkers) == 1 {
ww.log.Warn("can't remove the last worker", zap.Int64("pid", w.Pid()))
return nil
}

// destroy and stop
w.State().Transition(fsm.StateDestroyed)
_ = w.Stop()
Expand Down Expand Up @@ -403,6 +409,10 @@ func (ww *WorkerWatcher) wait(w *worker.Process) {
err = ww.Allocate()
if err != nil {
ww.log.Error("failed to allocate the worker", zap.String("internal_event_name", events.EventWorkerError.String()), zap.Error(err))
if atomic.LoadUint64(&ww.numWorkers) == 0 {
panic("no workers available, can't run the application")
}

return
}

Expand Down

0 comments on commit 9837866

Please sign in to comment.