config: support days (d) and weeks (w) in durations

fixes #18
This commit is contained in:
Christian Schwarz 2017-10-05 15:12:50 +02:00
parent 3e647c14c0
commit 83d450b1f2
6 changed files with 52 additions and 40 deletions

View File

@ -49,7 +49,7 @@ func parseLocalJob(c JobParsingContext, name string, i map[string]interface{}) (
return
}
if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil {
if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil {
err = errors.Wrap(err, "cannot parse interval")
return
}

View File

@ -48,7 +48,7 @@ func parsePullJob(c JobParsingContext, name string, i map[string]interface{}) (j
return nil, err
}
if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil {
if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil {
err = errors.Wrap(err, "cannot parse 'interval'")
return nil, err
}

View File

@ -50,7 +50,7 @@ func parseSourceJob(c JobParsingContext, name string, i map[string]interface{})
return
}
if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil {
if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil {
err = errors.Wrap(err, "cannot parse 'interval'")
return
}

View File

@ -8,6 +8,9 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"os"
"regexp"
"strconv"
"time"
)
var ConfigFileDefaultLocations []string = []string{
@ -252,3 +255,41 @@ func parseAuthenticatedChannelListenerFactory(c JobParsingContext, v map[string]
}
}
var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`)
func parsePostitiveDuration(e string) (d time.Duration, err error) {
comps := durationStringRegex.FindStringSubmatch(e)
if len(comps) != 3 {
err = fmt.Errorf("does not match regex: %s %#v", e, comps)
return
}
durationFactor, err := strconv.ParseInt(comps[1], 10, 64)
if err != nil {
return 0, err
}
if durationFactor <= 0 {
return 0, errors.New("duration must be positive integer")
}
var durationUnit time.Duration
switch comps[2] {
case "s":
durationUnit = time.Second
case "m":
durationUnit = time.Minute
case "h":
durationUnit = time.Hour
case "d":
durationUnit = 24 * time.Hour
case "w":
durationUnit = 24 * 7 * time.Hour
default:
err = fmt.Errorf("contains unknown time unit '%s'", comps[2])
return
}
d = time.Duration(durationFactor) * durationUnit
return
}

View File

@ -93,42 +93,6 @@ func parseGridPrunePolicy(e map[string]interface{}) (p *GridPrunePolicy, err err
return
}
var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`)
func parseDuration(e string) (d time.Duration, err error) {
comps := durationStringRegex.FindStringSubmatch(e)
if len(comps) != 3 {
err = fmt.Errorf("does not match regex: %s %#v", e, comps)
return
}
durationFactor, err := strconv.ParseInt(comps[1], 10, 64)
if err != nil {
return
}
var durationUnit time.Duration
switch comps[2] {
case "s":
durationUnit = time.Second
case "m":
durationUnit = time.Minute
case "h":
durationUnit = time.Hour
case "d":
durationUnit = 24 * time.Hour
case "w":
durationUnit = 24 * 7 * time.Hour
default:
err = fmt.Errorf("contains unknown time unit '%s'", comps[2])
return
}
d = time.Duration(durationFactor) * durationUnit
return
}
var retentionStringIntervalRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*x\s*([^\(]+)\s*(\((.*)\))?\s*$`)
func parseRetentionGridIntervalString(e string) (intervals []util.RetentionInterval, err error) {
@ -146,7 +110,7 @@ func parseRetentionGridIntervalString(e string) (intervals []util.RetentionInter
return nil, fmt.Errorf("contains factor <= 0")
}
duration, err := parseDuration(comps[2])
duration, err := parsePostitiveDuration(comps[2])
if err != nil {
return nil, err
}

View File

@ -26,6 +26,13 @@ global:
sockdir: /var/run/zrepl/stdinserver
```
## Durations & Intervals
Interval & duration fields in job definitions, pruning configurations, etc. must match the following regex:
```go
var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`)
// s = second, m = minute, h = hour, d = day, w = week (7 days)
```
## Super-Verbose Job Debugging