package processing import ( "bytes" "context" "crypto/sha256" "encoding/json" "errors" "fmt" "io" "log/slog" "net" "os" "os/exec" "sync" "time" "github.com/minio/minio-go/v7" "messenger/internal/attachment" "messenger/internal/broker" "messenger/internal/imaging" "messenger/internal/pipeline" "messenger/internal/platform/config" ) var errSourceMismatch = errors.New("original differs from accepted metadata") type Worker struct { Store *pipeline.Store Blobs attachment.Blobs Config config.Jobs Log *slog.Logger // An experiment may pause an individual attempt at an exact durability boundary. Checkpoint func(context.Context, pipeline.Job, string) error } func (w *Worker) Run(ctx context.Context) { var wg sync.WaitGroup for range w.Config.Concurrency { wg.Go(func() { for ctx.Err() == nil { err := w.consume(ctx) if ctx.Err() == nil { w.Log.Warn("consumer reconnecting", "error", err) pause(ctx, time.Second) } } }) } wg.Wait() } func (w *Worker) consume(ctx context.Context) error { c, err := broker.NewConsumer(ctx, w.Config.URL) if err != nil { return err } defer c.Close() for d := range c.Deliveries { var j pipeline.Job dec := json.NewDecoder(bytes.NewReader(d.Body)) dec.DisallowUnknownFields() if err := dec.Decode(&j); err != nil || !j.Valid() || dec.Decode(new(any)) != io.EOF { w.Log.Warn("discarding invalid attachment envelope") if err := d.Reject(false); err != nil { return err } continue } if err := w.process(ctx, j); err != nil { return err } if err := d.Ack(false); err != nil { return err } if err := w.checkpoint(ctx, j, "acked"); err != nil { return err } } return errors.New("broker delivery stream closed") } func (w *Worker) checkpoint(ctx context.Context, j pipeline.Job, phase string) error { if w.Checkpoint != nil { return w.Checkpoint(ctx, j, phase) } return nil } func (w *Worker) process(ctx context.Context, j pipeline.Job) error { c, err := w.Store.Claim(ctx, j, w.Config.Timeout+5*time.Second) if err != nil { return err } if c == nil { return w.checkpoint(ctx, j, "duplicate") } if err := w.checkpoint(ctx, j, "claimed"); err != nil { return err } jobCtx, cancel := context.WithTimeout(ctx, w.Config.Timeout) defer cancel() variants, failure := w.writeObjects(jobCtx, c) if failure != nil { if ctx.Err() != nil { return ctx.Err() } _, err = w.Store.Fail(ctx, c, *failure, retryPolicy(w.Config)) return err } if err := w.checkpoint(ctx, j, "objects"); err != nil { return err } saved, err := w.Store.Complete(ctx, c, variants) if err != nil || !saved { return err } return w.checkpoint(ctx, j, "committed") } func readOriginal(ctx context.Context, blobs attachment.Blobs, c *pipeline.Claim) ([]byte, error) { if c.SizeBytes < 1 || c.SizeBytes > imaging.MaxUploadBytes { return nil, errSourceMismatch } r, err := blobs.Open(ctx, c.SourceKey) if err != nil { return nil, err } data, err := io.ReadAll(io.LimitReader(r, c.SizeBytes+1)) closeErr := r.Close() if err != nil { return nil, err } if closeErr != nil { return nil, closeErr } h := sha256.Sum256(data) if int64(len(data)) != c.SizeBytes || !bytes.Equal(h[:], c.Hash) { return nil, errSourceMismatch } return data, nil } // Log only classified reasons. Storage and codec errors may contain URLs, // object keys, filenames or input-derived diagnostics. func (w *Worker) attemptFailure(c *pipeline.Claim, stage string, err error) *pipeline.Failure { failure := &pipeline.Failure{Code: "processing_failed"} reason := "operation_failed" var network net.Error var exit *exec.ExitError var storage minio.ErrorResponse switch { case errors.Is(err, imaging.ErrInvalidImage): failure.Code, failure.Permanent = "invalid_image", true reason = failure.Code case errors.Is(err, errSourceMismatch): failure.Code, failure.Permanent = "source_mismatch", true reason = failure.Code case errors.Is(err, attachment.ErrObjectMissing): failure.Code = "source_missing" reason = failure.Code case errors.Is(err, context.DeadlineExceeded): reason = "deadline_exceeded" case errors.Is(err, context.Canceled): reason = "canceled" case errors.As(err, &exit): reason = fmt.Sprintf("codec_exit_%d", exit.ExitCode()) case errors.Is(err, exec.ErrNotFound): reason = "codec_not_found" case errors.As(err, &storage): reason = fmt.Sprintf("s3_http_%d", storage.StatusCode) case errors.As(err, &network): reason = "network_error" if network.Timeout() { reason = "network_timeout" } case errors.Is(err, os.ErrPermission): reason = "permission_denied" } w.Log.Warn("attachment attempt failed", "job_id", c.JobID, "attempt", c.Attempt, "error_code", failure.Code, "stage", stage, "reason", reason) return failure } func (w *Worker) writeObjects(ctx context.Context, c *pipeline.Claim) ([]pipeline.Variant, *pipeline.Failure) { data, err := readOriginal(ctx, w.Blobs, c) if err != nil { return nil, w.attemptFailure(c, "read_original", err) } encoded, err := imaging.Render(ctx, data, c.PipelineVersion) if err != nil { return nil, w.attemptFailure(c, "render", err) } key := attachment.BlobKey(c.Hash) if c.SourceKey != key { if err := w.Blobs.Put(ctx, key, bytes.NewReader(data), c.SizeBytes, c.MIMEType); err != nil { return nil, w.attemptFailure(c, "write_original", err) } } var variants []pipeline.Variant for _, v := range encoded { hash := sha256.Sum256(v.Data) key := pipeline.VariantKey(c.AttachmentID, c.PipelineVersion, v.Name, hash[:]) n := int64(len(v.Data)) if err := w.Blobs.Put(ctx, key, bytes.NewReader(v.Data), n, v.MIMEType); err != nil { return nil, w.attemptFailure(c, "write_variant", err) } variants = append(variants, pipeline.Variant{ Name: v.Name, Key: key, MIMEType: v.MIMEType, Width: v.Width, Height: v.Height, SizeBytes: n, SHA256: hash[:], }) } return variants, nil }