From b3f74f75b6a1d7aba1d2aa88b2412499647bb580 Mon Sep 17 00:00:00 2001 From: Vladimir Plakhotnikov Date: Mon, 12 Jun 2023 13:38:18 +0500 Subject: [PATCH] Jobs Status Signed-off-by: Vladimir Plakhotnikov --- status.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 status.go diff --git a/status.go b/status.go new file mode 100644 index 0000000..c32178b --- /dev/null +++ b/status.go @@ -0,0 +1,48 @@ +package jobs + +import ( + "net/http" + + "github.com/roadrunner-server/api/v4/plugins/v1/status" + "github.com/roadrunner-server/sdk/v4/fsm" +) + +// Status return status of the particular plugin +func (p *Plugin) Status() (*status.Status, error) { + p.mu.RLock() + defer p.mu.RUnlock() + + workers := p.workersPool.Workers() + for i := 0; i < len(workers); i++ { + if workers[i].State().IsActive() { + return &status.Status{ + Code: http.StatusOK, + }, nil + } + } + // if there are no workers, threat this as error + return &status.Status{ + Code: http.StatusServiceUnavailable, + }, nil +} + +// Ready return readiness status of the particular plugin +func (p *Plugin) Ready() (*status.Status, error) { + p.mu.RLock() + defer p.mu.RUnlock() + + workers := p.workersPool.Workers() + for i := 0; i < len(workers); i++ { + // If state of the worker is ready (at least 1) + // we assume, that plugin's worker pool is ready + if workers[i].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 +}