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

feature: implement status #471

Merged
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
119 changes: 119 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package rrtemporal

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.wfP.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.wfP.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
}
21 changes: 21 additions & 0 deletions tests/configs/.rr-status.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'

rpc:
listen: tcp://127.0.0.1:6001

server:
command: "php php_test_files/worker.php"

temporal:
address: "127.0.0.1:7233"
cache_size: 100000
activities:
num_workers: 4
destroy_timeout: 1s

logs:
mode: development
level: debug

status:
address: "127.0.0.1:35544"
3 changes: 2 additions & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
require (
github.com/fatih/color v1.16.0
github.com/pborman/uuid v1.2.1
github.com/roadrunner-server/api/v4 v4.11.0
github.com/roadrunner-server/config/v4 v4.6.8
github.com/roadrunner-server/endure/v2 v2.4.3
github.com/roadrunner-server/goridge/v3 v3.8.1
Expand All @@ -15,6 +16,7 @@ require (
github.com/roadrunner-server/rpc/v4 v4.2.16
github.com/roadrunner-server/sdk/v4 v4.6.0
github.com/roadrunner-server/server/v4 v4.5.8
github.com/roadrunner-server/status/v4 v4.4.14
github.com/stretchr/testify v1.8.4
github.com/temporalio/roadrunner-temporal/v4 v4.6.0
go.temporal.io/api v1.27.0
Expand Down Expand Up @@ -59,7 +61,6 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.47.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/roadrunner-server/api/v4 v4.10.0 // indirect
github.com/roadrunner-server/errors v1.4.0 // indirect
github.com/roadrunner-server/tcplisten v1.4.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/roadrunner-server/api/v4 v4.10.0 h1:tF6vmA6MaQyOL/GQQc+nyj356oX3UoQBd+SXNtsu+bU=
github.com/roadrunner-server/api/v4 v4.10.0/go.mod h1:ou9QviOd5dxl3to1+BV4iZ3lnMLxuE/HqESNW5PDnw0=
github.com/roadrunner-server/api/v4 v4.11.0 h1:N/MX+9r0ExSQ3rJG7sYUc2WQkWYYnJTJDUhxkx3UdX0=
github.com/roadrunner-server/api/v4 v4.11.0/go.mod h1:+OkLfVYQM24v7uU+txIyCyE5U9qtxVwX3xMafzKpfzY=
github.com/roadrunner-server/config/v4 v4.6.8 h1:dxG+VdPL5+7JG1BMjvs21sqZdqEAr7EFuLxk3XEhR8Q=
github.com/roadrunner-server/config/v4 v4.6.8/go.mod h1:Bh7SBen7Bb38ex8j1F3xAcvARsLsxbLbLCVw/vZBd8c=
github.com/roadrunner-server/endure/v2 v2.4.3 h1:R9DdsLiLjtSFivZ1HKk/1eDZ0TYaKHQzakVwz9D2hto=
Expand All @@ -218,6 +218,8 @@ github.com/roadrunner-server/sdk/v4 v4.6.0 h1:dXMN7V8+VKjQAZamhKBizPGSqrpfAfVG6r
github.com/roadrunner-server/sdk/v4 v4.6.0/go.mod h1:YzRn2S947MqcnBcOuwu04CpAhsQGf6JBf+xY+njqu5o=
github.com/roadrunner-server/server/v4 v4.5.8 h1:pc/Bzaz0APk0rESo7OMybtFcNx3EOzOL95f+Yq7RaRc=
github.com/roadrunner-server/server/v4 v4.5.8/go.mod h1:vueybZ3U+noYb+o2t9QIzOUzRpZN+IcfY45rcysfEdE=
github.com/roadrunner-server/status/v4 v4.4.14 h1:0e0QEnQVXmLhUegwRQcgqp8BwpaguoSpKsL7M/PoVbA=
github.com/roadrunner-server/status/v4 v4.4.14/go.mod h1:ehXPGD8brMdD81nHTetPuIXewHR4RByfw0/FPsSKlpU=
github.com/roadrunner-server/tcplisten v1.4.0 h1:yWo09zktv/CSV6VywLfw4pwNcUchgTiIrW4uIICtO5M=
github.com/roadrunner-server/tcplisten v1.4.0/go.mod h1:A6+VSnW2ETGnN/e/CMdP63ZXqQDaC0UDMU6QmyuB0yM=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
Expand Down
116 changes: 116 additions & 0 deletions tests/plugin_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package tests

import (
"github.com/roadrunner-server/config/v4"
"github.com/roadrunner-server/endure/v2"
"github.com/roadrunner-server/logger/v4"
"github.com/roadrunner-server/server/v4"
"github.com/roadrunner-server/status/v4"
"github.com/stretchr/testify/require"
rrtemporal "github.com/temporalio/roadrunner-temporal/v4"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestTemporalCheckStatus(t *testing.T) {
cont := endure.New(slog.LevelDebug)

cfg := &config.Plugin{
Version: "2023.3.0",
Path: "configs/.rr-status.yaml",
Prefix: "rr",
}

err := cont.RegisterAll(
cfg,
&status.Plugin{},
&logger.Plugin{},
&rrtemporal.Plugin{},
&server.Plugin{},
)
assert.NoError(t, err)

err = cont.Init()
if err != nil {
t.Fatal(err)
}

ch, err := cont.Serve()
assert.NoError(t, err)

sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

wg := &sync.WaitGroup{}
wg.Add(1)

stopCh := make(chan struct{}, 1)

go func() {
defer wg.Done()
for {
select {
case e := <-ch:
assert.Fail(t, "error", e.Error.Error())
err = cont.Stop()
if err != nil {
assert.FailNow(t, "error", err.Error())
}
case <-sig:
err = cont.Stop()
if err != nil {
assert.FailNow(t, "error", err.Error())
}
return
case <-stopCh:
err = cont.Stop()
if err != nil {
assert.FailNow(t, "error", err.Error())
}
return
}
}
}()

time.Sleep(time.Second)

client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("GET", "http://127.0.0.1:35544/health?plugin=temporal", nil)
require.NoError(t, err)

resp, err := client.Do(req)
require.NoError(t, err)
require.NotNil(t, resp)

body, _ := io.ReadAll(resp.Body)
assert.Equal(t, "plugin: temporal, status: 200\n", string(body))
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()

req, err = http.NewRequest("GET", "http://127.0.0.1:35544/ready?plugin=temporal", nil)
require.NoError(t, err)

resp, err = client.Do(req)
require.NoError(t, err)
require.NotNil(t, resp)

body, _ = io.ReadAll(resp.Body)
assert.Equal(t, "plugin: temporal, status: 200\n", string(body))
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()

stopCh <- struct{}{}

wg.Wait()
}
Loading