Search code examples
ruby-on-railsrubyruby-on-rails-3cronsidekiq

Error "undefined method `to_datetime'" in sidekiq


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

Solution

  • 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 #<=>.