|
1 | | -// internal/scheduler/scheduler.go |
2 | | -package scheduler |
| 1 | +// internal/eventbridge/scheduler.gopackage eventbridge |
| 2 | + |
| 3 | +package eventbridge |
3 | 4 |
|
4 | 5 | import ( |
5 | 6 | "context" |
@@ -31,22 +32,43 @@ func NewService(cfg appconfig.Config, schedulerClient *scheduler.Client) *Servic |
31 | 32 | } |
32 | 33 | } |
33 | 34 |
|
34 | | -// CreateOrUpdateSchedule handles the idempotent logic for creating/updating a schedule. |
35 | | -func (s *Service) CreateOrUpdateSchedule(sessionID string, scheduleTime time.Time, namePrefix, queueArn, action, logContext string) error { |
| 35 | +// CreateOrUpdateSchedule handles the idempotent logic for creating/updating a standard schedule. |
| 36 | +func (s *Service) CreateOrUpdateSchedule(sessionID string, scheduleTime time.Time, namePrefix, action, logContext string) error { |
| 37 | + // Create standard message body |
| 38 | + messageBody := models.SQSMessageBody{ |
| 39 | + SessionID: sessionID, |
| 40 | + Action: action, |
| 41 | + } |
| 42 | + |
| 43 | + // Use the common scheduling method with the Session Scheduling Queue ARN |
| 44 | + return s.createOrUpdateScheduleWithPayload(sessionID, scheduleTime, namePrefix, s.Config.SQSSessionSchedulingQueueARN, messageBody, logContext) |
| 45 | +} |
| 46 | + |
| 47 | +// CreateOrUpdateReminderSchedule creates or updates a reminder-specific schedule |
| 48 | +func (s *Service) CreateOrUpdateReminderSchedule(sessionID string, scheduleTime time.Time, namePrefix, action, reminderType, logContext string) error { |
| 49 | + // Create reminder-specific message body with additional fields |
| 50 | + messageBody := models.SQSReminderMessageBody{ |
| 51 | + SessionID: sessionID, |
| 52 | + Action: action, |
| 53 | + ReminderType: reminderType, |
| 54 | + TemplateID: "session-reminder-template", |
| 55 | + NotificationID: fmt.Sprintf("reminder-%s-%s", reminderType, sessionID), |
| 56 | + } |
| 57 | + |
| 58 | + // Use the common scheduling method with the reminder message body |
| 59 | + return s.createOrUpdateScheduleWithPayload(sessionID, scheduleTime, namePrefix, s.Config.SQSSessionRemindersQueueARN, messageBody, logContext) |
| 60 | +} |
| 61 | + |
| 62 | +// createOrUpdateScheduleWithPayload is a generic method that handles the scheduling logic with any payload |
| 63 | +func (s *Service) createOrUpdateScheduleWithPayload(sessionID string, scheduleTime time.Time, namePrefix, queueArn string, payload interface{}, logContext string) error { |
36 | 64 | scheduleName := namePrefix + sessionID |
37 | 65 | log.Printf("Creating/updating schedule '%s' at time: %s", scheduleName, scheduleTime) |
38 | 66 |
|
39 | 67 | // Format time for EventBridge Scheduler expression: at(YYYY-MM-DDTHH:mm:ss) |
40 | 68 | scheduleExpression := fmt.Sprintf("at(%s)", scheduleTime.UTC().Format("2006-01-02T15:04:05")) |
41 | 69 |
|
42 | | - // Use the SQSMessageBody struct to ensure consistency between producer and consumer |
43 | | - messageBody := models.SQSMessageBody{ |
44 | | - SessionID: sessionID, |
45 | | - Action: action, |
46 | | - } |
47 | | - |
48 | | - // Marshal the struct to JSON |
49 | | - inputJSON, err := json.Marshal(messageBody) |
| 70 | + // Marshal the payload to JSON |
| 71 | + inputJSON, err := json.Marshal(payload) |
50 | 72 | if err != nil { |
51 | 73 | log.Printf("Error marshaling message body to JSON: %v", err) |
52 | 74 | return err |
|
0 commit comments