cmd/jobrun: repeat strategies as part of jobrun
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/zrepl/zrepl/jobrun"
|
||||
"github.com/zrepl/zrepl/rpc"
|
||||
"github.com/zrepl/zrepl/sshbytestream"
|
||||
. "github.com/zrepl/zrepl/util"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Pool struct {
|
||||
@@ -42,12 +44,15 @@ type Push struct {
|
||||
To *Pool
|
||||
Filter zfs.DatasetMapping
|
||||
InitialReplPolicy rpc.InitialReplPolicy
|
||||
RepeatStrategy jobrun.RepeatStrategy
|
||||
}
|
||||
type Pull struct {
|
||||
From *Pool
|
||||
Mapping zfs.DatasetMapping
|
||||
InitialReplPolicy rpc.InitialReplPolicy
|
||||
RepeatStrategy jobrun.RepeatStrategy
|
||||
}
|
||||
|
||||
type ClientMapping struct {
|
||||
From string
|
||||
Mapping zfs.DatasetMapping
|
||||
@@ -171,6 +176,7 @@ func parsePushs(v interface{}, pl poolLookup) (p []Push, err error) {
|
||||
To string
|
||||
Filter map[string]string
|
||||
InitialReplPolicy string
|
||||
Repeat map[string]string
|
||||
}, 0)
|
||||
|
||||
if err = mapstructure.Decode(v, &asList); err != nil {
|
||||
@@ -197,6 +203,10 @@ func parsePushs(v interface{}, pl poolLookup) (p []Push, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if push.RepeatStrategy, err = parseRepeatStrategy(e.Repeat); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
p[i] = push
|
||||
}
|
||||
|
||||
@@ -209,6 +219,7 @@ func parsePulls(v interface{}, pl poolLookup) (p []Pull, err error) {
|
||||
From string
|
||||
Mapping map[string]string
|
||||
InitialReplPolicy string
|
||||
Repeat map[string]string
|
||||
}, 0)
|
||||
|
||||
if err = mapstructure.Decode(v, &asList); err != nil {
|
||||
@@ -241,6 +252,9 @@ func parsePulls(v interface{}, pl poolLookup) (p []Pull, err error) {
|
||||
if pull.InitialReplPolicy, err = parseInitialReplPolicy(e.InitialReplPolicy, rpc.DEFAULT_INITIAL_REPL_POLICY); err != nil {
|
||||
return
|
||||
}
|
||||
if pull.RepeatStrategy, err = parseRepeatStrategy(e.Repeat); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
p[i] = pull
|
||||
}
|
||||
@@ -272,6 +286,25 @@ err:
|
||||
return
|
||||
}
|
||||
|
||||
func parseRepeatStrategy(r map[string]string) (s jobrun.RepeatStrategy, err error) {
|
||||
|
||||
if r == nil {
|
||||
return jobrun.NoRepeatStrategy{}, nil
|
||||
}
|
||||
|
||||
if repeatStr, ok := r["interval"]; ok {
|
||||
d, err := time.ParseDuration(repeatStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s = &jobrun.PeriodicRepeatStrategy{d}
|
||||
return s, err
|
||||
} else {
|
||||
return nil, fmt.Errorf("attribute 'interval' not found but required in repeat specification")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func expectList(v interface{}) (asList []interface{}, err error) {
|
||||
var ok bool
|
||||
if asList, ok = v.([]interface{}); !ok {
|
||||
|
||||
+4
-6
@@ -158,9 +158,8 @@ func cmdRun(c *cli.Context) error {
|
||||
i := 0
|
||||
for _, pull := range conf.Pulls {
|
||||
jobs[i] = jobrun.Job{
|
||||
Name: fmt.Sprintf("pull%d", i),
|
||||
Interval: time.Duration(5 * time.Second),
|
||||
Repeats: true,
|
||||
Name: fmt.Sprintf("pull.%d", i),
|
||||
RepeatStrategy: pull.RepeatStrategy,
|
||||
RunFunc: func(log jobrun.Logger) error {
|
||||
log.Printf("doing pull: %v", pull)
|
||||
return jobPull(pull, c, log)
|
||||
@@ -170,9 +169,8 @@ func cmdRun(c *cli.Context) error {
|
||||
}
|
||||
for _, push := range conf.Pushs {
|
||||
jobs[i] = jobrun.Job{
|
||||
Name: fmt.Sprintf("push%d", i),
|
||||
Interval: time.Duration(5 * time.Second),
|
||||
Repeats: true,
|
||||
Name: fmt.Sprintf("push.%d", i),
|
||||
RepeatStrategy: push.RepeatStrategy,
|
||||
RunFunc: func(log jobrun.Logger) error {
|
||||
log.Printf("doing push: %v", push)
|
||||
return jobPush(push, c, log)
|
||||
|
||||
@@ -23,6 +23,8 @@ pulls:
|
||||
# local replication, only allowed in pull mode
|
||||
# the from name 'local' is reserved for this purpose
|
||||
- from: local
|
||||
repeat:
|
||||
interval: 15m
|
||||
mapping: {
|
||||
"tank/usr/home":"mirrorpool/foo/bar"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user