Trying to get delayed_job working. I want to do a http post to another server without blocking my rails app. I figured delayed_job would be a good idea. However I am not able to get it to work.
Primarily in the controller if I hit a condition I want to do a http post to a known server.
What I have not understood so far is
- Can I delay a function defined in the controller itself?
- Do i need to define a separate class and have the function there which I can then include in the controller?
Steps so far:
- Added gem ‘delayed_job_active_record’ to Gem file
- Added gem “daemons” also
- rails generate delayed_job:upgrade
- rake db:migrate
- ran RAILS_ENV=development script/delayed_job start
Then execute the code.
Code in the controller:
Attempt 1: # Leads to NoMethodError (undefined method post_to_server for SimpleObjectForTestController )
class SimpleObjectForTestController
def create
....
if( some condition )
SimpleObjectForTestController.delay.post_to_server
end
....
end
def post_to_server
Typhoeus::Request.post( "http://127.0.0.1:4567/hi", :body => $fixed_config )
end
end
Attempt 2: Also gives same error now with SeperatedClass
class SeperatedClass
def post_to_server
Typhoeus::Request.post( "http://127.0.0.1:4567/hi", :body => $fixed_config )
end
end
class SimpleObjectForTestController
def create
....
if( some condition )
SeperatedClass.delay.post_to_server
end
....
end
end
Some other context:
This is a non UI application.
Using Rails 3.2.6
Ruby 1.9.2
Using delayed_job (3.0.3)
Using delayed_job_active_record (0.3.2)
Can someone tell me what I am doing wrong?
Your
post_to_serveris a instance method.This should work: