PHP Classes

File: pipe_factory.go

Recommend this page to a friend!
  Classes of Wolfy-J   roadrunner   pipe_factory.go   Download  
File: pipe_factory.go
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: roadrunner
Run multiple tasks of PHP in parallel using Golang
Author: By
Last change:
Date: 6 years ago
Size: 1,192 bytes
 

Contents

Class file image Download
package roadrunner import ( "github.com/pkg/errors" "github.com/spiral/goridge" "io" "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{} } // SpawnWorker creates new worker and connects it to goridge relay, // method Wait() must be handled on level above. func (f *PipeFactory) SpawnWorker(cmd *exec.Cmd) (w *Worker, err error) { if w, err = newWorker(cmd); err != nil { return nil, err } var ( in io.ReadCloser out io.WriteCloser ) if in, err = cmd.StdoutPipe(); err != nil { return nil, err } if out, err = cmd.StdinPipe(); err != nil { return nil, err } w.rl = goridge.NewPipeRelay(in, out) if err := w.start(); err != nil { return nil, errors.Wrap(err, "process error") } if pid, err := fetchPID(w.rl); pid != *w.Pid { go func(w *Worker) { w.Kill() }(w) if wErr := w.Wait(); wErr != nil { err = errors.Wrap(wErr, err.Error()) } return nil, errors.Wrap(err, "unable to connect to worker") } w.state.set(StateReady) return w, nil }