@@ -5,15 +5,12 @@ import (
5
5
"slices"
6
6
"time"
7
7
8
- "golang.org/x/xerrors"
9
-
10
- "github.com/coder/coder/v2/coderd/schedule/cron"
11
-
12
- "github.com/google/uuid"
13
-
14
8
"github.com/coder/quartz"
9
+ "github.com/google/uuid"
10
+ "golang.org/x/xerrors"
15
11
16
12
"github.com/coder/coder/v2/coderd/database"
13
+ "github.com/coder/coder/v2/coderd/schedule/cron"
17
14
)
18
15
19
16
// ActionType represents the type of action needed to reconcile prebuilds.
@@ -106,16 +103,19 @@ func MatchesCron(cronExpression string, at time.Time) (bool, error) {
106
103
// Otherwise, it falls back to the default number of instances specified in the prebuild configuration.
107
104
func (p PresetSnapshot )CalculateDesiredInstances (at time.Time ) (int32 ,error ) {
108
105
if len (p .PrebuildSchedules )== 0 {
106
+ // If no schedules are defined, fall back to the default desired instance count
109
107
return p .Preset .DesiredInstances .Int32 ,nil
110
108
}
111
109
110
+ // Validate that the provided timezone is valid
112
111
_ ,err := time .LoadLocation (p .Preset .AutoscalingTimezone )
113
112
if err != nil {
114
113
return 0 ,xerrors .Errorf ("failed to parse location %v: %w" ,p .Preset .AutoscalingTimezone ,err )
115
114
}
116
115
117
- //Check each schedule
116
+ //Look for a schedule whose cron expression matches the provided time
118
117
for _ ,schedule := range p .PrebuildSchedules {
118
+ // Prefix the cron expression with timezone information
119
119
cronExprWithTimezone := fmt .Sprintf ("CRON_TZ=%s %s" ,p .Preset .AutoscalingTimezone ,schedule .CronExpression )
120
120
matches ,err := MatchesCron (cronExprWithTimezone ,at )
121
121
if err != nil {
@@ -126,6 +126,7 @@ func (p PresetSnapshot) CalculateDesiredInstances(at time.Time) (int32, error) {
126
126
}
127
127
}
128
128
129
+ // If no schedule matches, fall back to the default desired instance count
129
130
return p .Preset .DesiredInstances .Int32 ,nil
130
131
}
131
132
@@ -160,8 +161,9 @@ func (p PresetSnapshot) CalculateState() *ReconciliationState {
160
161
var err error
161
162
desired ,err = p .CalculateDesiredInstances (p .clock .Now ())
162
163
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
165
167
}
166
168
eligible = p .countEligible ()
167
169
extraneous = max (actual - expired - desired ,0 )