NameError: uninitialized constant MyJob ActiveJob and Sidekiq - ruby-on-rails

I have a problem with sidekiq/activejob integration. I have a controller that calls a perform_later method from a MyJob class. This works with the perform method, but when I change to perfom_later, the job is scheduled in my development log. However, when I see the sidekiq dashboard, at the retries section, I can see NameError: uninitialized constant (look below image)
These are my files:
# app/jobs/crime_job.rb
class CrimeJob < ActiveJob::Base
queue_as :default
def perform(crime)
puts "Perform #{crime}"
end
def self.job_name(crime)
"RadarCrime:#{crime.id}"
end
end
Crime Controller
# app/controllers/crime_controller.rb
def show
# [...]
CrimeJob.perform_later(#crime)
end
Sidekiq initializer
# config/initializers/active_job.rb
Rails.application.config.active_job.queue_adapter = :sidekiq

Well, I also open an issue in Sidekiq repository, and the solution is easier than I've think.
Just restart the sidekiq process and it's works fine.
Issue link: https://github.com/mperham/sidekiq/issues/2207

Related

Sidekiq fake mode is not working on rails rspec testing

I am trying to test a job on my rails code I have the following
RSpec.describe MyJob, type: :job do
it 'job in correct queue' do
VCR.use_cassette('mycassette/type') do
described_class.perform_later(id)
assert_equal 1, described_class.jobs.size
end
end
end
The above gives me an error undefined method jobs I have tested and it seems that I am running on the correct mode, Sidekiq::Testing.fake? gives me true
perform_later means you're using ActiveJob instead of using Sidekiq directly, so you also have to use ActiveJob's testing helpers: https://edgeapi.rubyonrails.org/classes/ActiveJob/TestHelper.html
assert_enqueued_jobs 1 do
described_class.perform_later(id)
end
Alternatively you can remove ActiveJob entirely by changing your job definition from:
class MyJob < ActiveJob::Base
queue_as :my_queue
# ...
to:
class MyJob
include Sidekiq::Worker
sidekiq_options queue: 'my_queue'
# ...

Rails 5 gem 'delayed_job_active_record' uninitialized constant Job

I have two jobs to run in Production one is always working fine, but other gets stuck sometimes with error
uninitialized constant AppointmentReminderJob\nDid you mean?
AppointmentLog\n/Path/shared/bundle/ruby/2.5.0/gems/activesupport-
5.2.4.1/lib/active_support/inflector/methods.rb:283:in
Started delayed job on Production via RAILS_ENV=production bin/delayed_job restart
In application.rb set config.active_job.queue_adapter = :delayed_job
File name appointment_reminder_job.rb
class AppointmentReminderJob < ApplicationJob
queue_as :default
def perform(appointment, clinic)
# My code here
end
end
Setting Job
AppointmentReminderJob.set(wait_until: Appointment::TIME_LIMIT).perform_later(#appointment, #clinic)
Note: Am not getting this error always but sometimes.

How to run a resque job in the console?

I am trying to call a job in the console but always get errors.
Followed the following documentation:
http://tutorials.jumpstartlab.com/topics/performance/background_jobs.html
https://www.youtube.com/watch?v=UrE47tCrZXc
https://guides.rubyonrails.org/active_job_basics.html
https://github.com/resque/resque
How can I run an ActiveJob in Rails console for debugging?
Created the following file:
(lib/jobs/archive_survey_jobs.rb)
module Jobs
class ArchiveSurveysJob < ActiveJob::Base
#queue = :setup
def perform(survey_type)
if SurveyTypes.all_classes.map(&:to_s).include?(survey_type)
surveys = Survey.where(something: 'stuff')\
.where.not(something: 'stuff',
something: 'stuff')
surveys.each do |survey|
do_something
end
end
end
end
end
I understand that I can do something like Resque.enqueue(ArchiveSurveysJob, 'string_here')
How can I call this in the console? If I try:
Jobs::ArchiveSurveysJob.create(survey_type: 'string_here'),
when I check the Resque Statuses it resulted in an error: `The task failed because of an error:
undefined local variable or method `args' for #
If I try this:
Jobs::ArchiveSurveysJob.perform(`string_here`)
Or:
Jobs::ArchiveSurveysJob.perform_now('string_here')
I get:
ArgumentError: wrong number of arguments (given 0, expected 1..2)
Please let me know if I am missing some documentation or if I am doing something wrong.
In your example, perform is an instance method. youre calling the class level perform which might not have any arguments. I dont know about your version of resque specifically, however you can enqueue jobs from the console like this in at least one version: Resque.enqueue(Jobs::ArchiveSurveysJob)
ps, make sure your resque workers are running.
In dev environments I typically have resque running in line:
# config/initializers/resque.rb
Resque.inline = true if Rails.env.dev? || Rails.env.test?
How I managed to do this was by creating a Rake task and calling the Job there.

Ruby on Rails jobs NotImplementedError

I am trying to implement a Job in my Ruby on Rails application, but I keep getting this error:
NotImplementedError
Error from the server:
NotImplementedError (NotImplementedError):
app/controllers/cron_controller.rb:6:in `message_10_minutes'
Here's the ActiveJob:
class TestSmsJob < ActiveJob::Base
queue_as :default
#include Plivio
def perform(*args)
# do my stuff
end
end
This is the call to the function for executing the job:
class CronController < ApplicationController
def index
end
def message_10_minutes
TestSmsJob.set(wait: 10.minutes).perform_later()
render :layout => false
end
end
Do you guys know what am I missing?
Rails by default comes with own implementation of asynchronous queuing however you still needs to specify which adapter do you want to use. If you want to use built in adapter you simply don't have to bundle any other gem.
Here is the list of adapters - http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html
Since sidekiq may be too hard for the start you can use sucker_punch - https://github.com/brandonhilkert/sucker_punch
After you choose your adapter you have to update config
module YourApp
class Application < Rails::Application
# Be sure to have the adapter's gem in your Gemfile
# and follow the adapter's specific installation
# and deployment instructions.
config.active_job.queue_adapter = :adapter_name
end
end

Rails Resque undefined method error in external module

I'm having trouble calling methods from an included module inside a resque worker. In the example below, I keep getting undefined method errrors when I attempt to call the say method inside the worker (which is in the TestLib module). I've reduced the code down to bare basics to illustrate the issue:
Controller
(/app/controllers/test_controller.rb)
class TestController < ApplicationController
def testque
Resque.enqueue( TestWorker, "HI" )
end
end
Library
(/lib/test_lib.rb)
module TestLib
def say( word )
puts word
end
end
Worker
(/workers/test_worker.rb)
require 'test_lib'
class TestWorker
include TestLib
#queue = :test_queue
def self.perform( word )
say( word ) #returns: undefined method 'say' for TestWorker:Class
TestLib::say( word ) #returns: undefined method 'say' for TestLib::Module
end
end
Rakefile
(resque.rake)
require "resque/tasks"
task "resque:setup" => :environment
I'm running resque using the following command: rake environment resque:work QUEUE='*'
Gems:
rails (3.0.4)
redis (2.2.2)
redis-namespace (1.0.3)
resque (1.19.0)
Server:
nginx/1.0.6
Anyone have any ideas as to what's going on there?
When you include a module, its methods become instance methods. When you extend, they become class methods. You just need to change include TestLib to extend TestLib and it should work.

Resources