Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad23d33

Browse files
authored
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
1 parent c31292a commit ad23d33

15 files changed

+44
-36
lines changed

cli/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/coder/coder/v2/cli/clibase"
1111
"github.com/coder/coder/v2/cli/cliui"
12-
"github.com/coder/coder/v2/coderd/schedule"
12+
"github.com/coder/coder/v2/coderd/schedule/cron"
1313
"github.com/coder/coder/v2/coderd/util/ptr"
1414
"github.com/coder/coder/v2/codersdk"
1515
)
@@ -39,7 +39,7 @@ func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]coders
3939
lastBuilt := now.UTC().Sub(workspace.LatestBuild.Job.CreatedAt).Truncate(time.Second)
4040
autostartDisplay := "-"
4141
if !ptr.NilOrEmpty(workspace.AutostartSchedule) {
42-
if sched, err := schedule.Weekly(*workspace.AutostartSchedule); err == nil {
42+
if sched, err := cron.Weekly(*workspace.AutostartSchedule); err == nil {
4343
autostartDisplay = fmt.Sprintf("%s %s (%s)", sched.Time(), sched.DaysOfWeek(), sched.Location())
4444
}
4545
}

cli/schedule.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/coder/coder/v2/cli/clibase"
1212
"github.com/coder/coder/v2/cli/cliui"
13-
"github.com/coder/coder/v2/coderd/schedule"
13+
"github.com/coder/coder/v2/coderd/schedule/cron"
1414
"github.com/coder/coder/v2/coderd/util/ptr"
1515
"github.com/coder/coder/v2/coderd/util/tz"
1616
"github.com/coder/coder/v2/codersdk"
@@ -255,7 +255,7 @@ func displaySchedule(workspace codersdk.Workspace, out io.Writer) error {
255255
schedNextStop = "-"
256256
)
257257
if !ptr.NilOrEmpty(workspace.AutostartSchedule) {
258-
sched, err := schedule.Weekly(ptr.NilToEmpty(workspace.AutostartSchedule))
258+
sched, err := cron.Weekly(ptr.NilToEmpty(workspace.AutostartSchedule))
259259
if err != nil {
260260
// This should never happen.
261261
_, _ = fmt.Fprintf(out, "Invalid autostart schedule %q for workspace %s: %s\n", *workspace.AutostartSchedule, workspace.Name, err.Error())

cli/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"golang.org/x/xerrors"
1010

11-
"github.com/coder/coder/v2/coderd/schedule"
11+
"github.com/coder/coder/v2/coderd/schedule/cron"
1212
"github.com/coder/coder/v2/coderd/util/tz"
1313
)
1414

@@ -74,7 +74,7 @@ func relative(d time.Duration) string {
7474
}
7575

7676
// parseCLISchedule parses a schedule in the format HH:MM{AM|PM} [DOW] [LOCATION]
77-
func parseCLISchedule(parts ...string) (*schedule.Schedule, error) {
77+
func parseCLISchedule(parts ...string) (*cron.Schedule, error) {
7878
// If the user was careful and quoted the schedule, un-quote it.
7979
// In the case that only time was specified, this will be a no-op.
8080
if len(parts) == 1 {
@@ -121,7 +121,7 @@ func parseCLISchedule(parts ...string) (*schedule.Schedule, error) {
121121
}
122122
}
123123

124-
sched, err := schedule.Weekly(fmt.Sprintf(
124+
sched, err := cron.Weekly(fmt.Sprintf(
125125
"CRON_TZ=%s %d %d * * %s",
126126
loc.String(),
127127
minute,

coderd/autobuild/lifecycle_executor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/coder/coder/v2/coderd/database/dbauthz"
1818
"github.com/coder/coder/v2/coderd/database/dbtime"
1919
"github.com/coder/coder/v2/coderd/schedule"
20+
"github.com/coder/coder/v2/coderd/schedule/cron"
2021
"github.com/coder/coder/v2/coderd/wsbuilder"
2122
"github.com/coder/coder/v2/codersdk"
2223
)
@@ -306,7 +307,7 @@ func isEligibleForAutostart(ws database.Workspace, build database.WorkspaceBuild
306307
return false
307308
}
308309

309-
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
310+
sched, err := cron.Weekly(ws.AutostartSchedule.String)
310311
if err != nil {
311312
return false
312313
}

coderd/autobuild/lifecycle_executor_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/coder/coder/v2/coderd/coderdtest"
1818
"github.com/coder/coder/v2/coderd/database"
1919
"github.com/coder/coder/v2/coderd/schedule"
20+
"github.com/coder/coder/v2/coderd/schedule/cron"
2021
"github.com/coder/coder/v2/coderd/util/ptr"
2122
"github.com/coder/coder/v2/codersdk"
2223
"github.com/coder/coder/v2/provisioner/echo"
@@ -784,9 +785,9 @@ func mustProvisionWorkspaceWithParameters(t *testing.T, client *codersdk.Client,
784785
return coderdtest.MustWorkspace(t, client, ws.ID)
785786
}
786787

787-
func mustSchedule(t *testing.T, s string) *schedule.Schedule {
788+
func mustSchedule(t *testing.T, s string) *cron.Schedule {
788789
t.Helper()
789-
sched, err := schedule.Weekly(s)
790+
sched, err := cron.Weekly(s)
790791
require.NoError(t, err)
791792
return sched
792793
}

coderd/provisionerdserver/provisionerdserver_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/coder/coder/v2/coderd/gitauth"
2828
"github.com/coder/coder/v2/coderd/provisionerdserver"
2929
"github.com/coder/coder/v2/coderd/schedule"
30+
"github.com/coder/coder/v2/coderd/schedule/cron"
3031
"github.com/coder/coder/v2/coderd/telemetry"
3132
"github.com/coder/coder/v2/codersdk"
3233
"github.com/coder/coder/v2/provisionerd/proto"
@@ -1330,7 +1331,7 @@ func TestCompleteJob(t *testing.T) {
13301331
}, nil
13311332
}
13321333

1333-
sched, err := schedule.Daily(c.userQuietHoursSchedule)
1334+
sched, err := cron.Daily(c.userQuietHoursSchedule)
13341335
if !assert.NoError(t, err) {
13351336
return schedule.UserQuietHoursScheduleOptions{}, err
13361337
}

coderd/schedule/autostop_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1717
"github.com/coder/coder/v2/coderd/database/dbtime"
1818
"github.com/coder/coder/v2/coderd/schedule"
19+
"github.com/coder/coder/v2/coderd/schedule/cron"
1920
"github.com/coder/coder/v2/testutil"
2021
)
2122

@@ -432,7 +433,7 @@ func TestCalculateAutoStop(t *testing.T) {
432433
}, nil
433434
}
434435

435-
sched, err := schedule.Daily(c.userQuietHoursSchedule)
436+
sched, err := cron.Daily(c.userQuietHoursSchedule)
436437
if !assert.NoError(t, err) {
437438
return schedule.UserQuietHoursScheduleOptions{}, err
438439
}

coderd/schedule/cron.go renamed to coderd/schedule/cron/cron.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
// package schedule provides utilities for managing template and workspace
22
// autostart and autostop schedules. This includes utilities for parsing and
33
// deserializing cron-style expressions.
4-
package schedule
4+
package cron
55

66
import (
77
"fmt"
88
"strings"
99
"time"
1010

11-
"github.com/robfig/cron/v3"
11+
rbcron "github.com/robfig/cron/v3"
1212
"golang.org/x/xerrors"
1313
)
1414

1515
// For the purposes of this library, we only need minute, hour, and
1616
// day-of-week. However to ensure interoperability we will use the standard
1717
// five-valued cron format. Descriptors are not supported.
18-
const parserFormat = cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow
18+
const parserFormat = rbcron.Minute | rbcron.Hour | rbcron.Dom | rbcron.Month | rbcron.Dow
1919

20-
var defaultParser = cron.NewParser(parserFormat)
20+
var defaultParser = rbcron.NewParser(parserFormat)
2121

2222
// Weekly parses a Schedule from spec scoped to a recurring weekly event.
2323
// Spec consists of the following space-delimited fields, in the following order:
@@ -30,11 +30,11 @@ var defaultParser = cron.NewParser(parserFormat)
3030
//
3131
// Example Usage:
3232
//
33-
// local_sched, _ := schedule.Weekly("59 23 *")
33+
// local_sched, _ := cron.Weekly("59 23 *")
3434
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))
3535
// // Output: 2022-04-04T23:59:00Z
3636
//
37-
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")
37+
// us_sched, _ := cron.Weekly("CRON_TZ=US/Central 30 9 1-5")
3838
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
3939
// // Output: 2022-04-04T14:30:00Z
4040
func Weekly(raw string) (*Schedule, error) {
@@ -56,11 +56,11 @@ func Weekly(raw string) (*Schedule, error) {
5656
//
5757
// Example Usage:
5858
//
59-
// local_sched, _ := schedule.Weekly("59 23 * * *")
59+
// local_sched, _ := cron.Weekly("59 23 * * *")
6060
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))
6161
// // Output: 2022-04-04T23:59:00Z
6262
//
63-
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 * * *")
63+
// us_sched, _ := cron.Weekly("CRON_TZ=US/Central 30 9 * * *")
6464
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
6565
// // Output: 2022-04-04T14:30:00Z
6666
func Daily(raw string) (*Schedule, error) {
@@ -83,7 +83,7 @@ func parse(raw string) (*Schedule, error) {
8383
return nil, xerrors.Errorf("parse schedule: %w", err)
8484
}
8585

86-
schedule, ok := specSched.(*cron.SpecSchedule)
86+
schedule, ok := specSched.(*rbcron.SpecSchedule)
8787
if !ok {
8888
return nil, xerrors.Errorf("expected *cron.SpecSchedule but got %T", specSched)
8989
}
@@ -110,7 +110,7 @@ func parse(raw string) (*Schedule, error) {
110110
// It's essentially a wrapper for robfig/cron/v3 that has additional
111111
// convenience methods.
112112
type Schedule struct {
113-
sched *cron.SpecSchedule
113+
sched *rbcron.SpecSchedule
114114
// XXX: there isn't any nice way for robfig/cron to serialize
115115
cronStr string
116116
}

coderd/schedule/cron_test.go renamed to coderd/schedule/cron/cron_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package schedule_test
1+
package cron_test
22

33
import (
44
"testing"
55
"time"
66

77
"github.com/stretchr/testify/require"
88

9-
"github.com/coder/coder/v2/coderd/schedule"
9+
"github.com/coder/coder/v2/coderd/schedule/cron"
1010
)
1111

1212
func Test_Weekly(t *testing.T) {
@@ -144,7 +144,7 @@ func Test_Weekly(t *testing.T) {
144144
testCase := testCase
145145
t.Run(testCase.name, func(t *testing.T) {
146146
t.Parallel()
147-
actual, err := schedule.Weekly(testCase.spec)
147+
actual, err := cron.Weekly(testCase.spec)
148148
if testCase.expectedError == "" {
149149
nextTime := actual.Next(testCase.at)
150150
require.NoError(t, err)

coderd/schedule/user.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/google/uuid"
77

88
"github.com/coder/coder/v2/coderd/database"
9+
"github.com/coder/coder/v2/coderd/schedule/cron"
910
)
1011

1112
type UserQuietHoursScheduleOptions struct {
@@ -17,7 +18,7 @@ type UserQuietHoursScheduleOptions struct {
1718
// schedule (and UserSet will be false). If quiet hours schedules are not
1819
// entitled or disabled instance-wide, this value will be nil to denote that
1920
// quiet hours windows should not be used.
20-
Schedule *Schedule
21+
Schedule *cron.Schedule
2122
UserSet bool
2223
}
2324

coderd/workspaces.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/coder/coder/v2/coderd/httpapi"
2323
"github.com/coder/coder/v2/coderd/httpmw"
2424
"github.com/coder/coder/v2/coderd/rbac"
25-
"github.com/coder/coder/v2/coderd/schedule"
25+
"github.com/coder/coder/v2/coderd/schedule/cron"
2626
"github.com/coder/coder/v2/coderd/searchquery"
2727
"github.com/coder/coder/v2/coderd/telemetry"
2828
"github.com/coder/coder/v2/coderd/util/ptr"
@@ -1306,7 +1306,7 @@ func validWorkspaceSchedule(s *string) (sql.NullString, error) {
13061306
return sql.NullString{}, nil
13071307
}
13081308

1309-
_, err := schedule.Weekly(*s)
1309+
_, err := cron.Weekly(*s)
13101310
if err != nil {
13111311
return sql.NullString{}, err
13121312
}

coderd/workspaces_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/coder/coder/v2/coderd/parameter"
3131
"github.com/coder/coder/v2/coderd/rbac"
3232
"github.com/coder/coder/v2/coderd/schedule"
33+
"github.com/coder/coder/v2/coderd/schedule/cron"
3334
"github.com/coder/coder/v2/coderd/util/ptr"
3435
"github.com/coder/coder/v2/codersdk"
3536
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -1869,7 +1870,7 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
18691870

18701871
require.EqualValues(t, *testCase.schedule, *updated.AutostartSchedule, "expected autostart schedule to equal requested")
18711872

1872-
sched, err := schedule.Weekly(*updated.AutostartSchedule)
1873+
sched, err := cron.Weekly(*updated.AutostartSchedule)
18731874
require.NoError(t, err, "parse returned schedule")
18741875

18751876
next := sched.Next(testCase.at)

enterprise/coderd/schedule/user.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/coder/coder/v2/coderd/database"
1111
agpl "github.com/coder/coder/v2/coderd/schedule"
12+
"github.com/coder/coder/v2/coderd/schedule/cron"
1213
"github.com/coder/coder/v2/coderd/tracing"
1314
)
1415

@@ -49,7 +50,7 @@ func (s *enterpriseUserQuietHoursScheduleStore) parseSchedule(ctx context.Contex
4950
rawSchedule = s.defaultSchedule
5051
}
5152

52-
sched, err := agpl.Daily(rawSchedule)
53+
sched, err := cron.Daily(rawSchedule)
5354
if err != nil {
5455
// This shouldn't get hit during Gets, only Sets.
5556
return agpl.UserQuietHoursScheduleOptions{}, xerrors.Errorf("parse daily schedule %q: %w", rawSchedule, err)

enterprise/coderd/users_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/require"
99

1010
"github.com/coder/coder/v2/coderd/coderdtest"
11-
"github.com/coder/coder/v2/coderd/schedule"
11+
"github.com/coder/coder/v2/coderd/schedule/cron"
1212
"github.com/coder/coder/v2/codersdk"
1313
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1414
"github.com/coder/coder/v2/enterprise/coderd/license"
@@ -22,14 +22,14 @@ func TestUserQuietHours(t *testing.T) {
2222
t.Parallel()
2323

2424
defaultQuietHoursSchedule := "CRON_TZ=America/Chicago 0 0 * * *"
25-
defaultScheduleParsed, err := schedule.Daily(defaultQuietHoursSchedule)
25+
defaultScheduleParsed, err := cron.Daily(defaultQuietHoursSchedule)
2626
require.NoError(t, err)
2727
nextTime := defaultScheduleParsed.Next(time.Now().In(defaultScheduleParsed.Location()))
2828
if time.Until(nextTime) < time.Hour {
2929
// Use a different default schedule instead, because we want to avoid
3030
// the schedule "ticking over" during this test run.
3131
defaultQuietHoursSchedule = "CRON_TZ=America/Chicago 0 12 * * *"
32-
defaultScheduleParsed, err = schedule.Daily(defaultQuietHoursSchedule)
32+
defaultScheduleParsed, err = cron.Daily(defaultQuietHoursSchedule)
3333
require.NoError(t, err)
3434
}
3535

@@ -61,14 +61,14 @@ func TestUserQuietHours(t *testing.T) {
6161

6262
// Set their quiet hours.
6363
customQuietHoursSchedule := "CRON_TZ=Australia/Sydney 0 0 * * *"
64-
customScheduleParsed, err := schedule.Daily(customQuietHoursSchedule)
64+
customScheduleParsed, err := cron.Daily(customQuietHoursSchedule)
6565
require.NoError(t, err)
6666
nextTime = customScheduleParsed.Next(time.Now().In(customScheduleParsed.Location()))
6767
if time.Until(nextTime) < time.Hour {
6868
// Use a different default schedule instead, because we want to avoid
6969
// the schedule "ticking over" during this test run.
7070
customQuietHoursSchedule = "CRON_TZ=Australia/Sydney 0 12 * * *"
71-
customScheduleParsed, err = schedule.Daily(customQuietHoursSchedule)
71+
customScheduleParsed, err = cron.Daily(customQuietHoursSchedule)
7272
require.NoError(t, err)
7373
}
7474

enterprise/coderd/workspaces_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/v2/coderd/coderdtest"
1717
"github.com/coder/coder/v2/coderd/database"
1818
agplschedule "github.com/coder/coder/v2/coderd/schedule"
19+
"github.com/coder/coder/v2/coderd/schedule/cron"
1920
"github.com/coder/coder/v2/coderd/util/ptr"
2021
"github.com/coder/coder/v2/codersdk"
2122
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
@@ -585,7 +586,7 @@ func TestWorkspaceAutobuild(t *testing.T) {
585586

586587
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
587588

588-
sched, err := agplschedule.Weekly("CRON_TZ=UTC 0 * * * *")
589+
sched, err := cron.Weekly("CRON_TZ=UTC 0 * * * *")
589590
require.NoError(t, err)
590591

591592
ws := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {

0 commit comments

Comments
 (0)