Skip to content
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

jobs: Add every_hour and every_day extentions to the cron expression. #76373

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 18 additions & 2 deletions pkg/jobs/scheduled_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package jobs
import (
"context"
"fmt"
"math/rand"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -188,13 +189,28 @@ func (j *ScheduledJob) HasRecurringSchedule() bool {
return len(j.rec.ScheduleExpr) > 0
}

// parseCronExpr parses cron expression, with added support for
// cockroach specific mnemonics.
func parseCronExpr(expr string) (cron.Schedule, error) {
switch strings.ToLower(expr) {
case "@every_hour":
// Schedule runs once an hour at a randomly chosen minute.
return cron.ParseStandard(fmt.Sprintf("%d * * * *", rand.Intn(60)))
case "@every_day":
// Schedule runs once a day at a randomly chosen time.
return cron.ParseStandard(fmt.Sprintf("%d %d * * *", rand.Intn(60), rand.Intn(24)))
default:
return cron.ParseStandard(expr)
}
}

// Frequency returns how often this schedule executes.
func (j *ScheduledJob) Frequency() (time.Duration, error) {
if !j.HasRecurringSchedule() {
return 0, errors.Newf(
"schedule %d is not periodic", j.rec.ScheduleID)
}
expr, err := cron.ParseStandard(j.rec.ScheduleExpr)
expr, err := parseCronExpr(j.rec.ScheduleExpr)
if err != nil {
return 0, errors.Wrapf(err,
"parsing schedule expression: %q; it must be a valid cron expression",
Expand All @@ -212,7 +228,7 @@ func (j *ScheduledJob) ScheduleNextRun() error {
return errors.Newf(
"cannot set next run for schedule %d (empty schedule)", j.rec.ScheduleID)
}
expr, err := cron.ParseStandard(j.rec.ScheduleExpr)
expr, err := parseCronExpr(j.rec.ScheduleExpr)
if err != nil {
return errors.Wrapf(err, "parsing schedule expression: %q", j.rec.ScheduleExpr)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/jobs/scheduled_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -117,3 +118,22 @@ func TestPauseUnpauseJob(t *testing.T) {
require.False(t, loaded.IsPaused())
require.False(t, loaded.NextRun().Equal(time.Time{}))
}

func TestCrdbCronExtensions(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

frequency := func(s cron.Schedule) time.Duration {
notALeapYear := time.Date(2022, 1, 2, 3, 4, 5, 6, time.UTC)
nextRun := s.Next(notALeapYear)
return s.Next(nextRun).Sub(nextRun)
}

schedule, err := parseCronExpr("@every_hour")
require.NoError(t, err)
require.Equal(t, time.Hour, frequency(schedule))

schedule, err = parseCronExpr("@every_day")
require.NoError(t, err)
require.Equal(t, 24*time.Hour, frequency(schedule))
}