summaryrefslogtreecommitdiff
path: root/pipe_factory.go
blob: ce32dacc7aada4fb3e137c6ce000c3b250c2505f (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
45
46
package roadrunner

import (
	"github.com/spiral/goridge"
	"os/exec"
)

// PipeFactory connects to workers using standard streams (STDIN, STDOUT pipes).
type PipeFactory struct {
}

// NewPipeFactory returns new factory instance and starts listening
func NewPipeFactory() *PipeFactory {
	return &PipeFactory{}
}

// NewWorker creates worker and connects it to appropriate relay or returns error
func (f *PipeFactory) NewWorker(cmd *exec.Cmd) (w *Worker, err error) {
	w, err = NewWorker(cmd)
	if err != nil {
		return nil, err
	}

	in, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}

	out, err := cmd.StdinPipe()
	if err != nil {
		return nil, err
	}

	if err := w.Start(); err != nil {
		return nil, err
	}

	w.attach(goridge.NewPipeRelay(in, out))

	return w, nil
}

// Close closes all open factory descriptors.
func (f *PipeFactory) Close() error {
	return nil
}