Specify the ajax process that completed the slow task

I have a Rails 3 application that uses delayed_job to retrieve some data (using the class method) when the user lands on the page. How can I point to the ajax process that was started by the class method (so that I can stop polling)?

EDIT:

To clarify, I want to know when I started delayed_job, and not when the ajax process succeeded. Then I want to pass the completed delayed_job status to the running ajax process.

+8
ajax ruby-on-rails comet polling delayed-job
source share
3 answers

As a rule, the best way to do this is to keep an indication of progress in your database. For example:

class User def perform_calculation begin self.update_attributes :calculation_status => 'started' do_something_complex self.update_attributes :calculation_status => 'success' rescue Exception => e self.update_attributes :calculation_status => 'error' end end end 

So, when you queue a task:

 User.update_attributes :calculation_status => 'enqueued' User.send_later :perform_calculation 

You can ask your controller for job status:

 def check_status @user = User.find(params[:id]) render :json => @user.calculation_status end 

You can then poll the ajax process and simply call check_status to find out how the work is progressing if it succeeds or if it fails.

+11
source share

With this gem, you can track progress directly on the Delayed::Job object itself: https://github.com/GBH/delayed_job_progress

Completed jobs are no longer automatically deleted, so you can poll the job until it returns with a completed state.

0
source share

If you use any JavaScript framework such as prototypejs, then in the optional options hash you usually provide a callback to onComplete and / or onSuccess. API reference

-2
source share

All Articles