Question: Rails 5 Active Job and Sidekiq not sending mailer

Question

Rails 5 Active Job and Sidekiq not sending mailer

Answers 1
Added at 2016-11-19 06:11
Tags
Question

So I am very new to sidekiq and activejob, I know that i dont need to background job a mailer that being sent on an account creation, however I really want to understand how this all works, and the best way to learn is to do...

I have the following Job, Mailer and Controller action set up, however when I create a new account It fails with tis error: (I have posted the entire stack for brevity)

e_owner: processed outbound mail in 1.3ms
[ActiveJob] [WelcomeEmailJob] [18ba16e9-e3d9-4dd1-b6f4-0a435449c68c] Performed WelcomeEmailJob from Sidekiq(default) in 26.39ms
Completed 404 Not Found in 25051ms (ActiveRecord: 91.9ms)



ActiveRecord::RecordNotFound - Couldn't find Account with 'id'=:
  activerecord (5.0.0.1) lib/active_record/core.rb:173:in `find'
  app/mailers/welcome_notification_mailer.rb:4:in `welcome_owner'
  actionpack (5.0.0.1) lib/abstract_controller/base.rb:188:in `process_action'
  actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
  actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:in `process'
  actionmailer (5.0.0.1) lib/action_mailer/rescuable.rb:23:in `block in process'
  actionmailer (5.0.0.1) lib/action_mailer/rescuable.rb:15:in `handle_exceptions'
  actionmailer (5.0.0.1) lib/action_mailer/rescuable.rb:22:in `process'
  actionview (5.0.0.1) lib/action_view/rendering.rb:30:in `process'
  actionmailer (5.0.0.1) lib/action_mailer/base.rb:595:in `block in process'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionmailer (5.0.0.1) lib/action_mailer/base.rb:594:in `process'
  actionmailer (5.0.0.1) lib/action_mailer/message_delivery.rb:105:in `block in processed_mailer'
  actionmailer (5.0.0.1) lib/action_mailer/message_delivery.rb:104:in `tap'
  actionmailer (5.0.0.1) lib/action_mailer/message_delivery.rb:104:in `processed_mailer'
  actionmailer (5.0.0.1) lib/action_mailer/message_delivery.rb:95:in `deliver_now'
  app/jobs/welcome_email_job.rb:6:in `perform'
  activejob (5.0.0.1) lib/active_job/execution.rb:34:in `block in perform_now'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in `call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in halting'
  i18n (0.7.0) lib/i18n.rb:257:in `with_locale'
  activejob (5.0.0.1) lib/active_job/translation.rb:7:in `block (2 levels) in <module:Translation>'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:391:in `instance_exec'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:391:in `block in make_lambda'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:285:in `block in halting'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:447:in `block in around'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:286:in `block (2 levels) in halting'
  activejob (5.0.0.1) lib/active_job/logging.rb:24:in `block (4 levels) in <module:Logging>'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  activejob (5.0.0.1) lib/active_job/logging.rb:23:in `block (3 levels) in <module:Logging>'
  activejob (5.0.0.1) lib/active_job/logging.rb:44:in `block in tag_logger'
  activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
  activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
  activejob (5.0.0.1) lib/active_job/logging.rb:44:in `tag_logger'
  activejob (5.0.0.1) lib/active_job/logging.rb:20:in `block (2 levels) in <module:Logging>'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:391:in `instance_exec'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:391:in `block in make_lambda'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:285:in `block in halting'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:447:in `block in around'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_perform_callbacks'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
  activejob (5.0.0.1) lib/active_job/execution.rb:33:in `perform_now'
  activejob (5.0.0.1) lib/active_job/execution.rb:16:in `perform_now'
  app/controllers/accounts_controller.rb:22:in `create'
  actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
  actionpack (5.0.0.1) lib/abstract_controller/base.rb:188:in `process_action'
  actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
  actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in `call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
  actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
  actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (5.0.0.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
  activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:in `process'
  actionview (5.0.0.1) lib/action_view/rendering.rb:30:in `process'
  actionpack (5.0.0.1) lib/action_controller/metal.rb:190:in `dispatch'
  actionpack (5.0.0.1) lib/action_controller/metal.rb:262:in `dispatch'
  actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
  actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
  actionpack (5.0.0.1) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
  actionpack (5.0.0.1) lib/action_dispatch/routing/mapper.rb:46:in `serve'
  actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
  actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `each'
  actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
  actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
  apartment (1.2.0) lib/apartment/elevators/generic.rb:24:in `call'
  apartment (1.2.0) lib/apartment/reloader.rb:18:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `catch'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (2.0.1) lib/rack/etag.rb:25:in `call'
  rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
  rack (2.0.1) lib/rack/head.rb:12:in `call'
  rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
  rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
  activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
  activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
  airbrake (5.6.1) lib/airbrake/rack/middleware.rb:23:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
  web-console (3.4.0) lib/web_console/middleware.rb:135:in `call_app'
  web-console (3.4.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (3.4.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (3.4.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
  railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
  railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
  activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
  activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
  railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
  sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
  rack (2.0.1) lib/rack/method_override.rb:22:in `call'
  rack (2.0.1) lib/rack/runtime.rb:22:in `call'
  activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
  actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
  rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
  railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
  puma (3.6.0) lib/puma/configuration.rb:225:in `call'
  puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
  puma (3.6.0) lib/puma/server.rb:415:in `process_client'
  puma (3.6.0) lib/puma/server.rb:275:in `block in run'
  puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

My Job, Mailer and Controller Actions are here:

    class WelcomeEmailJob < ApplicationJob
      queue_as :default

      def perform(account_id)
        @account = Account.find(account_id)
        WelcomeNotificationMailer.welcome_owner(@account).deliver_now
      end
    end

    class WelcomeNotificationMailer < ApplicationMailer

      def welcome_owner(account_id)
        account = Account.find(account)
        mail to: @account.owner.email, subject: 'Welcome to the PATROLVAULT family!'
      end

    end

class AccountsController < ApplicationController
      def create
        @account = Account.new(account_params)

        if @account.valid?
          Apartment::Tenant.create(@account.subdomain)
          Apartment::Tenant.switch!(@account.subdomain)
          @account.save

          WelcomeEmailJob.perform_now(@account.id)

          redirect_to login_url(subdomain: @account.subdomain), :flash => { notice: 'Account created successfully. Please check your email.' }
        end
      end
end

I have been stuck on this for some time and I am sure its something i just can't see.

Thanks in advance for your assistance! Please let me know if you require more info.

Answers
nr: #1 dodano: 2016-11-19 14:11

The app is going too fast, in a way. Welcome email job is being called before Account has been persisted to the db. You should move the job to an after_create_commit hook in your model. i.e.:

in your /app/models/account.rb

class Account < ApplicationRecord
  after_create_commit :send_welcome_email

  ...

  def send_welcome_email
    WelcomeNotificationMailer.welcome_owner(self).deliver_now
  end
end

Edit: if you want to make it a background job, as long as your sidekiq is configured correctly (you have a 'mailers' queue), you can call deliver_later instead of deliver_now and that should do the trick

Source Show
◀ Wstecz