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:
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
"tm/cmd/worker/workers"
|
||||
"tm/internal/notice"
|
||||
notificationDomain "tm/internal/notification"
|
||||
"tm/internal/tender"
|
||||
ai_summarizer "tm/pkg/ai_summarizer"
|
||||
"tm/pkg/config"
|
||||
@@ -87,10 +88,12 @@ func InitWorker(config Config, mongoManager *mongo.ConnectionManager, appLogger
|
||||
"notice_fetch_error_backoff": config.Worker.NoticeFetchErrorBackoff.String(),
|
||||
"translation_enabled": config.Worker.TranslationEnabled,
|
||||
"translation_interval": config.Worker.TranslationInterval,
|
||||
"notification_interval": config.Worker.NotificationInterval,
|
||||
})
|
||||
// Initialize repositories
|
||||
noticeRepo := notice.NewRepository(mongoManager, appLogger)
|
||||
tenderRepo := tender.NewRepository(mongoManager, appLogger)
|
||||
notificationRepo := notificationDomain.NewRepository(mongoManager, appLogger)
|
||||
|
||||
// Create a single shared cron scheduler for all recurring jobs
|
||||
scheduler := schedule.NewCronScheduler(appLogger, mongoManager, noticeRepo)
|
||||
@@ -159,6 +162,26 @@ func InitWorker(config Config, mongoManager *mongo.ConnectionManager, appLogger
|
||||
appLogger.Warn("AI summarizer client not available, tender translation worker is disabled", map[string]interface{}{})
|
||||
}
|
||||
|
||||
notificationInterval := config.Worker.NotificationInterval
|
||||
if notificationInterval == "" {
|
||||
notificationInterval = "0 * * * * *"
|
||||
appLogger.Warn("WORKER_NOTIFICATION_INTERVAL not set, using default schedule", map[string]interface{}{
|
||||
"interval": notificationInterval,
|
||||
})
|
||||
}
|
||||
|
||||
scheduler.AddJob(schedule.Job{
|
||||
Name: "Scheduled Notification Worker Job",
|
||||
Func: func() {
|
||||
worker := workers.NewNotificationWorker(notificationRepo, appLogger)
|
||||
worker.Run()
|
||||
},
|
||||
Expr: notificationInterval,
|
||||
})
|
||||
appLogger.Info("Scheduled notification delivery worker", map[string]interface{}{
|
||||
"interval": notificationInterval,
|
||||
})
|
||||
|
||||
// Kick off one notice-processing pass without blocking startup (cron continues on schedule)
|
||||
go func() {
|
||||
w := workers.NewNoticeWorker(
|
||||
|
||||
@@ -33,6 +33,8 @@ type WorkerConfig struct {
|
||||
// TranslationEnabled schedules the automatic batch translation cron job on the worker.
|
||||
// On-demand translation (admin/public tender endpoints and AI pipeline routes on web) is unaffected.
|
||||
TranslationEnabled bool `env:"WORKER_TRANSLATION_ENABLED" envDefault:"true"`
|
||||
// NotificationInterval schedules promotion of due scheduled notifications from pending to sent.
|
||||
NotificationInterval string `env:"WORKER_NOTIFICATION_INTERVAL" envDefault:"0 * * * * *"`
|
||||
}
|
||||
|
||||
// AISummarizerConfig holds configuration for the external AI summarizer service.
|
||||
|
||||
@@ -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