I have a controller that takes a message and posts the data to a 3rd party url:
def create
@message = Message.new(params[:message])
if @message.valid?
post_to_server(@message)
end
end
private
def post_to_server
# Http Post to 3rd Party Url
end
Message is not a ActiveRecord object. It is a plain ruby object with some ActiveModel validations.
Now my concern here is that my application may appear slow if it is waiting for a response from the 3rd party server. What would be the best way to defer execution of this method so that the user can get a response immediately and the post_to_server part is run in the background?
I’ve googled a bit and found this: https://github.com/collectiveidea/delayed_job
But that requires that my object be an active record object and persisted. This seems a bit overkill for what I need.
Are there any other ways to defer execution of a method until after a response is returned?
Thanks
there is only one simple way to do that and it is to use delayedjob or resque or any other gems that provide delayed background processing. Sending request is IO blocking, so if u send this request if deffered thread, u block the current process in ruby mri.
DelayedJob will marshal your object using YAML and then convert it back for the execution, so u can pass any object.
@message.post_to_serveror
Jobs.post_to_server(@message)https://github.com/collectiveidea/delayed_job