Skip to content

Commit e767803

Browse files
committed
Replace rebfig/cron/v3 with adhocore/gronx
Signed-off-by: JUN JIE NAN <[email protected]>
1 parent d24dd38 commit e767803

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

cmd/incusd/snapshot_common.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/robfig/cron/v3"
9+
"github.com/adhocore/gronx"
1010

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

100100
func cronSpecIsNow(spec string) (bool, error) {
101-
sched, err := cron.ParseStandard(spec)
102-
if err != nil {
103-
return false, fmt.Errorf("Could not parse cron '%s'", spec)
104-
}
105-
106101
// Check if it's time to snapshot
107102
now := time.Now()
108103

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

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

119117
if !now.Add(time.Minute).Equal(next) {
120118
return false, nil

cmd/incusd/snapshot_common_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import (
1010
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
1111
)
1212

13-
func (suite *containerTestSuite) TestSnapshotScheduling() {
13+
type snapshotCommonTestSuite struct {
14+
daemonTestSuite
15+
}
16+
17+
func (suite *snapshotCommonTestSuite) TestSnapshotScheduling() {
1418
args := db.InstanceArgs{
1519
Type: instancetype.Container,
1620
Ephemeral: false,
@@ -36,5 +40,6 @@ func (suite *containerTestSuite) TestSnapshotScheduling() {
3640
}
3741

3842
func TestSnapshotCommon(t *testing.T) {
39-
suite.Run(t, new(containerTestSuite))
43+
suite.Run(t, new(snapshotCommonTestSuite))
4044
}
45+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23.7
44

55
require (
66
github.com/LINBIT/golinstor v0.55.0
7+
github.com/adhocore/gronx v1.19.5
78
github.com/armon/go-proxyproto v0.1.0
89
github.com/cenkalti/backoff/v4 v4.3.0
910
github.com/checkpoint-restore/go-criu/v6 v6.3.0
@@ -44,7 +45,6 @@ require (
4445
github.com/pierrec/lz4/v4 v4.1.22
4546
github.com/pkg/sftp v1.13.9
4647
github.com/pkg/xattr v0.4.10
47-
github.com/robfig/cron/v3 v3.0.1
4848
github.com/sirupsen/logrus v1.9.3
4949
github.com/spf13/cobra v1.9.1
5050
github.com/spf13/pflag v1.0.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/LINBIT/golinstor v0.55.0/go.mod h1:Al+or3qxnkEMBNHRBg37qygETyWfoDKfdm
4444
github.com/Rican7/retry v0.3.0/go.mod h1:CxSDrhAyXmTMeEuRAnArMu1FHu48vtfjLREWqVl7Vw0=
4545
github.com/Rican7/retry v0.3.1 h1:scY4IbO8swckzoA/11HgBwaZRJEyY9vaNJshcdhp1Mc=
4646
github.com/Rican7/retry v0.3.1/go.mod h1:CxSDrhAyXmTMeEuRAnArMu1FHu48vtfjLREWqVl7Vw0=
47+
github.com/adhocore/gronx v1.19.5 h1:cwIG4nT1v9DvadxtHBe6MzE+FZ1JDvAUC45U2fl4eSQ=
48+
github.com/adhocore/gronx v1.19.5/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
4749
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
4850
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
4951
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
@@ -401,8 +403,6 @@ github.com/prometheus/procfs v0.16.0/go.mod h1:8veyXUu3nGP7oaCxhX6yeaM5u4stL2FeM
401403
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
402404
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
403405
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
404-
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
405-
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
406406
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
407407
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
408408
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=

shared/validate/validate.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"strings"
1414
"time"
1515

16+
"github.com/adhocore/gronx"
1617
"github.com/google/uuid"
1718
"github.com/kballard/go-shellquote"
18-
"github.com/robfig/cron/v3"
1919
"gopkg.in/yaml.v2"
2020

2121
"github.com/lxc/incus/v6/shared/osarch"
@@ -599,19 +599,13 @@ func IsCron(aliases []string) func(value string) error {
599599
return func(value string) error {
600600
isValid := func(value string) error {
601601
// Accept valid aliases.
602-
for _, alias := range aliases {
603-
if alias == value {
604-
return nil
605-
}
606-
}
607-
608-
if len(strings.Split(value, " ")) != 5 {
609-
return fmt.Errorf("Schedule must be of the form: <minute> <hour> <day-of-month> <month> <day-of-week>")
602+
if slices.Contains(aliases, value) {
603+
return nil
610604
}
611605

612-
_, err := cron.ParseStandard(value)
613-
if err != nil {
614-
return fmt.Errorf("Error parsing schedule: %w", err)
606+
valid := gronx.IsValid(value)
607+
if !valid {
608+
return fmt.Errorf("Error parsing cron expr: %s", value)
615609
}
616610

617611
return nil

0 commit comments

Comments
 (0)