74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
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) {
|
|
for ctx.Err() == nil {
|
|
err := dispatchSession(ctx, store, cfg, barrier)
|
|
if ctx.Err() == nil {
|
|
log.Warn("attachment dispatcher will retry", "error", err)
|
|
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()
|
|
}
|