ActiveJob::DeserializationError: Error while trying to deserialize arguments - ruby

I am trying to send mail in production but it is throwing Activejob deserailization error.sidekiq is running in background. I have added sidekiq gem. I wrote one method in comment_notification.rb for sending email to the user. Then in controller in create action I have added this
def create
CommentNotification.send_comment_mail(#current_user).deliver_later(wait: 1.minute)
end
def send_comment_email(current_user)
mail( to: current_user.email,
:subject => "commented on post",
:from => "<noreply#xxx.com>")
end
It was working fine in local server but in production I am getting this error
/home/apps/commentpost/shared/bundle/ruby/2.3.0/gems/sidekiq-4.2.3/lib/sidekiq/processor.rb:69:in `run'
/home/apps/commentpost/shared/bundle/ruby/2.3.0/gems/sidekiq-4.2.3/lib/sidekiq/util.rb:17:in `watchdog'
/home/apps/commentpost/shared/bundle/ruby/2.3.0/gems/sidekiq-4.2.3/lib/sidekiq/util.rb:25:in `block in safe_thread'
2016-11-18T06:47:16.162Z 19093 TID-uw66g ActionMailer::DeliveryJob JID-e56b150964abf082e78089d9 INFO: start
2016-11-18T06:47:16.167Z 19093 TID-uw66g ActionMailer::DeliveryJob JID-e56b150964abf082e78089d9 INFO: fail: 0.005 sec
2016-11-18T06:47:16.167Z 19093 TID-uw66g WARN: {"context":"Job raised exception","job":{"class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"ActionMailer::DeliveryJob","queue":"mailers","args":[{"job_class":"ActionMailer::DeliveryJob","job_id":"96e06bc6-1380-47b9-9393-9727868b3897","queue_name":"mailers","priority":null,"arguments":["CommentNotification","send_comment_email","deliver_later",{"_aj_globalid":"gid://commentpost/comment/40"},{"_aj_globalid":"gid://commentpost/User/20"}],"locale":"en"}],"retry":true,"jid":"e56b150964abf082e78089d9","created_at":1479450405.8364522,"enqueued_at":1479451636.1602836,"error_message":"Error while trying to deserialize arguments: Couldn't find Comment with 'id'=40","error_class":"ActiveJob::DeserializationError","failed_at":1479450405.8429642,"retry_count":6,"retried_at":1479451636.1668367},"jobstr":"{\"class\":\"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper\",\"wrapped\":\"ActionMailer::DeliveryJob\",\"queue\":\"mailers\",\"args\":[{\"job_class\":\"ActionMailer::DeliveryJob\",\"job_id\":\"96e06bc6-1380-47b9-9393-9727868b3897\",\"queue_name\":\"mailers\",\"priority\":null,\"arguments\":[\"CommentNotification\",\"send_comment_email\",\"deliver_later\",{\"_aj_globalid\":\"gid://commentpost/comment/40\"},{\"_aj_globalid\":\"gid://commentpost/User/20\"}],\"locale\":\"en\"}],\"retry\":true,\"jid\":\"e56b150964abf082e78089d9\",\"created_at\":1479450405.8364522,\"enqueued_at\":1479451636.1602836,\"error_message\":\"Error while trying to deserialize arguments: Couldn't find Comment with 'id'=40\",\"error_class\":\"ActiveJob::DeserializationError\",\"failed_at\":1479450405.8429642,\"retry_count\":5,\"retried_at\":1479450981.998904}"}
2016-11-18T06:47:16.167Z 19093 TID-uw66g WARN: ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find Comment with 'id'=40
2016-11-18T06:47:16.167Z 19093 TID-uw66g WARN: /
Could anyone please help me regarding this doubt? For this I will be thankful.

As I understand correctly the create method in controller creates also the comment and sends the email for the freshly created comment?
Then it would be better to use a callback here.
We had the same issue in our project and we solved it by using something like that:
# in model
after_commit :send_mail, on: :create
private
def send_mail
CommentNotification.send_comment_mail(campaign.user).deliver_later
end
Then you can be sure that the record really exists in the Database before the mail gets delivered.
The problem here is, that you run a Comment#create and enqueue the mail in the controller. Now it can happen, that Sidekiq runs the task before Rails commits the new comment.
And then you get exactly this error.
Regards,
spa

When I ran into an issue with seemingly simple errors being raised due to the record not being the database (it was!), I found out that my Sidekiq workers were running in test mode and were able to dequeue jobs whose database records were supposed to be in the development database.
I was able to debug this after some trial and error in the stack trace by inspecting ActiveRecord::Base.connection's connection_parameters instance variable - it was pointing to the test database!
Looking through the Sidekiq logs, I was able to see that it was starting up workers to execute jobs against the test environment and database. My specific issue was also caused by spring. Disabling its use by invoking bundle exec sidekiq ... instead of sprint sidekiq fixed my issue.

Related

Capybara with selenium throws Net::ReadTimeout error Intermittently when using `visit` method with cucumber

I am getting Net::ReadTimeout error when using visit methods in a cucumber feature. Unfortunately, I am not able to reproduce this issue on my local system, it only intermittently fail on semaphore. We are using 20 parallel threads in semaphore.
The weired thing is I can see the screen shot of the page using capybara screenshot but still get the timeout error
Here is the test
RSpec.feature do
step 'that I have created a questionnaire' do
safe_visit '/somesite' # getting error on this line sometime
fill_in 'name', with: 'punit'
end
end
We are using rutabaga which is wrapper over turnip gem and allow to run features as rspecs.
What I have already tried (and did not work):
Increase timeout to 120 and retries Net::ReadTimeout (Net::ReadTimeout) Selenium Ruby
Add the selenium driver desired capabilities
Getting Net::ReadTimeout: visiting a website in Ruby Capybara Cucumber
NOTE: Please dont mark this as a duplicate of any other question because I have done a lot of search for quite a few days and then posted this question. Also one key difference in my issue is it is flakey and only happened rarely.
Please let me know in comment if I need any more info is needed.

How to fix error uninitialized constant error in ruby when using rake console?

I am using my rake console pry session to test out a class I have created for a CLI application I am making. Everytime I type "City.new" I expect to see nil attributes as instructed but I get a:
City.new
NameError: uninitialized constant City
What are common reasons for getting this error if I defined my class in another file. How can I fix this issue?
Thanks so much
I came across the same issue, I had not generated the controller for City model. Try that to see if will work

Hominid exception error with EOF reached file

I am using hominid gem for Rails 3 for to connect with MailChimP API. I write a class with many methods for my problem domain.
I need a rake task which run each X time. My problem is when i test the hominid methods.
Each X time hominid gem raise a exception with EOF Reached file error.
I test my methods many time without to change arguments. sometimes run very well while other times throw this exception.
I also test my methods calling from console and controller action.
There occurs same problem.
How could I sync hominid API calls?
Which could be the problem?
Thanks in advance

mysql timeout error in rails 3 when using delete_all and make! in step definition

I'm using following construct in a cucumber step definition.
Given "I have following stuff" do
Model.delete_all
list.each { |i| Model.make!(:name => i) }
end
(make! is from machinist 2).
Above step fails with INSERT statement timeout. When I open up a console for test, environment, I can execute each statement without an issue. Also, if I disable transactional features timeout goes away.
Can anyone please help me fix this? (This ran without an issue with rails 2.x)
(Database MySQL)
This turned out be an issue with machinist beta version I was using. I'm not 100% sure what was wrong, but when I used pure ActiveRecord code to create the objects, instead of make! it worked.
Then I switched to Factory_Girl instead of machinist, and now it works fine.

Sinatra - how do I debug it when it's online?

I wrote a teeny tiny Sinatra app that runs fine, locally, but for some reason as soon as I put it online, all I get is 'Internal Server Error'.
How do I get the logging output?
I'm running on Dreamhost with passenger, using the instructions from the Sinatra book.
So I added to more handlers:
get '/hello/:name' do
"Hello, #{params[:name]}!"
end
get '/nokogiri-test/' do
doc = Nokogiri::HTML(open('http://www.google.co.il/search?q='+params[:query]))
res = ''
doc.xpath('//li//h3//a').each do |li|
res+= li.content + '<br />'
end
res
end
The first one works fine, the second throws an error.
I'm not interested in why there's an error. I'm interested in how to get feedback for it and resolve errors in the future.
Ah! The answer comes from here.
Never would have thought to look in there, but I got desperate. The solution is to set the environment to :development:
set :environment, :development
I stuck that into my configuration files and it produced all the error output.
Still doesn't solve my problem if I ever want to discover what's causing an error in a PRODUCTION app...
So how would I solve that?
If you're seeing apache's Internal Server Error message, you should be able to check the apache error log to find out what's going on. I think on dreamhost the log file is stored in /home/your_user_name/logs/yourdomain.com/http/error.log

Resources