package workers import ( "context" "time" "tm/pkg/ai_summarizer" "tm/pkg/logger" ) // AIPipelineDailyWorker triggers the Opplens AI pipeline daily run (sync + full pipeline on newly synced tenders). type AIPipelineDailyWorker struct { Logger logger.Logger AIClient *ai_summarizer.Client } // NewAIPipelineDailyWorker creates a worker that calls POST /pipeline/daily-run on the AI service. func NewAIPipelineDailyWorker(log logger.Logger, aiClient *ai_summarizer.Client) *AIPipelineDailyWorker { return &AIPipelineDailyWorker{ Logger: log, AIClient: aiClient, } } // Run starts the pipeline daily-run job. A 409 response means a run is already in progress and is returned as an error // so the daily job tracker does not mark today as completed before the run actually starts. func (w *AIPipelineDailyWorker) Run() error { if w.AIClient == nil { w.Logger.Warn("AI pipeline daily worker skipped: AI client is not configured", map[string]interface{}{}) return nil } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() w.Logger.Info("AI pipeline daily worker started", map[string]interface{}{}) resp, err := w.AIClient.PipelineDailyRun(ctx) if err != nil { w.Logger.Error("AI pipeline daily worker failed", map[string]interface{}{ "error": err.Error(), }) return err } w.Logger.Info("AI pipeline daily worker triggered successfully", map[string]interface{}{ "status": resp.Status, "report": resp.Report, }) return nil }