Skip to content

Replace rebfig/cron/v3 with adhocore/gronx #1959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cmd/incusd/snapshot_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/robfig/cron/v3"
"github.com/adhocore/gronx"

localUtil "github.com/lxc/incus/v6/internal/server/util"
"github.com/lxc/incus/v6/shared/util"
Expand Down Expand Up @@ -98,11 +98,6 @@ func getObfuscatedTimeValuesForSubject(subjectID int64) (string, string) {
}

func cronSpecIsNow(spec string) (bool, error) {
sched, err := cron.ParseStandard(spec)
if err != nil {
return false, fmt.Errorf("Could not parse cron '%s'", spec)
}

// Check if it's time to snapshot
now := time.Now()

Expand All @@ -114,7 +109,10 @@ func cronSpecIsNow(spec string) (bool, error) {

// Calculate the next scheduled time based on the snapshots.schedule
// pattern and the time now.
next := sched.Next(now)
next, err := gronx.NextTickAfter(spec, now, false)
if err != nil {
return false, fmt.Errorf("Could not parse cron '%s': %w", spec, err)
}

if !now.Add(time.Minute).Equal(next) {
return false, nil
Expand Down
16 changes: 10 additions & 6 deletions cmd/incusd/snapshot_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import (
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
)

func (suite *containerTestSuite) TestSnapshotScheduling() {
type snapshotCommonTestSuite struct {
daemonTestSuite
}

func (s *snapshotCommonTestSuite) TestSnapshotScheduling() {
args := db.InstanceArgs{
Type: instancetype.Container,
Ephemeral: false,
Name: "hal9000",
}

c, op, _, err := instance.CreateInternal(suite.d.State(), args, nil, true, true)
suite.Req.Nil(err)
suite.Equal(true, snapshotIsScheduledNow("* * * * *",
c, op, _, err := instance.CreateInternal(s.d.State(), args, nil, true, true)
s.Req.Nil(err)
s.Equal(true, snapshotIsScheduledNow("* * * * *",
int64(c.ID())),
"snapshot.schedule config '* * * * *' should have matched now")
suite.Equal(true, snapshotIsScheduledNow("@daily,"+
s.Equal(true, snapshotIsScheduledNow("@daily,"+
"@hourly,"+
"@midnight,"+
"@weekly,"+
Expand All @@ -36,5 +40,5 @@ func (suite *containerTestSuite) TestSnapshotScheduling() {
}

func TestSnapshotCommon(t *testing.T) {
suite.Run(t, new(containerTestSuite))
suite.Run(t, new(snapshotCommonTestSuite))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.7

require (
github.com/LINBIT/golinstor v0.55.0
github.com/adhocore/gronx v1.19.5
github.com/armon/go-proxyproto v0.1.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/checkpoint-restore/go-criu/v6 v6.3.0
Expand Down Expand Up @@ -44,7 +45,6 @@ require (
github.com/pierrec/lz4/v4 v4.1.22
github.com/pkg/sftp v1.13.9
github.com/pkg/xattr v0.4.10
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/LINBIT/golinstor v0.55.0/go.mod h1:Al+or3qxnkEMBNHRBg37qygETyWfoDKfdm
github.com/Rican7/retry v0.3.0/go.mod h1:CxSDrhAyXmTMeEuRAnArMu1FHu48vtfjLREWqVl7Vw0=
github.com/Rican7/retry v0.3.1 h1:scY4IbO8swckzoA/11HgBwaZRJEyY9vaNJshcdhp1Mc=
github.com/Rican7/retry v0.3.1/go.mod h1:CxSDrhAyXmTMeEuRAnArMu1FHu48vtfjLREWqVl7Vw0=
github.com/adhocore/gronx v1.19.5 h1:cwIG4nT1v9DvadxtHBe6MzE+FZ1JDvAUC45U2fl4eSQ=
github.com/adhocore/gronx v1.19.5/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -408,8 +410,6 @@ github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlT
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
Expand Down
19 changes: 6 additions & 13 deletions shared/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"strings"
"time"

"github.com/adhocore/gronx"
"github.com/google/uuid"
"github.com/kballard/go-shellquote"
"github.com/robfig/cron/v3"
"gopkg.in/yaml.v2"

"github.com/lxc/incus/v6/shared/osarch"
Expand Down Expand Up @@ -599,22 +599,15 @@ func IsCron(aliases []string) func(value string) error {
return func(value string) error {
isValid := func(value string) error {
// Accept valid aliases.
for _, alias := range aliases {
if alias == value {
return nil
}
}

if len(strings.Split(value, " ")) != 5 {
return fmt.Errorf("Schedule must be of the form: <minute> <hour> <day-of-month> <month> <day-of-week>")
if slices.Contains(aliases, value) {
return nil
}

_, err := cron.ParseStandard(value)
if err != nil {
return fmt.Errorf("Error parsing schedule: %w", err)
if gronx.IsValid(value) {
return nil
}

return nil
return fmt.Errorf("Error parsing cron expr: %s", value)
}

// Can be comma+space separated (just commas are valid cron pattern).
Expand Down
Loading