undefined method `handle_asynchronously' - ruby-on-rails

I followed this article(How to schedule a function to execute at a future time?) to create schedule a method execution every couple of minutes.
I added the bellow code after my update method in the model controller.
handle_asynchronously :update_delay, :run_at => Proc.new { 1.minutes.from_now }
When i refresh the page i get this error
undefined method `handle_asynchronously'

Your linked post says
With DelayedJob you can do something like
So the method handle_asynchronously is probably a helper served by delayed_job. You should install the gem
#Gemfile
gem 'delayed_job_active_record'
Run
bundle install
and
rails generate delayed_job:active_record
rake db:migrate

Related

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
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

groupdate gem not included in Gemfile?

I have
gem 'groupdate'
in my Gemfile in my Rails project. I run bundle and then open bin/rails c to test the methods out yet I still get the error:
[4] pry(main)> users.group_by_day { |u| u.created_at }
NoMethodError: undefined method group_by_day' for #>>><User::ActiveRecord_Relation:0x007faee79ca818>
from /Users/Jonathan/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:136:inmethod_missing'
I've ran bundle update and whatnot and I'm unsure what the issue could be

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 !

delayed_jobs not creating rows in delayed_jobs table

In may rails application i wanted to use delayed job. SO i installed delayed jobs as follows.
in gemfile
gem 'delayed_job_active_record', '0.4.3'
then in console
rails generate delayed_job:active_record
rake db:create
It had created delayed_jobs table in database.
And i started
rake jobs:work
Then i added the following code in controller:
Task.handle_asynchronously :in_the_future, :run_at => Proc.new { 5.minutes.from_now }
here Task is the model name.
And in the model task.rb i wrote
def in_the_future
self.update_attiributes(:status=> "updated")
end
After running controller method it's not creating any record in the delayed_jobs table. Please correct me if i am doing anything wrong.
Can You change the controller code to
Task.delay(:run_at => Proc.new { 5.minutes.from_now }).in_the_future

Rails 3 + Exception notifier: How do I use exception notifier for rake tasks?

So in the old plugin for Rails 2 there used to be a method called notifiable that I could use to surround whatever Rake task I needed to attach exception notifier to. However, when I try to run my rake task it gives me an undefined method error. I looked around and noticed someone else use the exception_notify method and tried replacing this:
task(:create_orders_for => :environment) do
notifiable do
...
end
end
with this:
exception_notify {:create_orders_for => :environment} do
#notifiable do
...
end
But it doesn't work. Does anyone know what the Rails 3 version of this method is? I can't find it anywhere.
So this is what I eventually ended up doing. Works great.
Add the middleware configuration to your environment/whatever_environment_you_want.rb file
If you're testing in dev or test, you need to set the consider_all_requests_local to false
Change your rake task to this:
task(:create_orders_for => :environment) do
begin
...
rescue => e
ExceptionNotifier::Notifier.exception_notification(Rails.env, e).deliver
end
end

Resources