dragonfly icon indicating copy to clipboard operation
dragonfly copied to clipboard

Using after_assign processor with sidekiq

Open ahmgeek opened this issue 9 years ago • 3 comments

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 ?

ahmgeek avatar Jan 29 '16 18:01 ahmgeek

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

markevans avatar Jan 29 '16 19:01 markevans

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.

ahmgeek avatar Jan 29 '16 19:01 ahmgeek

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?

sintro avatar May 30 '16 22:05 sintro