delayed_jobs error, undefined method `email' for nil:NilClass - ruby-on-rails

the gem delayed_jobs works perfectly in development enviroment. But in production, it doesnot work. It throws the following error in the logs
Am using this delayed_job to send emails to users
I, [2013-11-20T13:55:28.418252 #31220] INFO -- : 2013-11-20T13:55:28+0000:
[Worker(delayed_job host:li483-234 pid:31220)] 1 jobs processed at 77.3979 j/s, 1 failed
I, [2013-11-20T14:05:48.680718 #31220] INFO -- : 2013-11-20T14:05:48+0000: [Worker(delayed_job host:li483-234 pid:31220)] Job Class#receive_msg (id=1) RUNNING
E, [2013-11-20T14:05:48.683409 #31220] ERROR -- : 2013-11-20T14:05:48+0000: [Worker(delayed_job host:li483-234 pid:31220)] Job Class#receive_msg (id=1) FAILED (5 prior attempts) with NoMethodError: undefined method `email' for nil:NilClass
any clues?

I found the answer
in config/application.rb place this on top
require 'yaml'
YAML::ENGINE.yamler = 'psych'
restart server and RAILS_ENV=production bin/delayed_job start

Related

Full backtrace for Omniauth error in testing enviroment

When running an automated test with a omniauth mock, I get:
Run options: -b --seed 58490
# Running:
D, [2023-02-14T18:24:28.454047 #7345] DEBUG -- omniauth: (github) Setup endpoint detected, running now.
E, [2023-02-14T18:24:28.457195 #7345] ERROR -- omniauth: (github) Authentication failure! undefined method `settings_object' for nil:NilClass: NoMethodError, undefined method `settings_object' for nil:NilClass
F
Failure:
Sessions::OmniauthControllerTes#test_should_create_a_github_session [/opt/web/test/controllers/sessions/omniauth_controller_test.rb:13]:
"Session.count" didn't change by 1.
Expected: 1
Actual: 0
rails test test/controllers/sessions/omniauth_controller_test.rb:10
Finished in 2.899824s, 0.3448 runs/s, 0.3448 assertions/s.
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
Even with -b, I don't get a backtrace for "Authentication failure!". I'm assuming this is either because:
Omniauth is catching the error
The testing environment is simulating a second http connection and this error is happening in that second connection
I've looked in log/test.log. I don't see any clues where this error is happening.
How to I get a backtrace for the "Authentication failure!" error so I know where to put a debugger call and further inspect?
I was able to find the location where to put the debugger call by running in the container:
$ grep 'Authentication failure!' -rin `find / -iname 'gem*' -type d `
But if there's a way for minitest to bring the backgrace into the forefront, I'd like to know it.

Resque not failing an obviously buggy job

I have the following problem with Resque and Rails 6 (ruby 2.7.2). I am new to this gem and wanted to do some simple testing to see how things are handled, so I created a simple failing job:
class TestJob < ApplicationJob
#queue = :default
def self.perform(*args)
0/0
end
end
Obviously, when I go to the console and run it, I get the error:
irb(main):001:0> TestJob.perform
Traceback (most recent call last):
3: from (irb):1
2: from app/jobs/test_job.rb:5:in `perform'
1: from app/jobs/test_job.rb:5:in `/'
ZeroDivisionError (divided by 0)
But when I run my worker with the command:
QUEUE=* VVERBOSE=true rake resque:work
And then enqueue my job in rails console, the output I get is:
irb(main):002:0> Resque.enqueue(TestJob)
=> true
And in the resque logs:
** [21:32:57 2020-12-09] 15198: resque-2.0.0: Waiting for default
** [21:33:02 2020-12-09] 15198: Checking default
** [21:33:02 2020-12-09] 15198: Found job on default
** [21:33:02 2020-12-09] 15198: resque-2.0.0: Processing default since 1607545982 [TestJob]
** [21:33:02 2020-12-09] 15198: got: (Job{default} | TestJob | [])
** [21:33:02 2020-12-09] 15198: resque-2.0.0: Forked 15765 at 1607545982
** [21:33:02 2020-12-09] 15765: done: (Job{default} | TestJob | [])
Additionally, in the resque web UI it counts this process as successful. The number of failed jobs stays 0. Furthermore, any puts and raise I put into the perform method are not showing in resque/rails console logs.
I have done a basic resque setup on a fresh rails app following the README on the resque's github page. I did not do anything custom. Obviously I restarted my console and worker after I made changes to the test_job.rb file, so this is not a problem of loading (maybe?). I also tried changing self.perform to perform.
I don't know why, but this is the solution. Rather than running:
Resque.enqueue(TestJob)
one should run:
TestJob.perform_later
The thing is, the first way is from the Resque README, and the second one is from the Rails docs. I guess I was fixated on doing this the first way, but it looks like going with the rails conventions is always best.

Rails rake rollbar:test didn't pass

I was trying to use rollbar gem to monitor error message.
I followed its document.
Add gem 'rollbar', and rails generate rollbar POST_SERVER_ITEM_ACCESS_TOKEN.
(I did changed the POST_SERVER_ITEM_ACCESS_TOKEN to my key)
But after I run rake rollbar:test, it only threw Test error from rollbar:test in dashboard.
Update
initializers/rollbar
Rollbar.configure do |config|
if Rails.env.test?
config.enabled = false
end
In console
rake rollbar:test 16:21:14
Testing manual report...
[Rollbar] Scheduling item
[Rollbar] Sending item
[Rollbar] Success
[Rollbar] Details: https://rollbar.com/instance/uuid?uuid=1f4c4e35-df5f-4606-916d-d97341b01dc7 (only available if report was successful)
Setting up the controller.
Processing...
Started GET "/verify" for at 2016-12-02 16:21:21 +0800
Check your rollbar config, probably it's disabled in development mode.
https://github.com/rollbar/rollbar-gem/blob/master/lib/generators/rollbar/templates/initializer.rb#L7

Ruby on Rails unicorn throws errors

I setup a droplet with Ruby on Rails on digitalocean and I removed the rails directory (App) and replaced it with the one I had locally. It is working fine, if I connect with ssh and go to the IP with the port 3000. However whenever I try to go to port 80, then it throws me an error:
http://prntscr.com/2qoljb
I have checked the logs and it says the following:
Rails Error: Unable to access log file. Please ensure that /home/rails/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
Rails Error: Unable to access log file. Please ensure that /home/rails/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
I, [2014-02-08T18:40:59.192086 #735] INFO -- : worker=1 ready
I, [2014-02-08T18:40:59.198211 #731] INFO -- : worker=0 ready
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:00 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 43ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:01 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 47ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:02 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 2ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
Started GET "/" for 188.193.201.119 at 2014-02-08 18:41:02 +0000
Processing by QuestionsController#index as HTML
Completed 500 Internal Server Error in 2ms
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/controllers/application_controller.rb:10:in `require_login'
The tables should exist, as everything is working completely fine, if I manually type in the IP with port 3000...
Thanks for your help in advance
EDIT
After setting permissions and the owner I am getting the following error instead:
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.3.5/lib/bundler/runtime.rb:216: warning: Insecure world writable dir /home/rails in PATH, mode 040777
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.3.5/lib/bundler/runtime.rb:216: warning: Insecure world writable dir /home/rails in PATH, mode 040777
I, [2014-02-08T19:27:30.231141 #1984] INFO -- : worker=1 ready
I, [2014-02-08T19:27:30.292714 #1981] INFO -- : worker=0 ready
First two lines explicitly talk about wrong file permissions but the Unicorn process shoud run under the same user as log file created by. I guess the capistrano and Unicorn run under different users.
If you are not sure what users should be and you have no problems with security just run:
$ chmod 0666 /home/rails/log/production.log
Once you restart the Unicorn the issue should gone.
I would not care about the problem with the log file in the moment. I do not think that is the root cause.
What catches my eye is Could not find table 'users'. The app does not find a table users in the database or does not find the database at all. Did check your credentials to the production database in config/database.yml? Did you run migrations on the database? Perhaps connect to the production database and check if the schema looks like you expect it to be.

rake jobs:work working fine. problem with script/delayed_job start

I am calling function with LoadData.send_later(:test).
LoadData is my class and test is my method.
It's working fine while i am running rake jobs:work.
But when i am running script/delayed_job start or run that time delayed_job.log shows error like
TEastern Daylight Time: *** Starting job worker delayed_job host:KShah pid:5968
TEastern Daylight Time: * [Worker(delayed_job host:KShah pid:5968)] acquired lock on LoadData.load_test_data_with_delayed_job
Could not load object for job: uninitialized constant LoadData
TEastern Daylight Time: * [JOB] delayed_job host:KShah pid:5968 completed after 0.0310
TEastern Daylight Time: 1 jobs processed at 10.6383 j/s, 0 failed ...
Any solution??
Try putting include LoadData in an initializer. I seem to remember DelayedJob including activerecord classes, notifiers etc, but not custom classes. Personally I'd put the class in your models directory. It's still dealing with data, even if it's not activerecord.
Try doing this:
Delayed::Job.enqueue LoadData.test
Also, a big gotcha that took me while to realize... if you make changes to the code restart rake jobs:work!

Resources