package processing import ( "context" "encoding/json" "log/slog" "time" "messenger/internal/broker" "messenger/internal/pipeline" "messenger/internal/platform/config" ) func retryPolicy(cfg config.Jobs) pipeline.RetryPolicy { return pipeline.RetryPolicy{MaxAttempts: cfg.MaxAttempts, BaseDelay: time.Second, MaxDelay: time.Minute} } func pause(ctx context.Context, d time.Duration) { timer := time.NewTimer(d) defer timer.Stop() select { case <-ctx.Done(): case <-timer.C: } } func Dispatch(ctx context.Context, log *slog.Logger, store *pipeline.Store, cfg config.Jobs, barrier func(context.Context, pipeline.Job, string) error) { var p *broker.Publisher defer func() { if p != nil { p.Close() } }() for ctx.Err() == nil { _, err := store.RecoverOne(ctx, retryPolicy(cfg)) if err == nil && p == nil { p, err = broker.NewPublisher(ctx, cfg.URL) } if err == nil { var sent bool sent, err = store.PublishOne(ctx, func(ctx context.Context, j pipeline.Job) error { body, err := json.Marshal(j) if err != nil { return err } if err := p.Publish(ctx, body); err != nil { return err } if barrier != nil { return barrier(ctx, j, "published") } return nil }) if err == nil { if !sent { pause(ctx, 250*time.Millisecond) } continue } } if p != nil { p.Close() p = nil } if ctx.Err() == nil { log.Warn("attachment dispatcher will retry", "error", err) } pause(ctx, time.Second) } }