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

Async Not Performing On Model OnCreate Callback #79

Closed cheynewallace closed 9 years ago

cheynewallace commented 9 years ago

I noticed today that I had a job that was supposed to fire on the "on create" call back for one of my models. It appears to work in development, but once I pushed to production (Heroku) it did not.

Tested it over a dozen times, and it does not work. So I've had to fall back to manually calling the Async action inline from the controller.

I have dozens of sucker_punch triggered jobs, they all work as normal. Only this one was triggered from an on create callback though.

Using v 1.0.2

brandonhilkert commented 9 years ago

Can you post the code you were using to create the callback?

cheynewallace commented 9 years ago

On the model

after_create :email_recipient
def email_recipient
  EmailJob.new.async.perform('request_created', self.note)
end

on the job

class EmailJob
  include SuckerPunch::Job
  workers 4

  def perform(type, note)
    case type   
    when 'request_created'
      NoteMailer.request_created(note).deliver
    when 'request_completed'
      NoteMailer.request_completed(note).deliver
    end
  end
end
brandonhilkert commented 9 years ago

when you loading the inline test library, does the job run in tests?


_The Build a Ruby Gem guide is available! http://brandonhilkert.com/books/build-a-ruby-gem/?utm_source=gmail-sig&utm_medium=email&utm_campaign=gem-config_

http://brandonhilkert.com

On Wed, Oct 15, 2014 at 3:58 PM, Cheyne notifications@github.com wrote:

On the model

after_create :email_recipientdef email_recipient EmailJob.new.async.perform('request_created', self.note)end

on the job

class EmailJob include SuckerPunch::Job workers 4

def perform(type, note) case type when 'request_created' NoteMailer.request_created(note).deliver when 'request_completed' NoteMailer.request_completed(note).deliver end endend

— Reply to this email directly or view it on GitHub https://github.com/brandonhilkert/sucker_punch/issues/79#issuecomment-59266060 .

cheynewallace commented 9 years ago

Sorry, when I said inline, I meant manually calling the async.perform method instead of letting the callback trigger it.

I'm still using an async method, but now my controller method looks something like this

request = Request.build(params,@user)
request.save
request.email_recipient

That last line ideally should just fire from the callback instead of needing to be explicitly called.

brandonhilkert commented 9 years ago

Yeah i got what you're saying. When I said "inline", i was referring to this: https://github.com/brandonhilkert/sucker_punch#testing

Can you run your tests with that and see if it's triggered?

cheynewallace commented 9 years ago

Sorry, having trouble testing this. I spun up a staging machine, the same as prod , and the problem appears to happen randomly. Example, I created the same task 5 times and twice it failed to trigger the email. (this is when using async call back from a rails model)

The problem does not appear to happen when directly calling the method. I'm not really sure if this a rails issue or a sucker_punch issue at the moment

I'm watching my out going mail logs too .. So it's definitely not being triggered. I thought it may have been a mail filter issue, but its not.

cheynewallace commented 9 years ago

This looks like more of a race condition to do with rails call backs the more I step through this.. I dont think sucker_punch is the cause of the issue.

Feel free to close this issue

Thanks for your help

brandonhilkert commented 9 years ago

Try after_commit instead of after_create

re: https://github.com/mperham/sidekiq/wiki/Problems-and-Troubleshooting#cannot-find-modelname-with-id12345

cheynewallace commented 9 years ago

Great, thanks i'll try that out.