-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor file upader to process derivatives in separate jobs
This avoids an issue where if e.g. an image derivative is too large for the serverless image handler, only that single derivative will fail to process. This issue was happening when uploading large PNG files. Now, thumb, small, and medium derivatives should still continue to process, while large sizes may fail. Shrine: Creating derivatives concurrently https://shrinerb.com/docs/processing#c-creating-derivatives-concurrently Large files cause "body size too long" error aws-solutions/serverless-image-handler#35
- Loading branch information
1 parent
7433ffe
commit 26a5393
Showing
3 changed files
with
36 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
class AttachmentDerivativeJob < ApplicationJob | ||
# https://shrinerb.com/docs/plugins/backgrounding | ||
|
||
def perform(attacher_class, record_class, record_id, name, file_data) | ||
def perform(attacher_class, record_class, record_id, name, file_data, derivative_name) | ||
attacher_class = Object.const_get(attacher_class) | ||
record = Object.const_get(record_class).find(record_id) # if using Active Record | ||
|
||
attacher = attacher_class.retrieve(model: record, name: name, file: file_data) | ||
attacher.create_derivatives # calls derivatives processor | ||
attacher.atomic_promote | ||
attacher.create_derivatives(:image, name: derivative_name) # calls derivatives processor | ||
attacher.atomic_persist do |reloaded_attacher| | ||
# make sure we don't override derivatives created in other jobs | ||
attacher.merge_derivatives(reloaded_attacher.derivatives) | ||
end | ||
rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound | ||
# attachment has changed or the record has been deleted, nothing to do | ||
attacher.derivatives[derivative_name].delete # delete now orphaned derivative | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters