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

ActionMailer::DeliveryJob from Rake problem: enqueued but nor performed, #212

Closed fones closed 6 years ago

fones commented 6 years ago

Rails 2.3.1 Rails 5.0.0.1

Got rake task that is fired at night to check subscriptions in my saas project.

When someone is not paying, we suspend his/her website and send an e-mail.

part of Rake task

    # Suspend all websites
    organization.websites.map(&:suspend)
    # Send emails
    organization.admins.each do |admin|
      WebsitesMailer.notrenewed_website_suspend(admin, id).deliver_later
    end

website#suspend

  def suspend
    update(status: STATUS_SUSPENDED)
    SuspendWebsiteJob.perform_later(code)
  end

And now I get this in my log:

[ActiveJob] Enqueued SuspendWebsiteJob (Job ID: 7603f3c3-8fdf-4402-aff8-062ef6bb39bc) to SuckerPunch(default) with arguments: "1"
[ActiveJob] [SuspendWebsiteJob] [7603f3c3-8fdf-4402-aff8-062ef6bb39bc] Performing SuspendWebsiteJob from SuckerPunch(default) with arguments: "1"
[ActiveJob] [SuspendWebsiteJob] [7603f3c3-8fdf-4402-aff8-062ef6bb39bc] Performed SuspendWebsiteJob from SuckerPunch(default) in 5056.09ms
[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 0b3f4586-4aab-4391-a812-3cdb362cb1e9) to SuckerPunch(mailers) with arguments: "WebsitesMailer", "notrenewed_website_suspend", "deliver_now", #<GlobalID:0x007f83cfa29f18 @uri=#<URI::GID gid://saas/User/3>>, 14

Firt job SuspendWebsiteJob is working properly, bot second WebsitesMailer.notrenewed_website_suspend(admin, id).deliver_later is enqueued bot not proccessed.

I even change the default queue name for ActionMailer from mailers to default

config.action_mailer.deliver_later_queue_name = 'default'

but got the same results:

[ActiveJob] Enqueued SuspendWebsiteJob (Job ID: 54491bf7-11ad-4991-a19b-3f8088385247) to SuckerPunch(default) with arguments: "1"
[ActiveJob] [SuspendWebsiteJob] [54491bf7-11ad-4991-a19b-3f8088385247] Performing SuspendWebsiteJob from SuckerPunch(default) with arguments: "1"
[ActiveJob] [SuspendWebsiteJob] [54491bf7-11ad-4991-a19b-3f8088385247] Performed SuspendWebsiteJob from SuckerPunch(default) in 5193.54ms
[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: b76cd7fd-4f2f-414e-be61-b903c4d773e1) to SuckerPunch(default) with arguments: "WebsitesMailer", "notrenewed_website_suspend", "deliver_now", #<GlobalID:0x007fdca19c1c58 @uri=#<URI::GID gid://saas/User/3>>, 14

For now I am using delivery_now to workaround this. In rake task its does not matter (now or later).

When I start this from controller it works OK, got problemy only when doing this from Rake task.

brandonhilkert commented 6 years ago

Sounds similar to https://github.com/brandonhilkert/sucker_punch/issues/204#issuecomment-328641602.

The process of a rake task shuts down after the tasks are completed, so there's no leftover process for the Sucker Punch workers to be running. When executed from a controller, the controller is still alive allowing the job to finish. If using from rake, it's best to run them synchronously to avoid this.