Enhance notification delivery system with scheduled processing
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced a new `NotificationWorker` to promote due scheduled notifications from pending to sent, improving notification management. - Added `NotificationInterval` configuration to schedule the notification delivery worker, with a default value for flexibility. - Implemented `MarkDueScheduledAsSent` method in the notification repository to update the status of notifications based on their delivery time. - Updated the notification service to process due scheduled notifications during relevant operations, ensuring timely delivery. This update enhances the notification system by automating the delivery of scheduled notifications, improving user engagement and operational efficiency.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package workers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"tm/internal/notification"
|
||||
"tm/pkg/logger"
|
||||
)
|
||||
|
||||
// NotificationWorker promotes due scheduled notifications from pending to sent.
|
||||
type NotificationWorker struct {
|
||||
Repository notification.Repository
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
// NewNotificationWorker creates a notification delivery worker.
|
||||
func NewNotificationWorker(repository notification.Repository, logger logger.Logger) *NotificationWorker {
|
||||
return &NotificationWorker{
|
||||
Repository: repository,
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Run marks pending scheduled notifications as sent once their delivery time has passed.
|
||||
func (w *NotificationWorker) Run() {
|
||||
count, err := w.Repository.MarkDueScheduledAsSent(context.Background(), time.Now().Unix())
|
||||
if err != nil {
|
||||
w.Logger.Error("Scheduled notification worker failed", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
w.Logger.Info("Scheduled notification worker completed", map[string]interface{}{
|
||||
"count": count,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user