jrgifford/delayed_paperclip

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

Opened this issue · 6 comments

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.

The only related issue I see is this jstorimer#34

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

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

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)


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 #125 which was just merged

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?