Resque not failing an obviously buggy job - ruby-on-rails

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.

Related

Rails 5 - Resque not processing enqueued job

I'm trying to do enqueue a simple job using Resque 1.26.0 (and Redis-rb 3.3.1). The job doesn't seem to be processing the perform function because resque-web is processing each job and shows 0 failures. The jobs also are being processed instantly.
The jobs are enqueued from a controller action with
Resque.enqueue(TestJob, url)
The job itself looks like
class TestJob < ApplicationJob
#queue = :tags_queue
Logger.new("log/resque_worker_QUEUE.log").fatal("thing")
def self.perform(url)
Logger.new("log/resque_worker_QUEUE.log").fatal("other thing")
logger.fatal("more errors please")
myDivideByZeroVar= 1/0
raise "error"
Logger.new("log/resque_worker_QUEUE.log").fatal("other thing")
logger.fatal("more errors please")
end
end
A rake task is also set up:
require 'resque/tasks'
task "resque:setup" => :environment
The redis-server is running.
The worker is started with rake resque:work QUEUE=*. Using verbose logging doesn't show anything useful.
The log file only shows the first fatal error string "thing". None of the other errors are logged that are inside perform.
What am I doing wrong here?
Solved this. The job needed to be called using ActiveJob instead of using Resque.enqueue. TestJob.perform_later(url) worked just fine.

Resque-Scheduler not working with ActiveJob in Rails 4.2

