diff options
Diffstat (limited to 'service/factory.go')
-rw-r--r-- | service/factory.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/service/factory.go b/service/factory.go new file mode 100644 index 00000000..dbdebc4f --- /dev/null +++ b/service/factory.go @@ -0,0 +1,69 @@ +package service + +import ( + "github.com/spiral/roadrunner" + "time" + "os/exec" + "strings" + "net" +) + +type PoolConfig struct { + Command string + Relay string + + Number uint64 + MaxJobs uint64 + + Timeouts struct { + Allocate int + Destroy int + } +} + +func (f *PoolConfig) NewServer() (*roadrunner.Server, func(), error) { + relays, terminator, err := f.relayFactory() + if err != nil { + terminator() + return nil, nil, err + } + + rr := roadrunner.NewServer(f.cmd(), relays) + if err := rr.Configure(f.rrConfig()); err != nil { + return nil, nil, err + } + + return rr, nil, nil +} + +func (f *PoolConfig) rrConfig() roadrunner.Config { + return roadrunner.Config{ + NumWorkers: f.Number, + MaxExecutions: f.MaxJobs, + AllocateTimeout: time.Second * time.Duration(f.Timeouts.Allocate), + DestroyTimeout: time.Second * time.Duration(f.Timeouts.Destroy), + } +} + +func (f *PoolConfig) cmd() func() *exec.Cmd { + cmd := strings.Split(f.Command, " ") + return func() *exec.Cmd { return exec.Command(cmd[0], cmd[1:]...) } +} + +func (f *PoolConfig) relayFactory() (roadrunner.Factory, func(), error) { + if f.Relay == "pipes" || f.Relay == "pipe" { + return roadrunner.NewPipeFactory(), nil, nil + } + + dsn := strings.Split(f.Relay, "://") + if len(dsn) != 2 { + return nil, nil, dsnError + } + + ln, err := net.Listen(dsn[0], dsn[1]) + if err != nil { + return nil, nil, err + } + + return roadrunner.NewSocketFactory(ln, time.Minute), func() { ln.Close() }, nil +} |