Skip to content

Commit

Permalink
added watch support for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Nov 27, 2016
1 parent 8eb9c85 commit b1e0218
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
36 changes: 26 additions & 10 deletions backends/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (

"github.com/HeavyHorst/easyKV"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template"
"github.com/Sirupsen/logrus"
"github.com/natefinch/pie"
)

Expand Down Expand Up @@ -48,13 +46,6 @@ func (p *Plugin) Connect() (template.Backend, error) {
return p.Backend, err
}

if p.Backend.Watch {
log.WithFields(logrus.Fields{
"backend": p.Backend.Name,
}).Warn("Watch is not supported, using interval instead")
p.Backend.Watch = false
}

plugin := &plug{client}
if err := plugin.Init(p.Config); err != nil {
return p.Backend, err
Expand Down Expand Up @@ -83,7 +74,32 @@ func (p *plug) Close() {
p.client.Close()
}

type WatchConfig struct {
Prefix string
Opts easyKV.WatchOptions
}

// WatchPrefix is not supported for now
func (p *plug) WatchPrefix(prefix string, ctx context.Context, opts ...easyKV.WatchOption) (uint64, error) {
return 0, easyKV.ErrWatchNotSupported
var result uint64

wc := WatchConfig{Prefix: prefix}
for _, option := range opts {
option(&wc.Opts)
}

errchan := make(chan error)
go func() {
select {
case errchan <- p.client.Call("Plugin.WatchPrefix", wc, &result):
case <-ctx.Done():
}
}()

select {
case <-ctx.Done():
return wc.Opts.WaitIndex, nil
case err := <-errchan:
return result, err
}
}
8 changes: 8 additions & 0 deletions docs/content/plugins/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ just compile it and move the executable to /etc/remco/plugins.
package main

import (
"context"
"log"
"net/rpc/jsonrpc"

"github.com/HeavyHorst/easyKV"
"github.com/HeavyHorst/easyKV/env"
"github.com/HeavyHorst/remco/backends/plugin"
"github.com/natefinch/pie"
)

Expand Down Expand Up @@ -55,6 +57,12 @@ func (e *EnvRPCServer) Close(args interface{}, resp *interface{}) error {
e.Impl.Close()
return nil
}

func (e EnvRPCServer) WatchPrefix(args plugin.WatchConfig, resp *uint64) error {
var err error
*resp, err = e.Impl.WatchPrefix(args.Prefix, context.Background(), easyKV.WithKeys(args.Opts.Keys), easyKV.WithWaitIndex(args.Opts.WaitIndex))
return err
}
```

Then create a config file with this backend section.
Expand Down

0 comments on commit b1e0218

Please sign in to comment.