Asynchronous access via Ruby On Rails


ふと見かけたので、超訳
全部じゃなくて、ところどころです。 (^^;

http://dineshakadinks.blogspot.com/2008/08/asynchronous-access-via-ruby-on-rails.html



Over the last month we have been pondering upon a major problem that needed to be implemented in a Ruby On Rails application.

先月から Rails アプリケーションでどう実装しようか考えていた。

As the problem it self suggests, this is a tedious process. Database access is evident and the procedure is also lengthy.

DBアクセスがあり、長時間かかる処理が悩みの種。

If the algorithm was called inside the controller, the response would stall until the whole algorithm was completed. This is NOT what I wanted.

その処理(アルゴリズム)がコントローラから呼ばれると、処理が完了するまで応答がブロックされてしまう。
これはよくない。

Ap4r came to the rescue here. Asynchronous Processing For Ruby could be used by any rails application to route all those tedious and time consuming accesses.

ここで、AP4R
AP4R (Ruby のための非同期処理ライブラリ) は、こうした時間のかかる処理を実行するのに使える。

... and wallah !! You are in ! Simple as that.

(訳註: たぶん、喜んでる)

I had two places which acted as triggers. One was in the controller part and the other was from a model.

非同期処理を呼び出したかった部分は 2箇所ある。コントローラ内とモデル内。

Triggering from the controller part was easy as the plug-in's init file actually added the ap4r method with the Action Controller.

コントローラからの呼び出し部分は AP4R プラグインで追加されたメソッドで簡単に実装できた。

Triggering from the model was tricky.

モデルからの呼び出しはトリッキー。
(訳註: AP4R は URL を指定することで非同期処理を実行するが、モデルは URL (コントローラ) を意識するものではないので...)

I took the easy way out of this by firing a system command which did a wget to the url. This method (corresponding to the url) written in the controller does an asynchronous access thus returning immediately.

そんなわけで、(モデルから) wget した。wget する URL で非同期処理を呼び出すメソッド (アクション) を実行。こうすることで、応答はすぐ返ってくる。

I thought I did a pretty good job in writing the algorithm.

めでたしめでたし。