PaperClip check existence before process

146 views Asked by At

I'm trying to skip PaperClip process if my music has been deleted.

Basically when I upload a music, I want to destroy every single piece of this uploaded file if something went wrong. This operation is delayed and done by ffmpeg. Another task is delayed to attached picture to my music.

Unfortunately, PaperClip process (to attach the picture) has already been delayed when I destroy bad musics. This means when the process starts the source doesn't exist anymore. This blocks my delayed_jobs and I have to destroy it manually.

I'm using Delayed_paperclip to create those delayed jobs.

How can I check if the source exists before processing?

1

There are 1 answers

3
Dan Myasnikov On

The issue you are dealing with is called race condition: https://en.wikipedia.org/wiki/Race_condition

Assuming, you are donig something like: @my_instance.delay.method

You might want to pass the id of the object you are attaching an image to it to a perform method

def perform(obj_id)
  begin
    MyObj.find(obj_id)
  rescue
    # => do your stuff here
  ensure
    # => ensure your stuff here
  end
end

Example: https://github.com/collectiveidea/delayed_job/#custom-jobs