summaryrefslogtreecommitdiff
path: root/plugins/resetter/plugin.go
blob: 4feb692a2689a7159f2404b17d4ebfc207f6f3f4 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package resetter

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

const PluginName = "resetter"

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

func (p *Plugin) ResetAll() error {
	const op = errors.Op("resetter_plugin_reset_all")
	for name := range p.registry {
		err := p.registry[name].Reset()
		if err != nil {
			return errors.E(op, err)
		}
	}
	return nil
}

func (p *Plugin) ResetByName(plugin string) error {
	const op = errors.Op("resetter_plugin_reset_by_name")
	if plugin, ok := p.registry[plugin]; ok {
		return plugin.Reset()
	}
	return errors.E(op, errors.Errorf("can't find plugin: %s", plugin))
}

func (p *Plugin) GetAll() []string {
	all := make([]string, 0, len(p.registry))
	for name := range p.registry {
		all = append(all, name)
	}
	return all
}

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

// Reset named service.
func (p *Plugin) Reset(name string) error {
	svc, ok := p.registry[name]
	if !ok {
		return errors.E("no such service", errors.Str(name))
	}

	return svc.Reset()
}

// RegisterTarget resettable service.
func (p *Plugin) RegisterTarget(name endure.Named, r Resettable) 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, log: p.log}
}