summaryrefslogtreecommitdiff
path: root/plugins/resetter/plugin.go
blob: 191185aefaeffa2fc878e017f69c571a5e21c97e (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 resetter

import (
	endure "github.com/spiral/endure/pkg/container"
	"github.com/spiral/errors"
)

const PluginName = "resetter"

type Plugin struct {
	registry map[string]Resetter
}

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

// Reset named service.
func (p *Plugin) Reset(name string) error {
	const op = errors.Op("resetter_plugin_reset_by_name")
	svc, ok := p.registry[name]
	if !ok {
		return errors.E(op, errors.Errorf("no such plugin: %s", name))
	}

	return svc.Reset()
}

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

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

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

// Available interface implementation
func (p *Plugin) Available() {
}

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