brandonhilkert / sucker_punch

Sucker Punch is a Ruby asynchronous processing library using concurrent-ruby, heavily influenced by Sidekiq and girl_friday.
MIT License
2.65k stars 114 forks source link

job order issues? getting "ActiveRecord::RecordNotFound not found" #217

Closed jtoy closed 6 years ago

jtoy commented 6 years ago

I am using sucker punch with ActiveRecord and rails. I use sucker punch in an after_create callback of a rails model:

  after_create :send_hooks
  def send_hooks
    WebhookJob.perform_async(klass:"Transcript",id:id,name:"transcript_created")
    WebhookJob.perform_async(klass:"Transcript",id:self.id,name:"item_and_transcript_created")
    TriggerJob.perform_async(id)
    true
  end

In each of those jobs, I call Transcript.find(id) . And every time send_hooks is called, I will get one of those jobs randomly failing with :

Sucker Punch job error for class: 'WebhookJob' args: [{:klass=>"Transcript", :id=>244, :name=>"transcript_created"}]
ActiveRecord::RecordNotFound Couldn't find Transcript with 'id'=244

The item exists for sure, because its happening in an after_create callback of rails. How is it possible for this kind of error to appear? I'm wondering if sucker punch jobs should not be called multiple times in a row? I also do have some of these jobs calling jobs from inside their jobs. Can sucker punch be used like that?

brandonhilkert commented 6 years ago

Try after_commit instead of after_create. Sounds like the job is running before the transaction completes.