Has anyone been able to get scheduled jobs to work in Rails 4.2?
I am using resque, and I am attempting to use resque-scheduler to schedule jobs. I have a schedule that get loaded and the scheduler runs, and even looks like it is running the jobs but it doesn't do anything.
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Starting
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Loading Schedule
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Scheduling friends
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Schedules Loaded
resque-scheduler: [INFO] 2014-09-16T01:54:55-07:00: queueing FriendsJob (friends)
I can enqueue jobs like this and they get processed.
TestJob.enqueue(params[:id])
and I get this output in the worker log
got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])
** [01:24:01 2014-09-16] 54841: resque-1.25.2: Processing default since 1410855841 [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper]
** [01:24:01 2014-09-16] 54841: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])]
** [01:24:01 2014-09-16] 54841: resque-1.25.2: Forked 54882 at 1410855841
** [01:24:01 2014-09-16] 54882: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])]
Hello World!!
** [01:24:01 2014-09-16] 54882: done: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])
But when I try to schedule a job, it looks like the are getting enqueue but they well not doing anything.
Here is the schedule.yml
friends:
every: "30s"
queue: "friends"
class: "FriendsJob"
args: 8
description: "Friends jobs scheduler"
Here is the output from the scheduled job.
** [01:23:36 2014-09-16] 54841: got: (Job{friends} | FriendsJob | [8])
** [01:23:36 2014-09-16] 54841: resque-1.25.2: Processing friends since 1410855816 [FriendsJob]
** [01:23:36 2014-09-16] 54841: Running before_fork hooks with [(Job{friends} | FriendsJob | [8])]
** [01:23:36 2014-09-16] 54841: resque-1.25.2: Forked 54880 at 1410855816
** [01:23:36 2014-09-16] 54880: Running after_fork hooks with [(Job{friends} | FriendsJob | [8])]
** [01:23:36 2014-09-16] 54880: done: (Job{friends} | FriendsJob | [8])
After reading this http://dev.mikamai.com/post/96343027199/rails-4-2-new-gems-active-job-and-global-id
I am suspecting it has something to do with ActiveJob and GlobalId.
Take a look at the difference enqueued
** [01:24:01 2014-09-16] 54841: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])]
vs scheduled
** [01:23:36 2014-09-16] 54841: Running before_fork hooks with [(Job{friends} | FriendsJob | [8])]
The jobs themselves were generated via
rails g job <JobName>
They look like this:
class TestJob < ActiveJob::Base
queue_as :default
def perform(friend_id)
friend = Friend.find(friend_id)
name = friend.name.swapcase
puts "Hello World!!"
end
end
class FriendsJob < ActiveJob::Base
queue_as :friends
def perform(friend_id)
friend = Friend.find(friend_id)
name = friend.name.swapcase
puts "Hello World!!"
end
end
Any help with this will be greatly appreciated and thanks in advance.
********* UPDATE *********
I've removed the action_job railtie and I am using Resque and Resque-Scheduler only, and scheduled jobs are working now. So this does seems to be related to ActionJob/GlobalId. I've open an issue in the rails project. Hopefully, they will fix it soon.
****** SECOND UPDATE *********
I got an update on this. Cristianbica who works in the ActiveJob codebase said this.
"We haven't thought at recurring jobs so far and we don't support this as none of the adapters support this without an external gem. However this is a very nice feature but I don't think we can make it in time for 4.2. Also I'm not sure it will suitable to be included in rails"
https://github.com/JustinAiken/active_scheduler is a gem that wraps the one into the other
It seems that ActiveJob in Rails 4.2 is not supported by resque-scheduler. Therefore, jobs are not scheduled correctly, that explains the difference in the log when a job is enqueued using ActiveJob API and resque-scheduler.
To fix this, we should find a way to schedule job within ActiveJob wrapper:
ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper
Resque-scheduler provides the way to support extensions that are not supported by default. To do this, we should extend custom job class to support the #scheduled method. This way we can enqueue job manually using ActiveJob API.
The easiest way is to write general code method in the base job, and then extend all jobs from it:
# custom base job
class Job < ActiveJob::Base
# method called by resque-scheduler to enqueue job in a queue
def self.scheduled(queue, klass, *args)
# create the job instance and pass the arguments
job = self.job_or_instantiate(*args)
# set correct queue
job.queue_name = queue
# enqueue job using ActiveJob API
job.enqueue
end
end
Reque-scheduler will call this method to schedule every single job extended from Job class. This way jobs are going to be enqueued within the ActiveJob wrapper. Result will be same as calling MyJob.perform_later(*args).
UPDATE: 4/4/16 - Whilst the below answer is still correct with the current version of Rails, I now use the active_scheduler gem created by Justin as mentioned in the answer above: https://stackoverflow.com/a/29155372/1299792
ORIGINAL ANSWER:
Avoid using ActiveJob if you need to schedule recurring jobs.
From the issue that Luis raised
As we are not currently supporting recurring jobs with ActiveJob we're going to close this. If it's possible to support recurring jobs I'm seeing this as a separate gem or in rails 5. Feature requests and talks around them are usually talked in the mailing list (https://groups.google.com/forum/#!forum/rubyonrails-core).
As a solution to your problem #luismadrigal I suggest you use the resque-scheduler way to do recurring job.
https://github.com/rails/rails/issues/16933#issuecomment-58945932
There was talk about creating a gem in the mailing list but I can't find any further information on it.

Why is Mongoid/Moped throwing Timeout::Error to Airbrake in production Rake tasks?

