Skip to content
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
4 changes: 2 additions & 2 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func (p *Plugin) Serve() chan error {
p.mu.Lock()
defer p.mu.Unlock()

var err error
p.pool, err = p.server.NewPool(context.Background(), p.cfg.Pool, map[string]string{RrMode: RrModeHTTP}, p.log)
pl, err := p.server.NewPool(context.Background(), p.cfg.Pool, map[string]string{RrMode: RrModeHTTP}, p.log)
if err != nil {
errCh <- err
return errCh
}
p.pool = pl

p.handler, err = handler.NewHandler(
p.cfg,
Expand Down
28 changes: 8 additions & 20 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import (
"net/http"

"github.com/roadrunner-server/api-plugins/v6/status"
"github.com/roadrunner-server/pool/v2/fsm"
)

// Status return status of the particular plugin
func (p *Plugin) Status() (*status.Status, error) {
p.mu.RLock()
defer p.mu.RUnlock()

if p.pool == nil {
return &status.Status{
Code: http.StatusServiceUnavailable,
}, nil
}

workers := p.pool.Workers()

for _, w := range workers {
Expand All @@ -27,24 +32,7 @@ func (p *Plugin) Status() (*status.Status, error) {
}, nil
}

// Ready return readiness status of the particular plugin
// Ready returns readiness status of the particular plugin.
func (p *Plugin) Ready() (*status.Status, error) {
p.mu.RLock()
defer p.mu.RUnlock()

workers := p.pool.Workers()

for _, w := range workers {
// If state of the worker is ready (at least 1)
// we assume, that plugin's worker pool is ready
if w.State().Compare(fsm.StateReady) {
return &status.Status{
Code: http.StatusOK,
}, nil
}
}
// if there are no workers, threat this as no content error
return &status.Status{
Code: http.StatusServiceUnavailable,
}, nil
return p.Status()
}
23 changes: 15 additions & 8 deletions status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"testing"

"github.com/roadrunner-server/http/v6/api"
"github.com/roadrunner-server/pool/v2/fsm"
"github.com/roadrunner-server/pool/v2/payload"
staticPool "github.com/roadrunner-server/pool/v2/pool/static_pool"
Expand Down Expand Up @@ -47,40 +48,46 @@ func newWorkerInState(t *testing.T, state int64) *worker.Process {
func TestPluginStatusAndReady(t *testing.T) {
tests := []struct {
name string
workers []*worker.Process
pool api.Pool
statusCode int
readyCode int
}{
{
name: "nil pool",
pool: nil,
statusCode: http.StatusServiceUnavailable,
readyCode: http.StatusServiceUnavailable,
},
{
name: "no workers",
workers: nil,
pool: &mockPool{},
statusCode: http.StatusServiceUnavailable,
readyCode: http.StatusServiceUnavailable,
},
{
name: "ready worker",
workers: []*worker.Process{newWorkerInState(t, fsm.StateReady)},
pool: &mockPool{workers: []*worker.Process{newWorkerInState(t, fsm.StateReady)}},
statusCode: http.StatusOK,
readyCode: http.StatusOK,
},
{
name: "inactive worker",
workers: []*worker.Process{newWorkerInState(t, fsm.StateInactive)},
pool: &mockPool{workers: []*worker.Process{newWorkerInState(t, fsm.StateInactive)}},
statusCode: http.StatusServiceUnavailable,
readyCode: http.StatusServiceUnavailable,
},
{
// a busy worker is active but not ready, which is what separates the two
// An active working worker satisfies both status and readiness.
name: "working worker",
workers: []*worker.Process{newWorkerInState(t, fsm.StateWorking)},
pool: &mockPool{workers: []*worker.Process{newWorkerInState(t, fsm.StateWorking)}},
statusCode: http.StatusOK,
readyCode: http.StatusServiceUnavailable,
readyCode: http.StatusOK,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Plugin{pool: &mockPool{workers: tt.workers}}
p := &Plugin{pool: tt.pool}

st, err := p.Status()
require.NoError(t, err)
Expand Down
Loading