Error "undefined method `to_datetime'" in sidekiq - ruby-on-rails

Start sidekiq with the command
bundle exec sidekiq -e production -P /path/to/pid/file/tmp/pids/sidekiq.pid -L /path/to/log/file/shared/log/sidekiq.log --daemon
In the log error
2017-06-29T06:59:44.776Z 16181 TID-1jr7pg ERROR: CRON JOB: undefined method `to_datetime' for #<EtOrbi::EoTime:0x0000000a933848>
2017-06-29T06:59:44.776Z 16181 TID-1jr7pg ERROR: CRON JOB: /home/user/.rvm/gems/ruby-2.0.0-p247#script-admin/gems/activesupport-3.2.13/lib/active_support/core_ext/date_time/calculations.rb:141:in `<=>'
error while executing the method /home/user/.rvm/gems/ruby-2.0.0-p247#script-admin/gems/activesupport-3.2.13/lib/active_support/core_ext/date_time/calculations.rb:141:in <=>:
def <=> (other)
super other.kind_of?(Infinity) ? other : other.to_datetime
end
What can be done with the problem?
UPD: Updated version rails to 3.2.22.5 and there is a new error
ERROR: CRON JOB: comparison of Time with EtOrbi::EoTime failed
ERROR: CRON JOB: /home/user/.rvm/gems/ruby-2.0.0-p247#script-admin/gems/sidekiq-cron-0.3.1/lib/sidekiq/cron/job.rb:434:in `<'
in this place
def not_enqueued_after?(time)
#last_enqueue_time.nil? || #last_enqueue_time < last_time(time)
end

Your issue arises not from sidekiq but from Rails 3.2.13. #<=> does not handle undefined method to_datetime. It was fixed in future versions of Rails. For example, in the Rails 3.2.22.5:
def <=>(other)
if other.kind_of?(Infinity)
super
elsif other.respond_to? :to_datetime
super other.to_datetime
else
nil
end
end
Therefore, the simplest way to solve your issue is to update your Rails version. If it is not an options paste your code or rewrite #<=>.

to_datetime is a method from rails' activesupport library and your sidekiq worker is not using this.
try adding require 'active_support/core_ext' to your sidekiq initializer config config/initializers/sidekiq.rb and restart sidekiq

Related

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.

Is it possible to access Models in schedule.rb (whenever gem)?

I am performing some tests in a Ruby on Rails application using Whenever gem.
Is it possible to access Models from the schedule.rb file ?!
For example:
#Hard Worker
job = Job.find_by(name: 'Hard')
if job.status == 'active'
every 4.minutes do
runner 'HardWorker.perform_async'
end
end
Just checked this and I encountered this error:
config/schedule.rb:2:in `initialize': undefined method `find_by' for Whenever::Job:Class (NoMethodError)
Many thanks !

how to run rails commands in system command in rails when we run through delayed jobs?

def process_order
OrderProcess.delay(run_at:1.minutes.from_now).processing_order(params[:id])
redirect_to '/'
end
when process_order is executed
it will execute delayed job
class OrderProcess
def self.processing_order(order_id)
system("rails generate controller welcome index")
end
end
when we remove delay(run_at:1.minutes.from_now) and process_order then rails commands in system command are getting executed but when we run through delayed job rails commands are not getting executed
thanks in advance

Sidekiq: NoMethodError: undefined method `perform'

Here is what I am trying to do:
2.1.2 :001 > UpdateStyleRanks.perform_async
Here is the error:
NoMethodError: undefined method `perform' for #<UpdateStyleRanks:0x00000002f5e388>
Here is my worker:
# app/workers/update_style_ranks.rb
class UpdateStyleRanks
include Sidekiq::Worker
def perform
end
end
There is no naming requirement for workers. You can do this:
class Foo
include Sidekiq::Worker
def perform; end
end
I suspect you just need to restart Rails/Spring/Zeus to pick up the new class.
How would one restart a heroku dyno running in background?
heroku run:detached bundle exec sidekiq -e production
Would the following commands force sidekiq to pickup new class?
heroku ps:stop run
heroku run:detached bundle exec sidekiq -e production
In current versions of sidekiq (4 and 5) I believe the solution is to call perform_async and not perform.
You should rename you the file to update_style_rank_worker.rb
and the class name to UpdateStyleRankWorker

uninitialized constant SampleController::Delayed while executing delayed_job worker method

I am getting the following error while executing SampleController.
uninitialized constant IdeasController::Delayed
I have already started the delayed_job by using rake jobs:work. I have the following delayed_job code in SampleController.rb
Delayed::Job.enqueue(DelayedWorker.new({:model=>object.class.to_s,:object_id=>object.id,:meth=>:create_suggestion}))
delayed_worker.rb contains the following code:
class DelayedWorker < Struct.new(:options)
def perform
if obj=Object.const_get(options[:model]).find(options[:object_id])
if (options[:para] ? obj.send(options[:meth],options[:para].first) : obj.send(options[:meth]))
puts "Successfull"
else
puts "Failed"
end
end
end
end
Any one please help me for resolving this.
Thanks...
Change
Delayed::Job.enqueue(DelayedWorker.new({:model=>object.class.to_s,:object_id=>object.id,:meth=>:create_suggestion}))
to
::Delayed::Job.enqueue(DelayedWorker.new({:model=>object.class.to_s,:object_id=>object.id,:meth=>:create_suggestion}))

Resources