Skip to content

Commit ff41e18

Browse files
minor fixes
1 parent 6cae709 commit ff41e18

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

coderd/prebuilds/preset_snapshot.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import (
55
"slices"
66
"time"
77

8-
"golang.org/x/xerrors"
9-
10-
"github.com/coder/coder/v2/coderd/schedule/cron"
11-
12-
"github.com/google/uuid"
13-
148
"github.com/coder/quartz"
9+
"github.com/google/uuid"
10+
"golang.org/x/xerrors"
1511

1612
"github.com/coder/coder/v2/coderd/database"
13+
"github.com/coder/coder/v2/coderd/schedule/cron"
1714
)
1815

1916
// ActionType represents the type of action needed to reconcile prebuilds.
@@ -106,16 +103,19 @@ func MatchesCron(cronExpression string, at time.Time) (bool, error) {
106103
// Otherwise, it falls back to the default number of instances specified in the prebuild configuration.
107104
func (p PresetSnapshot) CalculateDesiredInstances(at time.Time) (int32, error) {
108105
if len(p.PrebuildSchedules) == 0 {
106+
// If no schedules are defined, fall back to the default desired instance count
109107
return p.Preset.DesiredInstances.Int32, nil
110108
}
111109

110+
// Validate that the provided timezone is valid
112111
_, err := time.LoadLocation(p.Preset.AutoscalingTimezone)
113112
if err != nil {
114113
return 0, xerrors.Errorf("failed to parse location %v: %w", p.Preset.AutoscalingTimezone, err)
115114
}
116115

117-
// Check each schedule
116+
// Look for a schedule whose cron expression matches the provided time
118117
for _, schedule := range p.PrebuildSchedules {
118+
// Prefix the cron expression with timezone information
119119
cronExprWithTimezone := fmt.Sprintf("CRON_TZ=%s %s", p.Preset.AutoscalingTimezone, schedule.CronExpression)
120120
matches, err := MatchesCron(cronExprWithTimezone, at)
121121
if err != nil {
@@ -126,6 +126,7 @@ func (p PresetSnapshot) CalculateDesiredInstances(at time.Time) (int32, error) {
126126
}
127127
}
128128

129+
// If no schedule matches, fall back to the default desired instance count
129130
return p.Preset.DesiredInstances.Int32, nil
130131
}
131132

@@ -160,8 +161,9 @@ func (p PresetSnapshot) CalculateState() *ReconciliationState {
160161
var err error
161162
desired, err = p.CalculateDesiredInstances(p.clock.Now())
162163
if err != nil {
163-
// TODO: handle error
164-
panic(err)
164+
// In case of an error, fall back to the default desired instance count
165+
desired = p.Preset.DesiredInstances.Int32
166+
// TODO: log error
165167
}
166168
eligible = p.countEligible()
167169
extraneous = max(actual-expired-desired, 0)

0 commit comments

Comments
 (0)