Repository: heartcombo/devise

Issue #3550: ActiveJob Integration on Rails 4.2 - Couldn't find User
Created: 2015-04-03T16:49:13Z
Hello,
as mentioned on the readme the ActiveJob integration on Rails 4.2 is as easy as adding
def send_devise_notification(notification, *args)
  devise_mailer.send(notification, self, *args).deliver_later
end
to the User model.
The good things is: a confirmation mail for a new user gets enqueued to my Sidekiq queue. But it looks like the job will be kicked off before the actual user instance is persisted. So the Sidekiq job fails for the first time. On a later retry the job gets done. How can I ensure the user entity is persisted before we send any mails?
Backtrace:
sidekiq_1     | 2015-04-03T16:32:19.315Z 1 TID-oshefkll0 ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper JID-bb1b58a2b3543aca381d2068 INFO: start
sidekiq_1     | 2015-04-03T16:32:19.348Z 1 TID-oshefkll0 ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper JID-bb1b58a2b3543aca381d2068 INFO: fail: 0.033 sec
sidekiq_1     | 2015-04-03T16:32:19.349Z 1 TID-oshefkll0 WARN: {"retry"=>true, "queue"=>"mailers", "class"=>"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper", "args"=>[{"job_class"=>"ActionMailer::DeliveryJob", "job_id"=>"6a519d5a-d2f6-4b4f-9490-4aa06a6c451c", "queue_name"=>"mailers", "arguments"=>["UserMailer", "confirmation_instructions", "deliver_now", {"_aj_globalid"=>"gid://api/User/1"}, "4-c3GN3_g2iMMkgsKMnJ", {}]}], "jid"=>"bb1b58a2b3543aca381d2068", "enqueued_at"=>1428078739.304505, "error_message"=>"Error while trying to deserialize arguments: Couldn't find User with 'id'=1", "error_class"=>"ActiveJob::DeserializationError", "failed_at"=>1428078739.34841, "retry_count"=>0}
sidekiq_1     | 2015-04-03T16:32:19.349Z 1 TID-oshefkll0 WARN: Error while trying to deserialize arguments: Couldn't find User with 'id'=1
sidekiq_1     | 2015-04-03T16:32:19.349Z 1 TID-oshefkll0 WARN: /home/web/.gem/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:154:in `find'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/globalid-0.3.3/lib/global_id/locator.rb:126:in `locate'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/globalid-0.3.3/lib/global_id/locator.rb:17:in `locate'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/arguments.rb:88:in `deserialize_global_id'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/arguments.rb:74:in `deserialize_argument'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/arguments.rb:38:in `block in deserialize'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/arguments.rb:38:in `map'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/arguments.rb:38:in `deserialize'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/core.rb:85:in `deserialize_arguments'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/core.rb:75:in `deserialize_arguments_if_needed'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/execution.rb:30:in `perform_now'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/execution.rb:21:in `execute'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/queue_adapters/sidekiq_adapter.rb:40:in `perform'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/processor.rb:75:in `execute_job'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/processor.rb:52:in `block (2 levels) in process'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:127:in `call'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:127:in `block in invoke'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/server/retry_jobs.rb:74:in `call'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/logging.rb:24:in `with_context'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/server/logging.rb:7:in `call'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:132:in `call'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/middleware/chain.rb:132:in `invoke'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/processor.rb:51:in `block in process'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/processor.rb:98:in `stats'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/sidekiq-3.3.2/lib/sidekiq/processor.rb:50:in `process'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/calls.rb:26:in `public_send'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/calls.rb:26:in `dispatch'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/calls.rb:122:in `dispatch'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/cell.rb:60:in `block in invoke'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/cell.rb:71:in `block in task'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/actor.rb:357:in `block in task'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/tasks.rb:57:in `block in initialize'
sidekiq_1     | /home/web/.gem/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/tasks/task_fiber.rb:15:in `block in create'
Versions:

rails: 4.2.0
devise: 3.4.1
warden: 1.2.3

Duplicate issue #4062: Asynchronous email job cannot find user
Created: 2016-04-27T02:51:34Z
I get an exception of
ActiveJob::DeserializationError:  Error while trying to deserialize arguments: Couldn't find User with 'id'=xxx while trying to follow the README to let the devise send email asynchronously.
https://github.com/plataformatec/devise#activejob-integration
I think this is because send_on_create_confirmation_instructions is called after_create, and emails are sent before user is persisted.

Closing comment (2016-05-02T13:23:10Z): Duplicates #3550