ruby rest-client: make it never timeout?
Asked Answered
D

6

18

I am trying to use ruby rest-client to upload a large number of images to a site that I'm writing. My code looks like:

RestClient.post url, :timeout => 90000000, :open_timeout => 90000000, :file_param => file_obj

However, I am getting this error:

RestClient::RequestTimeout: Request Timeout
    from /Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb:174:in `transmit'
    from /Library/Ruby/

But when I look at the server log

Completed in 61493ms (View: 2, DB: 1) | 201 Created 

So there doesn't appear to be any reason why this is timing out. Anyone have any idea if there is a timeout param I am not correctly setting?

Thanks

Deflate answered 14/12, 2010 at 3:11 Comment(0)
G
22

This syntax sets the timeout as request header (see RestClient.post signature), if you want to use the timeout parameter you must use:

RestClient::Request.execute(:method => :post, :url => @url, :timeout => 90000000)

see: https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb#L12

Giza answered 26/3, 2011 at 21:23 Comment(2)
RestClient::Request.execute(:method => :post, :url => @url, :timeout => 90000000)Troublemaker
And put any parameters to be sent in the POST body (I think :file_param in the OP) into :payload : Request.execute(:method => :post, :url => @url, :timeout => 90000000, :payload => { :file_param => file_obj })Fewness
P
14

Looking at the docs, you can pass -1 through RestClient.execute timeout param:

# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil

It can be used as follows:

resource = RestClient::Resource.new(
  "url",
  :timeout => -1,
  :open_timeout => -1
response = resource.get :params => {<params>}
Pinkiepinkish answered 25/7, 2012 at 21:7 Comment(1)
This appears to have been updated to nil rather than -1. Using -1 logs a warning (but appears to work).Fewness
S
4

I have used following code and works like a charm as pointed out by Richard

resource = RestClient::Resource.new "url", 
                                    :timeout => $TIMEOUT, 
                                    :open_timeout => $OPEN_TIMEOUT

response = resource.get  :params => { ..... }
Simonne answered 3/3, 2012 at 2:40 Comment(0)
K
4

I already use RestClient.get and RestClient.post extensively, so for me, it was easier to 'Monkey Patch' RestClient. I would recommend using RestClient::Resource.new or RestClient::Request.Execute if possible.

However, since I'm lazy, and don't want to go swap out every occurrence of RestClient.get / RestClient.post in my code, I've decided to take a shortcut.

$timeout = 30
$open_timeout = 30

module RestClient2
  include RestClient

  def self.get(url, headers={}, &block)
    Request.execute(:method => :get, :url => url, :headers => headers, 
     :timeout => $timeout, :open_timeout => $open_timeout, &block)
  end

  def self.post(url, payload, headers={}, &block)
    Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers,
     :timeout => $timeout, :open_timeout => $open_timeout, &block)
  end
end

And than I just just quick replaced RestClient.get/post with RestClient2.get/post.

It would be nice, if RestClient::Request had a default timeout specified, like:

  @timeout = args[:timeout] || 30
  @open_timeout = args[:open_timeout] || 30
Knesset answered 1/4, 2013 at 3:13 Comment(0)
N
3

I'm having similar issues. A quick dive into the source reveals this bit of unfriendliness:

def self.post(url, payload, headers={}, &block)
  Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
end

Unless I'm missing something, the timeout options aren't passed on to the underlying request. Time for a patch ...

Niue answered 13/1, 2011 at 23:0 Comment(1)
A slightly deeper dive shows that while the get, post and related convenience methods indeed do not allow you to pass the :timeout and :open_timout options, they are just thin wrappers for Request.execute, which will accept them. Better to replace calls to the wrappers with calls to execute than to monkey patch, IMHO.Nappy
H
2

The RestClient::Resource.new() allows you to set :timeout and :open_timeout values that will get passed to the Request.execute method, when you use the resource's get, post, put, etc methods

Hesiod answered 12/11, 2011 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.