-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (C) 2017 ScyllaDB | ||
|
||
package scheduler | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/pkg/errors" | ||
"github.com/scylladb/scylla-manager/pkg/scheduler" | ||
"github.com/scylladb/scylla-manager/pkg/scheduler/trigger" | ||
) | ||
|
||
// Cron implements a trigger based on cron expression. | ||
// It supports the extended syntax including @monthly, @weekly, @daily, @midnight, @hourly, @every <time.Duration>. | ||
type Cron struct { | ||
spec []byte | ||
inner scheduler.Trigger | ||
} | ||
|
||
func NewCron(spec string) (Cron, error) { | ||
t, err := trigger.NewCron(spec) | ||
if err != nil { | ||
return Cron{}, err | ||
} | ||
|
||
return Cron{ | ||
spec: []byte(spec), | ||
inner: t, | ||
}, nil | ||
} | ||
|
||
func NewCronEvery(d time.Duration) Cron { | ||
c, _ := NewCron("@every " + d.String()) // nolint: errcheck | ||
return c | ||
} | ||
|
||
// MustCron calls NewCron and panics on error. | ||
func MustCron(spec string) Cron { | ||
c, err := NewCron(spec) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return c | ||
} | ||
|
||
// Next implements scheduler.Trigger. | ||
func (c Cron) Next(now time.Time) time.Time { | ||
return c.inner.Next(now) | ||
} | ||
|
||
func (c Cron) MarshalText() (text []byte, err error) { | ||
return c.spec, nil | ||
} | ||
|
||
func (c *Cron) UnmarshalText(text []byte) error { | ||
v, err := NewCron(string(text)) | ||
if err != nil { | ||
return errors.Wrap(err, "cron") | ||
} | ||
|
||
*c = v | ||
return nil | ||
} | ||
|
||
func (c Cron) MarshalCQL(info gocql.TypeInfo) ([]byte, error) { | ||
return c.MarshalText() | ||
} | ||
|
||
func (c *Cron) UnmarshalCQL(info gocql.TypeInfo, data []byte) error { | ||
return c.UnmarshalText(data) | ||
} | ||
|
||
func (c Cron) IsZero() bool { | ||
return c.inner == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2021 ScyllaDB | ||
|
||
package scheduler | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCronMarshalUnmarshal(t *testing.T) { | ||
spec := "@every 15s" | ||
|
||
var cron Cron | ||
if err := cron.UnmarshalText([]byte(spec)); err != nil { | ||
t.Fatal(err) | ||
} | ||
b, _ := cron.MarshalText() | ||
if string(b) != spec { | ||
t.Fatalf("MarshalText() = %s, expected %s", string(b), spec) | ||
} | ||
} | ||
|
||
func TestNewCronEvery(t *testing.T) { | ||
c := NewCronEvery(15 * time.Second) | ||
if c.IsZero() { | ||
t.Fatal() | ||
} | ||
} |