Разделите настройки команд worker и сохраняйте причину сбоя
This commit is contained in:
@@ -25,46 +25,49 @@ func pause(ctx context.Context, d time.Duration) {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
err := dispatchSession(ctx, store, cfg, barrier)
|
||||
if ctx.Err() == nil {
|
||||
log.Warn("attachment dispatcher will retry", "error", err)
|
||||
pause(ctx, time.Second)
|
||||
}
|
||||
pause(ctx, time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func dispatchSession(ctx context.Context, store *pipeline.Store, cfg config.Jobs, barrier func(context.Context, pipeline.Job, string) error) error {
|
||||
var publisher *broker.Publisher
|
||||
publish := func(ctx context.Context, j pipeline.Job) error {
|
||||
body, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := publisher.Publish(ctx, body); err != nil {
|
||||
return err
|
||||
}
|
||||
if barrier != nil {
|
||||
return barrier(ctx, j, "published")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for ctx.Err() == nil {
|
||||
if _, err := store.RecoverOne(ctx, retryPolicy(cfg)); err != nil {
|
||||
return err
|
||||
}
|
||||
if publisher == nil {
|
||||
var err error
|
||||
publisher, err = broker.NewPublisher(ctx, cfg.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer publisher.Close()
|
||||
}
|
||||
sent, err := store.PublishOne(ctx, publish)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !sent {
|
||||
pause(ctx, 250*time.Millisecond)
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user