Rails 5 - Resque not processing enqueued job - ruby-on-rails

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.

Related

Rails clockwork display on log

I'm trying to learn how to use the gem clockwork. I'm just trying to test on my Mac and putting a line onto the development log. I have it set at 3 minutes just for testing.
I have the following in lib/clock.rb :
require 'clockwork'
module Clockwork
handler do |job|
puts "Running #{job} =================================="
end
Clockwork.every(3.minutes, 'dailyjob')
end
Then I have lib/tasks/dailyjob.rb
class DailyJob
def perform
Rails.logger.info "Daily Job ========================================="
end
end
I then start Clockworks via the console $ clockwork clock.rb. It starts up and every 3 minutes the console says:
Running dailyjob ==================================
I, [2014-03-04T11:03:32.084240 #66442] INFO -- : Triggering 'dailyjob'
But, nothing shows up in the development.log file.
Thanks for the help!
Per docs, Clockwork will only write to STDOUT unless you configure it otherwise:
By default Clockwork logs to STDOUT. In case you prefer your own
logger implementation you have to specify the logger configuration
option. See example below.
Example from the docs is this:
module Clockwork
configure do |config|
config[:sleep_timeout] = 5
config[:logger] = Logger.new(log_file_path)
config[:tz] = 'EST'
config[:max_threads] = 15
config[:thread] = true
end
end

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.

Clean up task when combining multiple tasks in Rake

I have a build task in rake defined with the following dependencies:
desc 'Builds the App'
task :rebuild_dev => ["solr:start", "db:drop", "db:create", "db:migrate", "spec", "solr:stop"]
The first task "solr:start" starts the Solr Indexing server. Now, if the build fails (may be in spec tests fail), the "solr:stop" task is not executed. And the server is not stopped.
Is there any way to specify a clean-up task or a task that always runs even if one of the dependent tasks fail? In my case, to always ensure that "solr:stop" executes...
You just need use the ensure system of Ruby
desc "Builds the App"
task :rebuild_dev do
begin
["solr:start", "db:drop", "db:create", "db:migrate", "spec"].each do |t|
Rake::Task[t].execute
end
ensure
Rake::Task["solr:stop"].execute
end
end

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