refactor(worker): split job handling and retries, prepare variant collection
This commit is contained in:
+16
-16
@@ -66,21 +66,21 @@ func thumbnail(r io.ReadSeeker, maxPixels int) ([]byte, int, int, error) {
|
||||
return out.Bytes(), width, height, nil
|
||||
}
|
||||
|
||||
func (w *Worker) process(ctx context.Context, row db.Attachment) (db.CreateAttachmentVariantParams, error) {
|
||||
func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAttachmentVariantParams, error) {
|
||||
if row.StagingKey == nil {
|
||||
return db.CreateAttachmentVariantParams{}, errInvalidSource
|
||||
return nil, errInvalidSource
|
||||
}
|
||||
|
||||
source, err := w.s3.Get(ctx, *row.StagingKey)
|
||||
if err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer source.Body.Close()
|
||||
|
||||
tmp, err := os.CreateTemp("", "attachment-job-*")
|
||||
if err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@@ -91,54 +91,54 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) (db.CreateAttac
|
||||
hash := sha256.New()
|
||||
size, err := io.Copy(io.MultiWriter(tmp, hash), io.LimitReader(source.Body, int64(w.maxBytes)+1))
|
||||
if err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if size != int64(row.SizeBytes) ||
|
||||
hex.EncodeToString(hash.Sum(nil)) != row.Sha256 {
|
||||
return db.CreateAttachmentVariantParams{}, errInvalidSource
|
||||
return nil, errInvalidSource
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
select {
|
||||
case w.decodeSlot <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
return db.CreateAttachmentVariantParams{}, ctx.Err()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
<-w.decodeSlot
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, width, height, err := thumbnail(tmp, w.maxPixels)
|
||||
<-w.decodeSlot
|
||||
if err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := w.s3.Put(ctx, row.S3Key, tmp, size, string(row.ContentType)); err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/thumbnail"
|
||||
if err := w.s3.Put(ctx, key, bytes.NewReader(data), int64(len(data)), "image/jpeg"); err != nil {
|
||||
return db.CreateAttachmentVariantParams{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(data)
|
||||
return db.CreateAttachmentVariantParams{
|
||||
return []db.CreateAttachmentVariantParams{{
|
||||
AttachmentID: row.ID,
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Variant: db.AttachmentVariantNameThumbnail,
|
||||
@@ -148,5 +148,5 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) (db.CreateAttac
|
||||
Width: width,
|
||||
Height: height,
|
||||
Sha256: hex.EncodeToString(sum[:]),
|
||||
}, nil
|
||||
}}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user