Help us understand the problem. What is going on with this article?

外部連携等が遅い場合に強制的にタイムアウトさせる方法

More than 5 years have passed since last update.

rubyのtimeoutを用いる

sample_controller.rb
require 'timeout'

# ...
def index
  # 3秒たってもデータが取れなければ、Timeout::Errorがraiseされる
  Timeout::timeout(3) do
    # データ取りに行く
  end
rescue Timeout::Error
  render text: 'XXXサーバが遅いみたい'
end
# ...

まぁAjax使えよという内容。

Resqueとか?

resqueからのコピペ
class Archive
  require 'timeout'
  @queue = :file_serve

  def self.perform(repo_id, branch = 'master')
    repo = Repository.find(repo_id)
    # このJobがやりたいこととtransaction doは関係ないけど
    ActiveRecord::Base.transaction do
      Timeout::timeout(3) do
        repo.create_archive(branch)
      end
    end
  end
end

see: http://doc.ruby-lang.org/ja/1.9.3/class/Timeout.html

以下妄想

書いていて、こんな感じにできればワーカーの起動で面倒なことしなくて済みそうと思った。
(あるのかな?)

archive.rb
class Archive
  @queue = :file_serve
  @min_timeout = 5

  def self.perform(repo_id, branch = 'master')
    repo = Repository.find(repo_id)
    # Resque.current_worker.timeout でワーカーのタイムアウトが取ってこれる
    Timeout::timeout(Resque.current_worker.timeout) do
      # something
    end
  end
end

俺は5秒までのJob(@min_timeout <= 5)しか処理しないぜのワーカー

TIMEOUT=5 QUEUE=* rake resque:work

おいらは3秒までのJob(@min_timeout <= 3)しか処理しないぜのワーカー(ArchiveのキューはSkip)

TIMEOUT=3 QUEUE=* rake resque:work

と妄想。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away