summaryrefslogtreecommitdiff
path: root/plugins/informer/plugin.go
blob: 416c0112682dc60ff8aecb1e0b48f6bd259ff0a5 (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
package informer

import (
	endure "github.com/spiral/endure/pkg/container"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/worker"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

const PluginName = "informer"

type Plugin struct {
	registry map[string]Informer
	log      logger.Logger
}

func (p *Plugin) Init(log logger.Logger) error {
	p.registry = make(map[string]Informer)
	p.log = log
	return nil
}

// Workers provides BaseProcess slice with workers for the requested plugin
func (p *Plugin) Workers(name string) ([]worker.BaseProcess, error) {
	const op = errors.Op("informer_plugin_workers")
	svc, ok := p.registry[name]
	if !ok {
		return nil, errors.E(op, errors.Errorf("no such service: %s", name))
	}

	return svc.Workers(), nil
}

// CollectTarget resettable service.
func (p *Plugin) CollectTarget(name endure.Named, r Informer) error {
	p.registry[name.Name()] = r
	return nil
}

// Collects declares services to be collected.
func (p *Plugin) Collects() []interface{} {
	return []interface{}{
		p.CollectTarget,
	}
}

// Name of the service.
func (p *Plugin) Name() string {
	return PluginName
}

// RPCService returns associated rpc service.
func (p *Plugin) RPC() interface{} {
	return &rpc{srv: p, log: p.log}
}