package workers import ( "context" "errors" "time" "tm/pkg/ai_summarizer" "tm/pkg/logger" ) // AIPipelineAutoWorker triggers the Opplens AI pipeline auto run (sync + complete missing work). type AIPipelineAutoWorker struct { Logger logger.Logger AIClient *ai_summarizer.Client } // NewAIPipelineAutoWorker creates a worker that calls POST /pipeline/auto on the AI service. func NewAIPipelineAutoWorker(log logger.Logger, aiClient *ai_summarizer.Client) *AIPipelineAutoWorker { return &AIPipelineAutoWorker{ Logger: log, AIClient: aiClient, } } // Run starts the pipeline auto job. A 409 response means a run is already in progress and is not an error. func (w *AIPipelineAutoWorker) Run() error { if w.AIClient == nil { w.Logger.Warn("AI pipeline auto 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 auto worker started", map[string]interface{}{}) resp, err := w.AIClient.PipelineAuto(ctx) if err != nil { if errors.Is(err, ai_summarizer.ErrPipelineJobRunning) { w.Logger.Info("AI pipeline auto skipped: job already running", map[string]interface{}{}) return nil } w.Logger.Error("AI pipeline auto worker failed", map[string]interface{}{ "error": err.Error(), }) return err } w.Logger.Info("AI pipeline auto worker triggered successfully", map[string]interface{}{ "status": resp.Status, "report": resp.Report, }) return nil }