Skip to content

Commit 4a0baa7

Browse files
committed
make frequency minute and hour consistent with the rest with '-ly'
1 parent fd846fd commit 4a0baa7

5 files changed

Lines changed: 92 additions & 35 deletions

File tree

docs/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Schedule Frequency Options
2+
3+
The `frequency` field in a schedule supports the following values:
4+
5+
- `minutely`: Every N minutes
6+
- `hourly`: Every N hours
7+
- `daily`: Every N days
8+
- `weekly`: Every N weeks
9+
- `monthly`: Every N months
10+
- `yearly`: Every N years
11+
12+
Example schedule JSON:
13+
14+
```json
15+
{
16+
"frequency": "hourly",
17+
"interval": 2
18+
}
19+
```
20+
21+
This will schedule the event every 2 hours.

internal/scheduler/expander.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ func (e *Expander) expandRecurringEvent(ctx context.Context, event *models.Event
130130

131131
// Calculate occurrences based on frequency
132132
switch schedule.Frequency {
133-
case "minute":
133+
case "minutely":
134134
for t := startTime; t.Before(endTime); t = t.Add(time.Duration(schedule.Interval) * time.Minute) {
135135
if t.After(graceStart) {
136136
occurrences = append(occurrences, t)
137137
}
138138
}
139-
case "hour":
139+
case "hourly":
140140
for t := startTime; t.Before(endTime); t = t.Add(time.Duration(schedule.Interval) * time.Hour) {
141141
if t.After(graceStart) {
142142
occurrences = append(occurrences, t)

internal/scheduler/expander_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,60 @@ func TestEventExpander(t *testing.T) {
328328
assert.Contains(t, scheduledDays, time.Wednesday)
329329
assert.NotContains(t, scheduledDays, time.Monday)
330330
})
331+
332+
t.Run("minutely frequency event expansion", func(t *testing.T) {
333+
cleanup()
334+
startTime := time.Now().Truncate(time.Minute)
335+
scheduleConfig := &models.ScheduleConfig{
336+
Frequency: "minutely",
337+
Interval: 2,
338+
}
339+
event := &models.Event{
340+
ID: uuid.New(),
341+
Name: "Minutely Event",
342+
Description: "Every 2 minutes",
343+
StartTime: startTime,
344+
Webhook: "http://example.com",
345+
Schedule: scheduleConfig,
346+
Status: models.EventStatusActive,
347+
Metadata: []byte(`{"key": "value"}`),
348+
Tags: pq.StringArray{"test"},
349+
CreatedAt: time.Now(),
350+
}
351+
err := eventRepo.Create(ctx, event)
352+
require.NoError(t, err)
353+
err = expander.ExpandEvents(ctx)
354+
require.NoError(t, err)
355+
results, err := redisClient.ZRange(ctx, namespace+ScheduleKey, 0, -1).Result()
356+
require.NoError(t, err)
357+
assert.NotEmpty(t, results)
358+
})
359+
360+
t.Run("hourly frequency event expansion", func(t *testing.T) {
361+
cleanup()
362+
startTime := time.Now().Truncate(time.Hour)
363+
scheduleConfig := &models.ScheduleConfig{
364+
Frequency: "hourly",
365+
Interval: 3,
366+
}
367+
event := &models.Event{
368+
ID: uuid.New(),
369+
Name: "Hourly Event",
370+
Description: "Every 3 hours",
371+
StartTime: startTime,
372+
Webhook: "http://example.com",
373+
Schedule: scheduleConfig,
374+
Status: models.EventStatusActive,
375+
Metadata: []byte(`{"key": "value"}`),
376+
Tags: pq.StringArray{"test"},
377+
CreatedAt: time.Now(),
378+
}
379+
err := eventRepo.Create(ctx, event)
380+
require.NoError(t, err)
381+
err = expander.ExpandEvents(ctx)
382+
require.NoError(t, err)
383+
results, err := redisClient.ZRange(ctx, namespace+ScheduleKey, 0, -1).Result()
384+
require.NoError(t, err)
385+
assert.NotEmpty(t, results)
386+
})
331387
}

internal/scheduler/schedule.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ const (
1919
type Frequency string
2020

2121
const (
22-
FrequencyMinute Frequency = "minute"
23-
FrequencyHour Frequency = "hour"
24-
FrequencyDaily Frequency = "daily"
25-
FrequencyWeekly Frequency = "weekly"
26-
FrequencyMonthly Frequency = "monthly"
27-
FrequencyYearly Frequency = "yearly"
22+
FrequencyMinutely Frequency = "minutely"
23+
FrequencyHourly Frequency = "hourly"
24+
FrequencyDaily Frequency = "daily"
25+
FrequencyWeekly Frequency = "weekly"
26+
FrequencyMonthly Frequency = "monthly"
27+
FrequencyYearly Frequency = "yearly"
2828
)
2929

3030
// DayOfWeek represents a day of the week
@@ -77,7 +77,7 @@ func ParseSchedule(scheduleStr string, logger *zap.Logger) (*Schedule, error) {
7777
func (s *Schedule) Validate() error {
7878
// Validate frequency
7979
switch s.Frequency {
80-
case FrequencyMinute, FrequencyHour, FrequencyDaily, FrequencyWeekly, FrequencyMonthly, FrequencyYearly:
80+
case FrequencyMinutely, FrequencyHourly, FrequencyDaily, FrequencyWeekly, FrequencyMonthly, FrequencyYearly:
8181
default:
8282
return fmt.Errorf("invalid frequency: %s", s.Frequency)
8383
}
@@ -150,9 +150,9 @@ func (s *Schedule) GetNextOccurrences(startTime time.Time, endTime time.Time, lo
150150

151151
// Move to next interval
152152
switch s.Frequency {
153-
case FrequencyMinute:
153+
case FrequencyMinutely:
154154
current = current.Add(time.Duration(s.Interval) * time.Minute)
155-
case FrequencyHour:
155+
case FrequencyHourly:
156156
current = current.Add(time.Duration(s.Interval) * time.Hour)
157157
case FrequencyDaily:
158158
current = current.AddDate(0, 0, s.Interval)

internal/scheduler/scheduler.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ func (s *Scheduler) calculateNextOccurrence(event *models.Event) (*time.Time, er
177177
// Handle different frequencies
178178
var nextTime time.Time
179179
switch schedule.Frequency {
180-
case "minute":
180+
case "minutely":
181181
nextTime = event.StartTime.Add(time.Duration(schedule.Interval) * time.Minute)
182-
case "hour":
182+
case "hourly":
183183
nextTime = event.StartTime.Add(time.Duration(schedule.Interval) * time.Hour)
184184
case "daily":
185185
nextTime = event.StartTime.AddDate(0, 0, schedule.Interval)
@@ -190,30 +190,10 @@ func (s *Scheduler) calculateNextOccurrence(event *models.Event) (*time.Time, er
190190
case "yearly":
191191
nextTime = event.StartTime.AddDate(schedule.Interval, 0, 0)
192192
default:
193-
return nil, fmt.Errorf("invalid frequency: %s", schedule.Frequency)
193+
return nil, fmt.Errorf("unsupported frequency: %s", schedule.Frequency)
194194
}
195-
196-
// Check if we've exceeded count
197-
if schedule.Count != nil {
198-
// TODO: Implement count check
199-
return nil, nil
200-
}
201-
202-
// Check if we've exceeded until date
203-
if schedule.Until != nil {
204-
untilTime, err := time.Parse(time.RFC3339, *schedule.Until)
205-
if err != nil {
206-
return nil, fmt.Errorf("invalid until date: %w", err)
207-
}
208-
if nextTime.After(untilTime) {
209-
return nil, nil
210-
}
211-
}
212-
213-
// Check if the next occurrence is in the past
214195
if nextTime.Before(now) {
215-
return nil, nil
196+
nextTime = now
216197
}
217-
218198
return &nextTime, nil
219199
}

0 commit comments

Comments
 (0)