jrgifford / delayed_paperclip

Process your Paperclip attachments in the background with delayed_job or Resque.
http://www.jstorimer.com/ruby/2010/01/30/delayed-paperclip.html
MIT License
402 stars 155 forks source link

post_process hooks don't run when attachments are processed in background #120

Open mehulkar opened 9 years ago

mehulkar commented 9 years ago

I've added the following code to my attachment model:

class Attachment < ActiveRecord::Base
  has_attached_file :data
  before_post_process :mark_as_unprocessed
  after_post_process  :mark_as_processed
  process_in_background :data
private
  def mark_as_unprocessed
    assign_attributes(processed: false)
  end

  def mark_as_processed
    assign_attributes(processed: true)
  end
end

When I comment out the process_in_background directive, the attachment is successfully updated as processed: true, but when I process_in_background, it looks like the callbacks are not run. I'm not sure how to debug this.

mehulkar commented 9 years ago

The only related issue I see is this https://github.com/jstorimer/delayed_paperclip/issues/34

mehulkar commented 9 years ago

My current solution is to monkey patch paperclip's Attachment#assign method, but this makes me really uneasy

module Paperclip
  class Attachment
    alias_method :old_assign,     :assign

    def assign(uploaded_file)
      mark_processing(false)
      old_assign(uploaded_file)
      mark_processing(true) if post_processing
    end

    def mark_processing(bool)
      Rails.logger.debug("Marking Attachment#{instance.id}.processed as #{bool}")
      # this hackery is so we don't call save when a file is being assigned to a new record
      if instance.id.present?
        instance.update_attributes(processed: bool)
      else
        instance.assign_attributes(processed: bool)
      end
    end
  end
end
ScotterC commented 9 years ago

Hey @mehulkar can you add your paperclip, delayed_paperclip and rails version please?

mehulkar commented 9 years ago

Rails 4.0.4, Paperclip: 4.2.1, delayed_paperclip 2.8.0

$ rails --version
Rails 4.0.4

$ gem list paperclip

*** LOCAL GEMS ***

delayed_paperclip (2.8.0)
paperclip (4.2.1, 4.2.0)

$ cat Gemfile.lock| grep paperclip
    delayed_paperclip (2.8.0)
      paperclip (>= 3.3)
    paperclip (4.2.1)
  delayed_paperclip (= 2.8.0)
  paperclip (~> 4.1)
ScotterC commented 9 years ago

Hey @mehulkar I'm not exactly sure why your setting these before/post processed call backs. Was there an issue with the processing column that you were trying to fix? If so, it might have been fixed by https://github.com/jrgifford/delayed_paperclip/pull/125 which was just merged

mehulkar commented 9 years ago

A coworker had added a data_processing column before I got on the project, but it didn't seem to be doing anything, so I was forced to roll my own. I can try using the inbuilt version again. Does #125 merit a version bump to make it easier for me to get a locked down version, rather than point to master?