dragonfly
dragonfly copied to clipboard
Using after_assign processor with sidekiq
Hey @markevans I was trying to implement this for some time now, my idea was to use the after_assign
processor to optimize image, I've already done that. now I need to move it to the back ground.
here's a sample of the code:
class Image < ActiveRecord::Base
dragonfly_accessor :asset do
after_assign :optimize_image
end
private
def optimize_image
self.asset = asset.image_optim
end
end
I've also refactored it to be like:
class Image < ActiveRecord::Base
dragonfly_accessor :asset do
after_assign {|img| self.asset = img.image_optim }
end
end
and the processor code :
Dragonfly.app.configure do
plugin :imagemagick
processor :image_optim do |content, *args|
optimized = image_optim.optimize_image_data(content.data)
content.update(optimized) if optimized
end
.
.
.
so, using sidekiq, is it possible to move such a processor to the background ? if it's possible, any hints about it ?
I wouldn't use after_assign
for doing it in the background. You should run optimize_image
from the background job (however that gets scheduled), e.g. something like
# in Image
after_save :schedule_optimize_image
def schedule_optimize_image
MyWorker.perform_async(id)
end
and do the image optimization inside the worker
do you mean processing images after the upload ? even though the images uploaded to S3 ? I guess S3 doesn't offer the processing over it's buckets.
How can I pass the on-the-fly proceessing in background? I mean by this: after successfully saved the original image in storage, I will never use it for showing in clients interface, but I am using, for example,
thumb("100х100")` method. For the first call it will make all the work on thumbnail generation in main process (as I can assume) and then saves the generated image in cache. So the output of page which contains new image can be slow for user. But what if I want to initialize the first thumbnail generation immediately after image has been saved, so all needed cache will be pre-made in background?