summaryrefslogtreecommitdiff
path: root/util/state.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-29 23:37:16 +0300
committerGitHub <[email protected]>2018-09-29 23:37:16 +0300
commit6122fca108c20984732c969fb1ba53cce5b3c44a (patch)
tree40835f46a5c208ea2546b76e3bd9fa05429b405a /util/state.go
parentabe62c0675f839586312cff1c83d6a4cb31dd9d5 (diff)
parenta04b5b33eb30944007973067ec07e9c4a2c464ab (diff)
Merge pull request #39 from spiral/feature/1.3.0v1.2.3
Feature/1.3.0
Diffstat (limited to 'util/state.go')
-rw-r--r--util/state.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/util/state.go b/util/state.go
new file mode 100644
index 00000000..29fca945
--- /dev/null
+++ b/util/state.go
@@ -0,0 +1,62 @@
+package util
+
+import (
+ "errors"
+ "github.com/shirou/gopsutil/process"
+ "github.com/spiral/roadrunner"
+)
+
+// State provides information about specific worker.
+type State struct {
+ // Pid contains process id.
+ Pid int `json:"pid"`
+
+ // Status of the worker.
+ Status string `json:"status"`
+
+ // Number of worker executions.
+ NumJobs int64 `json:"numExecs"`
+
+ // Created is unix nano timestamp of worker creation time.
+ Created int64 `json:"created"`
+
+ // MemoryUsage holds the information about worker memory usage in bytes.
+ // Values might vary for different operating systems and based on RSS.
+ MemoryUsage uint64 `json:"memoryUsage"`
+}
+
+// WorkerState creates new worker state definition.
+func WorkerState(w *roadrunner.Worker) (*State, error) {
+ p, _ := process.NewProcess(int32(*w.Pid))
+ i, err := p.MemoryInfo()
+ if err != nil {
+ return nil, err
+ }
+
+ return &State{
+ Pid: *w.Pid,
+ Status: w.State().String(),
+ NumJobs: w.State().NumExecs(),
+ Created: w.Created.UnixNano(),
+ MemoryUsage: i.RSS,
+ }, nil
+}
+
+// ServerState returns list of all worker states of a given rr server.
+func ServerState(rr *roadrunner.Server) ([]*State, error) {
+ if rr == nil {
+ return nil, errors.New("rr server is not running")
+ }
+
+ result := make([]*State, 0)
+ for _, w := range rr.Workers() {
+ state, err := WorkerState(w)
+ if err != nil {
+ return nil, err
+ }
+
+ result = append(result, state)
+ }
+
+ return result, nil
+}