introduction to resque

24
Introduction to Resque Nov. 20, 2009 (Fri.) Feedforce, Inc. [email protected]

Upload: koshigoe

Post on 15-Jan-2015

3.199 views

Category:

Documents


1 download

DESCRIPTION

社内勉強会での発表に使ったスライドです。GitHub で使われているジョブキューシステムの Resque について、簡単に説明しています。

TRANSCRIPT

Page 1: Introduction to Resque

Introduction to ResqueNov. 20, 2009 (Fri.)Feedforce, [email protected]

Page 2: Introduction to Resque

Resque

GitHub のバックグラウンド処理用キュー

Ruby で実装

ジョブも Ruby

JobObj.perform(*args)

データベースに Redis を採用

git://github.com/defunkt/resque.git

モニタリング用 WEB アプリを同梱

Page 3: Introduction to Resque

Redis

Key-Value Store の一種

http://code.google.com/p/redis/

ANSI C で実装

POSIX システム上での動作を前提

永続性/高速/アトミック/String, List, Set

高度な演算/レプリケーション/など

メインデータベースとしても利用可能

Page 4: Introduction to Resque

Redis

$telnet localhost 6379SET keyname 3bar+OKGET keyname$3bar

Page 5: Introduction to Resque

Redis

http://w.koshigoe.jp/study/

Page 6: Introduction to Resque

GitHub の要件永続性

待機中ジョブの確認

待機中ジョブの更新

タグ

優先度

高速な push/pop

ワーカーの状態(run/fin)

失敗したジョブの確認

ワーカーの終了

fat, old, too long

永続的なワーカー

失敗したジョブはそのまま

再開、解放しない

Page 7: Introduction to Resque

これなんだ?アトミックにO(1)で push/pop

高速依存関係なしにインストールできる信頼できる Ruby クライアント任意の文字列を格納できる整数カウンタを使える永続性マスタ/スレーブのレプリケーションネットワーク

Page 8: Introduction to Resque

Resque + Redis

A. Resis

キューの問題を Redis に任せる

ワーカーの問題に焦点を当てる

可視性、信頼性、状態監視

Page 9: Introduction to Resque

Resque が請け負う事ジョブの実行

ワーカー/ジョブを監視する WEB インタフェース

応答性能のための親子 fork モデル

交換可能な失敗を知らせるバックエンド

Hoptoad とか

ほか、Redis のパワーを満喫

Page 10: Introduction to Resque

DEMO

$ sudo gem install redis redis-namespace yajl-ruby \ --source=http://gemcutter.org$ sudo gem install sinatra$ git clone git://github.com/defunkt/resque.git$ cd resque/examples/demo$ rackup config.ru$ open http://localhost:9292/

Page 11: Introduction to Resque

DEMO

$ VERBOSE=true QUEUE=default rake resque:work

Page 12: Introduction to Resque

DEMO

$ VVERBOSE=true QUEUE=default rake resque:work

Page 13: Introduction to Resque

DEMO

$ open http://localhost:9292/resque/

Page 14: Introduction to Resque

ジョブ?class Archive @queue = :file_serve

def self.perform(repo_id, branch = 'master') repo = Repository.find(repo_id) repo.create_archive(branch) endend

Page 15: Introduction to Resque

キューにジョブをclass Repository def async_create_archive(branch) Resque.enqueue(Archive, self.id, branch) endend

Page 16: Introduction to Resque

ジョブを取り出してklass, args = Resque.reserve(:file_serve)klass.perform(*args) if klass.respond_to? :perform

Page 17: Introduction to Resque

ワーカー起動$ cd app_root$ QUEUE=file_serve rake resque:work

Page 18: Introduction to Resque

ジョブの引数は JSON

{ 'class': 'Archive', 'args': [ 44, 'masterbrew' ]}

Page 19: Introduction to Resque

ジョブの失敗を通知ジョブが例外を発生させたら

ログに記録し

Resque::Failure モジュールに渡される

失敗は Redis か他のバックエンドに渡される

Resque は Hoptoad をサポートしている

http://www.hoptoadapp.com/pages/home

Page 20: Introduction to Resque

Resque vs DelayedJob

どちらかが常に優れているという話ではない

特性を把握し、用途に応じて選択すべき

Page 21: Introduction to Resque

Resque を選ぶべき複数のキューが必要数値による優先度は不要Ruby オブジェクトのまま引数に渡す必要がない非常に巨大なキューを扱う何が起きているかを確認したい多くの失敗や無秩序を前提としているRedis をセットアップできるRAM が小さすぎない

Page 22: Introduction to Resque

DelayedJob を選ぶべき数値による優先度を好む

毎日非常に多くのジョブを実行するわけでない

キューが小さく、すぐに空く

多くの失敗や無秩序を前提としない

簡単に何でもキューに投げ込みたい

Redis のセットアップを望まない

Page 23: Introduction to Resque

まとめ使った事はないけれど、モニタリングが素敵

GitHub という(個人的に十分な)実績がある

Ruby ベースなので(自分の)現状に馴染み易い

Redis は結構良い気がしている(根拠なし)

ちょっとだけ詳しく → http://w.koshigoe.jp/study/

Resque

Redis

Page 24: Introduction to Resque

ご清聴、ありがとうございました。