summaryrefslogtreecommitdiff
path: root/tools/process.go
blob: d92755d194905575fff0588f2e66f5b3fcfc3680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package tools

import (
	"github.com/shirou/gopsutil/process"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/interfaces/worker"
)

// ProcessState provides information about specific worker.
type ProcessState 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"`
}

// WorkerProcessState creates new worker state definition.
func WorkerProcessState(w worker.BaseProcess) (ProcessState, error) {
	const op = errors.Op("worker_process state")
	p, _ := process.NewProcess(int32(w.Pid()))
	i, err := p.MemoryInfo()
	if err != nil {
		return ProcessState{}, errors.E(op, err)
	}

	return ProcessState{
		Pid:         int(w.Pid()),
		Status:      w.State().String(),
		NumJobs:     w.State().NumExecs(),
		Created:     w.Created().UnixNano(),
		MemoryUsage: i.RSS,
	}, nil
}