Search code examples
ruby-on-railsrubyruby-on-rails-4activerecordsidekiq

Rails 4 Sidekiq cannot find the ActiveRecord Objects at all


I am using Rails 4.2.6 and sidekiq 4.2.2. I am getting an error always that says

2017-02-23T07:33:23.455Z 31886 TID-ovz89uiyg WARN: {"class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"ActionMailer::DeliveryJob","queue":"mailers","args":[{"job_class":"ActionMailer::DeliveryJob","job_id":"d59ec97f-b858-4f47-aefd-c9150836d7d2","queue_name":"mailers","arguments":["UserMailer","send_password","deliver_now",{"_aj_globalid":"gid://my-app/User/85"},"http://0.0.0.0:3000/auth/password/edit?config=default&reset_password_token=zBdjahtAX7w3BC3Si8Fz&redirect_url="],"locale":"en"}],"retry":true,"jid":"f5dfa96df15443fc7ed2f3ce","created_at":1487832182.7783082,"enqueued_at":1487835165.842009,"error_message":"Error while trying to deserialize arguments: Couldn't find User with 'id'=85","error_class":"ActiveJob::DeserializationError","failed_at":1487832182.8214011,"retry_count":7,"retried_at":1487835203.4498932}

I am getting the above error when sending the reset password email to the user.

I used this code for sending the mail in the background

UserMailer.send_password(self.id, reset_password_link).deliver_later

I tried the following:

UserMailer.send_password(self.id, reset_password_link).deliver_later(wait: 1.minutes)

And I also tried the after_commit callback as these document says:

https://github.com/plataformatec/devise/issues/3550 https://github.com/mperham/sidekiq/issues/322

And I am passing ids and not the object itself. Still I am getting the above error. I updated the sidekiq version to 4.2.9 restarted the sidekiq.

Still the same error. Inside sidekiq ActiveRecord Objects cannot be found. I tried with finding very old objects inside the sidekiq, by using the byebug. But sidekiq cannot find the ActiveRecord Objects.

I am trying this for 1 day and I created the workers (say 'SendResetPasswordWorker') instead of the delay_later method. Still the same error

{"class":"SendResetPasswordWorker","args":[90,"http://0.0.0.0:3000/auth/password/edit?config=default&reset_password_token=8xTR5enYdHDpuQg6cyPZ&redirect_url="],"retry":true,"queue":"default","jid":"7a8a831d837d3e4f3d75d5f7","created_at":1487842213.2409139,"enqueued_at":1487842213.240968,"error_message":"Couldn't find User with 'id'=90","error_class":"ActiveRecord::RecordNotFound","failed_at":1487842213.267404,"retry_count":0}2017-02-23T09:30:13.268Z 35119 TID-ow2w9e41c WARN: ActiveRecord::RecordNotFound: Couldn't find User with 'id'=90 2017-02-23T09:30:13.268Z 35119 TID-ow2w9e41c WARN: /Users/abhilash/.rbenv/versions/2.2.1/gemsets/gauge-slcsl/gems/activerecord-4.2.6/lib/active_record/core.rb:155:in `find' /Users/abhilash/my-app/components/auth/app/workers/

I also tested it for another worker 'UserAgentWorker', another scenario but still the same error.

Anyone has any thoughts about this error? If yes please share.


Solution

  • Finally I found the answer. I am using Apartment Gem for multi tenancy. Inside sidekiq the default schema goes back to 'public' instead of my current schema in postgres. So I have to switch back to the current tenant schema each time when sidekiq run the background jobs.

    Is it really needed to switch back schema all time when we run the code in background? May be we can do a setting in sidekiq, whenever it starts the job switch the schema to the current tenant. I will be checking that for the best solution. Thank you.