简体   繁体   中英

Ruby on Rails working in background [resque + resque-status]

I am building a webapp using ruby on rails that requires running C++ exe programs in the background. I compared 3 most frequently used gems for this(Delayed_Jobs, Resque, Sidekiq) and found that resque is the most suitable for me.

In Countroller I have create method like this

    def create
      @model = Model.create(model_params)
      # resque processe the file from the @model
      Resque.enqueue(JobWorker, @model.file.url)
      redirect_to model_path(@model.id)
    end

In Worker class I have

    class JobWorker
      @queue = :file
      def perform file_to_process
        # calling time consuming C++ here which generates 2 image files.            
        system('my_file_processing_program "#{file_to_process}"')
      end
    end

Now my question is how should I detect that job has finished? I want to send gemerated image file to client once images are generated by C++ application. which usercan view/download.

As redirect_to model_path(@model.id) will return after Resque.enqueue(JobWorker, @model.file.url) in the create in controller.

I tried using resque-status but that requires polling in the controller to check the status like...

    while status = Resque::Plugins::Status::Hash.get(job_id) and !status.completed? && !status.failed?
      sleep 1
      puts status.inspect
    end

Any suggestions?? Thank you in Advance...

If you want to go asynchronous system like faye( http://faye.jcoglan.com/ruby.html ) so you can send a message to the frontend when the process is done. Write the code to publish a message after your system code finishes execution.

Another simple step, though might not be feasible for you is to send an email at the end of the process. You can send an email to the client letting them know "that the process is complete visit link to see the result."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM