diff options
Diffstat (limited to 'commands.go')
-rw-r--r-- | commands.go | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/commands.go b/commands.go index 5368097f..ae9d0a12 100644 --- a/commands.go +++ b/commands.go @@ -2,26 +2,43 @@ package roadrunner import ( "encoding/json" + "fmt" "github.com/spiral/goridge" + "os" ) -// TerminateCommand must stop underlying process. -type TerminateCommand struct { - Terminate bool `json:"terminate"` +type stopCommand struct { + Stop bool `json:"stop"` } -// PidCommand send greeting message between processes in json format. -type PidCommand struct { - Pid int `json:"pid"` - Parent int `json:"parent,omitempty"` +type pidCommand struct { + Pid int `json:"pid"` } -// sends control message via relay using JSON encoding -func sendCommand(rl goridge.Relay, command interface{}) error { - bin, err := json.Marshal(command) +func sendCommand(rl goridge.Relay, v interface{}) error { + bin, err := json.Marshal(v) if err != nil { return err } return rl.Send(bin, goridge.PayloadControl) } + +func fetchPid(rl goridge.Relay) (pid int, err error) { + if err := sendCommand(rl, pidCommand{Pid: os.Getpid()}); err != nil { + return 0, err + } + + body, p, err := rl.Receive() + if !p.HasFlag(goridge.PayloadControl) { + return 0, fmt.Errorf("unexpected response, `control` header is missing") + } + + link := &pidCommand{} + //log.Println(string(body)) + if err := json.Unmarshal(body, link); err != nil { + return 0, err + } + + return link.Pid, nil +} |