summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-26 22:18:48 +0300
committerWolfy-J <[email protected]>2018-09-26 22:18:48 +0300
commitd24c43aaeff897394eca140478c73dd146f8710a (patch)
treeae7e9515aa11a7358349f7c822bf020a7c5f33ed /util
parentabe62c0675f839586312cff1c83d6a4cb31dd9d5 (diff)
worker information extraction is now moved to external package
Diffstat (limited to 'util')
-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..3984d72d
--- /dev/null
+++ b/util/state.go
@@ -0,0 +1,62 @@
+package util
+
+import (
+ "github.com/shirou/gopsutil/process"
+ "github.com/spiral/roadrunner"
+ "errors"
+)
+
+// 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
+}