refactor(coderd/schedule): move cron schedule to cron package (#9507)

This removes an indirect import of `coderd/database` from the CLI and
results in a logical separation between server related and generalized
schedule.

No size change (yet).

Ref: #9380
This commit is contained in:
Mathias Fredriksson
2023-09-04 16:48:25 +03:00
committed by GitHub
parent c31292abe8
commit ad23d33f28
15 changed files with 44 additions and 36 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/coderd/wsbuilder"
"github.com/coder/coder/v2/codersdk"
)
@ -306,7 +307,7 @@ func isEligibleForAutostart(ws database.Workspace, build database.WorkspaceBuild
return false
}
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
sched, err := cron.Weekly(ws.AutostartSchedule.String)
if err != nil {
return false
}

View File

@ -17,6 +17,7 @@ import (
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisioner/echo"
@ -784,9 +785,9 @@ func mustProvisionWorkspaceWithParameters(t *testing.T, client *codersdk.Client,
return coderdtest.MustWorkspace(t, client, ws.ID)
}
func mustSchedule(t *testing.T, s string) *schedule.Schedule {
func mustSchedule(t *testing.T, s string) *cron.Schedule {
t.Helper()
sched, err := schedule.Weekly(s)
sched, err := cron.Weekly(s)
require.NoError(t, err)
return sched
}

View File

@ -27,6 +27,7 @@ import (
"github.com/coder/coder/v2/coderd/gitauth"
"github.com/coder/coder/v2/coderd/provisionerdserver"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/coderd/telemetry"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionerd/proto"
@ -1330,7 +1331,7 @@ func TestCompleteJob(t *testing.T) {
}, nil
}
sched, err := schedule.Daily(c.userQuietHoursSchedule)
sched, err := cron.Daily(c.userQuietHoursSchedule)
if !assert.NoError(t, err) {
return schedule.UserQuietHoursScheduleOptions{}, err
}

View File

@ -16,6 +16,7 @@ import (
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/testutil"
)
@ -432,7 +433,7 @@ func TestCalculateAutoStop(t *testing.T) {
}, nil
}
sched, err := schedule.Daily(c.userQuietHoursSchedule)
sched, err := cron.Daily(c.userQuietHoursSchedule)
if !assert.NoError(t, err) {
return schedule.UserQuietHoursScheduleOptions{}, err
}

View File

@ -1,23 +1,23 @@
// package schedule provides utilities for managing template and workspace
// autostart and autostop schedules. This includes utilities for parsing and
// deserializing cron-style expressions.
package schedule
package cron
import (
"fmt"
"strings"
"time"
"github.com/robfig/cron/v3"
rbcron "github.com/robfig/cron/v3"
"golang.org/x/xerrors"
)
// For the purposes of this library, we only need minute, hour, and
// day-of-week. However to ensure interoperability we will use the standard
// five-valued cron format. Descriptors are not supported.
const parserFormat = cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow
const parserFormat = rbcron.Minute | rbcron.Hour | rbcron.Dom | rbcron.Month | rbcron.Dow
var defaultParser = cron.NewParser(parserFormat)
var defaultParser = rbcron.NewParser(parserFormat)
// Weekly parses a Schedule from spec scoped to a recurring weekly event.
// Spec consists of the following space-delimited fields, in the following order:
@ -30,11 +30,11 @@ var defaultParser = cron.NewParser(parserFormat)
//
// Example Usage:
//
// local_sched, _ := schedule.Weekly("59 23 *")
// local_sched, _ := cron.Weekly("59 23 *")
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))
// // Output: 2022-04-04T23:59:00Z
//
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")
// us_sched, _ := cron.Weekly("CRON_TZ=US/Central 30 9 1-5")
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
// // Output: 2022-04-04T14:30:00Z
func Weekly(raw string) (*Schedule, error) {
@ -56,11 +56,11 @@ func Weekly(raw string) (*Schedule, error) {
//
// Example Usage:
//
// local_sched, _ := schedule.Weekly("59 23 * * *")
// local_sched, _ := cron.Weekly("59 23 * * *")
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))
// // Output: 2022-04-04T23:59:00Z
//
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 * * *")
// us_sched, _ := cron.Weekly("CRON_TZ=US/Central 30 9 * * *")
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
// // Output: 2022-04-04T14:30:00Z
func Daily(raw string) (*Schedule, error) {
@ -83,7 +83,7 @@ func parse(raw string) (*Schedule, error) {
return nil, xerrors.Errorf("parse schedule: %w", err)
}
schedule, ok := specSched.(*cron.SpecSchedule)
schedule, ok := specSched.(*rbcron.SpecSchedule)
if !ok {
return nil, xerrors.Errorf("expected *cron.SpecSchedule but got %T", specSched)
}
@ -110,7 +110,7 @@ func parse(raw string) (*Schedule, error) {
// It's essentially a wrapper for robfig/cron/v3 that has additional
// convenience methods.
type Schedule struct {
sched *cron.SpecSchedule
sched *rbcron.SpecSchedule
// XXX: there isn't any nice way for robfig/cron to serialize
cronStr string
}

View File

@ -1,4 +1,4 @@
package schedule_test
package cron_test
import (
"testing"
@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
)
func Test_Weekly(t *testing.T) {
@ -144,7 +144,7 @@ func Test_Weekly(t *testing.T) {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
actual, err := schedule.Weekly(testCase.spec)
actual, err := cron.Weekly(testCase.spec)
if testCase.expectedError == "" {
nextTime := actual.Next(testCase.at)
require.NoError(t, err)

View File

@ -6,6 +6,7 @@ import (
"github.com/google/uuid"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/schedule/cron"
)
type UserQuietHoursScheduleOptions struct {
@ -17,7 +18,7 @@ type UserQuietHoursScheduleOptions struct {
// schedule (and UserSet will be false). If quiet hours schedules are not
// entitled or disabled instance-wide, this value will be nil to denote that
// quiet hours windows should not be used.
Schedule *Schedule
Schedule *cron.Schedule
UserSet bool
}

View File

@ -22,7 +22,7 @@ import (
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/coderd/searchquery"
"github.com/coder/coder/v2/coderd/telemetry"
"github.com/coder/coder/v2/coderd/util/ptr"
@ -1306,7 +1306,7 @@ func validWorkspaceSchedule(s *string) (sql.NullString, error) {
return sql.NullString{}, nil
}
_, err := schedule.Weekly(*s)
_, err := cron.Weekly(*s)
if err != nil {
return sql.NullString{}, err
}

View File

@ -30,6 +30,7 @@ import (
"github.com/coder/coder/v2/coderd/parameter"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
@ -1869,7 +1870,7 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
require.EqualValues(t, *testCase.schedule, *updated.AutostartSchedule, "expected autostart schedule to equal requested")
sched, err := schedule.Weekly(*updated.AutostartSchedule)
sched, err := cron.Weekly(*updated.AutostartSchedule)
require.NoError(t, err, "parse returned schedule")
next := sched.Next(testCase.at)