av501
av501

Reputation: 6739

Required delayed_job example that is not for sending mails

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

  1. Can I delay a function defined in the controller itself?
  2. Do i need to define a separate class and have the function there which I can then include in the controller?

Steps so far:

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?

Upvotes: 1

Views: 975

Answers (1)

Juri Glass
Juri Glass

Reputation: 91753

Your post_to_server is a instance method. This should work:

def self.post_to_server
  ...
end

Upvotes: 3

Related Questions