2011-09-08 40 views
1

我需要在生成頁面並將其發送給用戶後執行一些圖像操作(可能需要很長時間)。這項工作完成後,不要讓用戶等待。就像這樣:生成頁面後最簡單的方式生成併發送給用戶

if @post.save! 
    redirect_to :action => :index 
    # Now user is redirected and don't need to wait 
    # doing job in a background 
    do_image_manipulation 
end 

我想避免daemonizing。

回答

1

你可以嘗試使用線程。只需渲染你的視圖,然後產生一個新的線程。

1

不確定這可以在沒有運行某種處理守護進程的情況下完成。你看過beanstalkd還是resque

2

Delayed Job很可能是最簡單的事情:

if @post.save! 
    @post.delay.do_image_manipulation 
    redirect_to :action => :index 
end 

那麼實際do_image_manipilation通話將被處理後。有一個涉及到的惡魔(只是一個持續運行的耙子任務),但你不必自己處理細節,只需在適當的地方粘上一個.delay,Delayed Job負責繁重的工作。