Carlos San
Carlos San

Reputation: 23

Define timer method controller rails

I have a method in controller that calls another method created in a module like this example:

  def example
    @var1 = ModuleName::ClassName.get()

    respond_to do |format|
      format.json { render json: @var1}
    end
  end

The method get() goes to a website looking for information and returns an array. Everything works perfectly, but I wonder if in the controller there is a way to set a timeout if the application takes a long time to run! Is it possible?

Upvotes: 2

Views: 606

Answers (2)

Bhushan Lodha
Bhushan Lodha

Reputation: 6862

here is one way (a more general way) you could do that..

def example
 Timeout::timeout(40) do  # 40 sec, change it to anything you like
   @var1 = ModuleName::ClassName.get()
 rescue Timeout::error
   # do something (maybe set @var1's value if it couldn't get desired array)
 end
 respond_to do |format|
  format.json { render json: @var1}
 end
end

Upvotes: 2

oldhomemovie
oldhomemovie

Reputation: 15129

If under ModuleName::ClassName.get() you imply some kind of third-party ruby http library, then it's likely that you should set some kind of timeout parameter (depends on library). You just pass a desired timeout in seconds (or whatever measurement you want).

Thus the pseudo-code might look like this:

ModuleName::ClassName.get(10)

For more detailed answer, can you please be more specific about how are you doing a call to external service?

Upvotes: 0

Related Questions