I'm having a problem on my production installation. I have several scheduled rake tasks that run successfully. However, I've been getting MANY Timeout::Error: execution expired emails from Airbrake.
But, here's the weird part: the Rake tasks all complete successfully. I'm capturing the stdout/stderr in the cron command, and the errors don't appear in the logs. I've also added simple print statements to the end of the rake tasks to verify their completion.
How can I further troubleshoot this?
Here is a stacktrace from one of the emails:
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:203
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:203
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:235
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:203
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:260
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:260
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:259
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:153
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/connection.rb:30
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:507
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:122
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:527
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:542
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:526
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:515
[GEM_ROOT]/gems/newrelic_moped-0.0.3/lib/newrelic_moped/instrumentation.rb:24
[GEM_ROOT]/gems/newrelic_rpm-3.4.2.1/lib/new_relic/agent/method_tracer.rb:242
[GEM_ROOT]/gems/newrelic_moped-0.0.3/lib/newrelic_moped/instrumentation.rb:20
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:70
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/node.rb:376
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/cluster.rb:112
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/cluster.rb:125
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/cluster.rb:125
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/cluster.rb:78
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/cluster.rb:147
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/session/context.rb:108
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/session/context.rb:50
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/database.rb:76
[GEM_ROOT]/gems/moped-1.2.5/lib/moped/query.rb:36
[GEM_ROOT]/gems/mongoid-3.0.5/lib/mongoid/contextual/mongo.rb:69
[GEM_ROOT]/gems/mongoid-3.0.5/lib/mongoid/contextual.rb:18
[PROJECT_ROOT]/lib/tasks/reminders.rake:7
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:205
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:205
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:200
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:200
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:158
/usr/lib/ruby/1.9.1/monitor.rb:211
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:151
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/task.rb:144
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:116
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:94
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:94
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:94
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:133
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:88
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:66
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:133
[GEM_ROOT]/gems/rake-0.9.2.2/lib/rake/application.rb:63
[GEM_ROOT]/gems/rake-0.9.2.2/bin/rake:33
[GEM_ROOT]/bin/rake:19
[GEM_ROOT]/bin/rake:19

How do I use Rails clockwork gem to run rake tasks?

What is the syntax for calling rake tasks from clockwork? I've tried all kinds of syntax, and nothing seems to work. (I'm specifically interested in clockwork because Heroku's supporting it.)
Here's my clock.rb, using the same syntax that the whenever gem uses:
module Clockwork
puts "testing clockwork!"
every(30.seconds, 'Send Messages') {
rake 'scheduler:send_messages'
}
end
And here's my rake task in scheduler.rake:
task :send_messages => :environment do
puts "rake task run successfully!"
end
And here's what happens when I start a clockwork process:
$ clockwork lib/clock.rb
testing clockwork!
I, [2012-07-16T14:42:58.107245 #46427] INFO -- : Starting clock for 1 events: [ Send Messages ]
I, [2012-07-16T14:42:58.107364 #46427] INFO -- : Triggering 'Send Messages'
attempting to run rake task!
E, [2012-07-16T14:42:58.107437 #46427] ERROR -- : undefined method `rake' for Clockwork:Module (NoMethodError)
This runs every 30 seconds. As you can see, the clock.rb is executed successfully. But I can't for the life of me figure out the syntax to run a rake task. The clockwork readme is no help, unfortunately:
https://github.com/tomykaira/clockwork
rake is not a method, so you can't invoke it like that here.
You can either shell out and invoke it, something like
every(30.seconds, 'Send Messages') {
`rake scheduler:send_messages`
}
or rather invoke a new detached process using the heroku API. This is my preferred method right now:
Heroku::API.new.post_ps('your-app', 'rake scheduler:send_messages')
Heroku::API is available from heroku.rb: https://github.com/heroku/heroku.rb
You can add the following method to your clock.rb file:
def execute_rake(file,task)
require 'rake'
rake = Rake::Application.new
Rake.application = rake
Rake::Task.define_task(:environment)
load "#{Rails.root}/lib/tasks/#{file}"
rake[task].invoke
end
and then call
execute_rake("your_rake_file.rake","your:rake:task")
in your handler
You can pass in a block to every that executes your rake task:
every(1.day, "namespace:task") do
ApplicationName::Application.load_tasks
Rake::Task['namespace:task'].invoke
end
Invoking the task using Rake::Task['...'].invoke works well the first time, but the task need to be reenabled to be invoked again later.
ApplicationName::Application.load_tasks
module Clockwork do
every(10.minutes, "namespace:task") do
Rake::Task['namespace:task'].invoke
Rake::Task['namespace:task'].reenable
end
end
Otherwise the task will be invoked the first time, but no more after that until the clockwork process is restarted.

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