Skip to content

feat: add crontab package for supporting autostart/stop. #844

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 9 commits into from
Apr 4, 2022
Prev Previous commit
Next Next commit
fixup! feat: add crontab package for supporting autostart/stop. This …
…is basically a small wrapper around robfig/cron/v3.
  • Loading branch information
johnstcn committed Apr 4, 2022
commit c92c8eb15eb0c8dc6ebebd3aef0f52a156645ee7
10 changes: 5 additions & 5 deletions coderd/crontab/crontab.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ func (s cronSchedule) Next(t time.Time) time.Time {
}

func Parse(spec string) (WeeklySchedule, error) {
s, err := defaultParser.Parse(spec)
specSched, err := defaultParser.Parse(spec)
if err != nil {
return nil, xerrors.Errorf("parse schedule: %w", err)
}

schedule, ok := s.(*cron.SpecSchedule)
schedule, ok := specSched.(*cron.SpecSchedule)
if !ok {
return nil, xerrors.Errorf("expected cron.SpecSchedule but got %T", s)
return nil, xerrors.Errorf("expected *cron.SpecSchedule but got %T", specSched)
}

cs := &cronSchedule{
cronSched := &cronSchedule{
sched: schedule,
spec: spec,
}
return cs, nil
return cronSched, nil

}
21 changes: 11 additions & 10 deletions coderd/crontab/crontab_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package crontab
package crontab_test

import (
"testing"
"time"

"github.com/coder/coder/coderd/cron"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -46,18 +47,18 @@ func Test_Parse(t *testing.T) {
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
actual, err := Parse(tc.spec)
if tc.expectedError == "" {
nextTime := actual.Next(tc.at)
actual, err := cron.Parse(testCase.spec)
if testCase.expectedError == "" {
nextTime := actual.Next(testCase.at)
require.NoError(t, err)
require.Equal(t, tc.expectedNext, nextTime)
require.Equal(t, tc.spec, actual.String())
require.Equal(t, testCase.expectedNext, nextTime)
require.Equal(t, testCase.spec, actual.String())
} else {
require.EqualError(t, err, tc.expectedError)
require.EqualError(t, err, testCase.expectedError)
require.Nil(t, actual)
}
})
Expand Down