summaryrefslogtreecommitdiff
path: root/plugins/resetter/plugin.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-12-26 00:47:21 +0300
committerGitHub <[email protected]>2020-12-26 00:47:21 +0300
commit566d7f4c95eb5dedcb2da5afcda4bbea8eba077f (patch)
tree0007a6b8c8ac9e7d31b8a5f3f7f27669c860d261 /plugins/resetter/plugin.go
parent1bc3db2ea9b95edd0101676d7bfd75df3782c3bd (diff)
parent7a0dee1a416705c621edbf50e1f43fb39845348f (diff)
Merge pull request #463 from spiral/experiment/core_pluginsv2.0.0-beta1
[RR2] Plugins
Diffstat (limited to 'plugins/resetter/plugin.go')
-rw-r--r--plugins/resetter/plugin.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/plugins/resetter/plugin.go b/plugins/resetter/plugin.go
new file mode 100644
index 00000000..5d294086
--- /dev/null
+++ b/plugins/resetter/plugin.go
@@ -0,0 +1,80 @@
+package resetter
+
+import (
+ "github.com/spiral/endure"
+ "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("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("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
+}
+
+// RPCService returns associated rpc service.
+func (p *Plugin) RPC() interface{} {
+ return &rpc{srv: p, log: p.log}
+}