From ef0b15ab9eba2bcc5c5b6f147ef7b1351a6679b5 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Sun, 25 Oct 2020 11:27:10 +0300 Subject: - new-worker produces running worker --- plugins/factory/factory.go | 47 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'plugins/factory/factory.go') diff --git a/plugins/factory/factory.go b/plugins/factory/factory.go index 5d80682d..f7303b6d 100644 --- a/plugins/factory/factory.go +++ b/plugins/factory/factory.go @@ -16,22 +16,34 @@ type WorkerFactory interface { } type WFactory struct { - spw Spawner - eb *events.EventBroadcaster + events *events.EventBroadcaster + app Spawner + wFactory roadrunner.Factory } -func (wf *WFactory) NewWorkerPool(ctx context.Context, opt *roadrunner.Config, env Env) (roadrunner.Pool, error) { - cmd, err := wf.spw.NewCmd(env) +func (wf *WFactory) Init(app Spawner) (err error) { + wf.events = events.NewEventBroadcaster() + + wf.app = app + wf.wFactory, err = app.NewFactory() if err != nil { - return nil, err + return nil } - factory, err := wf.spw.NewFactory(env) + return nil +} + +func (wf *WFactory) AddListener(l events.EventListener) { + wf.events.AddListener(l) +} + +func (wf *WFactory) NewWorkerPool(ctx context.Context, opt *roadrunner.Config, env Env) (roadrunner.Pool, error) { + cmd, err := wf.app.NewCmd(env) if err != nil { return nil, err } - p, err := roadrunner.NewPool(ctx, cmd, factory, opt) + p, err := roadrunner.NewPool(ctx, cmd, wf.wFactory, opt) if err != nil { return nil, err } @@ -39,7 +51,7 @@ func (wf *WFactory) NewWorkerPool(ctx context.Context, opt *roadrunner.Config, e // TODO event to stop go func() { for e := range p.Events() { - wf.eb.Push(e) + wf.events.Push(e) if we, ok := e.Payload.(roadrunner.WorkerEvent); ok { if we.Event == roadrunner.EventWorkerLog { log.Print(color.YellowString(string(we.Payload.([]byte)))) @@ -52,25 +64,10 @@ func (wf *WFactory) NewWorkerPool(ctx context.Context, opt *roadrunner.Config, e } func (wf *WFactory) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) { - cmd, err := wf.spw.NewCmd(env) + cmd, err := wf.app.NewCmd(env) if err != nil { return nil, err } - wb, err := roadrunner.InitBaseWorker(cmd()) - if err != nil { - return nil, err - } - - return wb, nil -} - -func (wf *WFactory) Init(app Spawner) error { - wf.spw = app - wf.eb = events.NewEventBroadcaster() - return nil -} - -func (wf *WFactory) AddListener(l events.EventListener) { - wf.eb.AddListener(l) + return wf.wFactory.SpawnWorkerWithContext(ctx, cmd()) } -- cgit v1.2.3 From ba5c562f9038ba434e655fb82c44597fcccaff16 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Sun, 25 Oct 2020 15:55:51 +0300 Subject: - massive update in roadrunner 2.0 abstractions --- plugins/factory/factory.go | 73 ---------------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 plugins/factory/factory.go (limited to 'plugins/factory/factory.go') diff --git a/plugins/factory/factory.go b/plugins/factory/factory.go deleted file mode 100644 index f7303b6d..00000000 --- a/plugins/factory/factory.go +++ /dev/null @@ -1,73 +0,0 @@ -package factory - -import ( - "context" - - "log" - - "github.com/fatih/color" - "github.com/spiral/roadrunner/v2" - "github.com/spiral/roadrunner/v2/plugins/events" -) - -type WorkerFactory interface { - NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) - NewWorkerPool(ctx context.Context, opt *roadrunner.Config, env Env) (roadrunner.Pool, error) -} - -type WFactory struct { - events *events.EventBroadcaster - app Spawner - wFactory roadrunner.Factory -} - -func (wf *WFactory) Init(app Spawner) (err error) { - wf.events = events.NewEventBroadcaster() - - wf.app = app - wf.wFactory, err = app.NewFactory() - if err != nil { - return nil - } - - return nil -} - -func (wf *WFactory) AddListener(l events.EventListener) { - wf.events.AddListener(l) -} - -func (wf *WFactory) NewWorkerPool(ctx context.Context, opt *roadrunner.Config, env Env) (roadrunner.Pool, error) { - cmd, err := wf.app.NewCmd(env) - if err != nil { - return nil, err - } - - p, err := roadrunner.NewPool(ctx, cmd, wf.wFactory, opt) - if err != nil { - return nil, err - } - - // TODO event to stop - go func() { - for e := range p.Events() { - wf.events.Push(e) - if we, ok := e.Payload.(roadrunner.WorkerEvent); ok { - if we.Event == roadrunner.EventWorkerLog { - log.Print(color.YellowString(string(we.Payload.([]byte)))) - } - } - } - }() - - return p, nil -} - -func (wf *WFactory) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) { - cmd, err := wf.app.NewCmd(env) - if err != nil { - return nil, err - } - - return wf.wFactory.SpawnWorkerWithContext(ctx, cmd()) -} -- cgit v1.2.3