所以我对sidekiq和activejob很新,我知道我不需要在创建帐户时发送邮件的背景工作,但我真的想了解这一切是如何工作的,最好的学习方法是做...
我设置了以下作业,邮件程序和控制器操作,但是当我创建一个新帐户时它失败并显示错误:(为简洁起见,我已发布整个堆栈)
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'
我的工作,邮件和控制器操作在这里:
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
我已经坚持了一段时间,我确信这是我无法看到的。
预先感谢您的协助!如果您需要更多信息,请与我们联系。
答案 0 :(得分:4)
应用程序在某种程度上过快了。在将帐户持久保存到数据库之前,正在调用欢迎电子邮件作业。您应该将作业移动到模型中的after_create_commit
挂钩。即:
在/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
编辑:如果你想让它成为一个后台工作,只要你的sidekiq配置正确(你有一个'邮件程序'队列),你可以打电话给deliver_later
而不是deliver_now
那个应该做的伎俩