Jonas
Jonas

Reputation: 5149

Most simple way to do something after page was generated and sent to the user

I need to do some image manipulation (that could take long time) after page was generated and sent to the user. Not to make user wait while this job is done. Something like this:

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

I would like to avoid daemonizing.

Upvotes: 1

Views: 71

Answers (3)

Max
Max

Reputation: 15955

You could try using threads. Just render your view, and then spawn a new thread.

Upvotes: 1

mu is too short
mu is too short

Reputation: 434665

Delayed Job would probably be the easiest thing:

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

Then the actual do_image_manipilation call will be dealt with later. There is a daemon of sorts involved (just a constantly running rake task) but you don't have to deal with the details yourself, you just stick a .delay in the right places and Delayed Job takes care of the heavy lifting.

Upvotes: 2

Joost Schuur
Joost Schuur

Reputation: 4482

Not sure that this can be done without running some kind of processing daemon. Have you looked at beanstalkd or resque?

Upvotes: 1

Related Questions