Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

ruby on rails - Indicate to an ajax process that the delayed job has completed

I have a Rails 3 app that uses delayed_job to fetch some data (with a class method) when the user hits the page. How can I indicate to the ajax process that the class method has run (so that I can stop polling)?

EDIT:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Typically, the best way to do this is to store, in your database, an indication of the job's progress. For instance:

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 that when you enqueue the job:

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

You can query, in your controller, the status of the job:

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

You polling ajax process can then simply call check_status to see how the job is progressing, if it has succeeded or if it has failed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...