summaryrefslogtreecommitdiff
path: root/plugins/factory/factory.go
blob: 74fd241f57af95f0452832eee2f79ae57800b672 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package factory

import (
	"context"
	"github.com/temporalio/roadrunner-temporal/events"

	"github.com/temporalio/roadrunner-temporal/roadrunner"
)

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 {
	spw Spawner
	eb  *events.EventBroadcaster
}

func (wf *WFactory) NewWorkerPool(ctx context.Context, opt *roadrunner.Config, env Env) (roadrunner.Pool, error) {
	cmd, err := wf.spw.NewCmd(env)
	if err != nil {
		return nil, err
	}
	factory, err := wf.spw.NewFactory(env)
	if err != nil {
		return nil, err
	}

	p, err := roadrunner.NewPool(ctx, cmd, factory, opt)
	if err != nil {
		return nil, err
	}

	// TODO event to stop
	go func() {
		for e := range p.Events() {
			wf.eb.Push(e)
		}
	}()

	return p, nil
}

func (wf *WFactory) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) {
	cmd, err := wf.spw.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)
}