繁体   English   中英

Ruby on Rails在后台工作[resque + resque-status]

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

我在Rails上使用ruby构建了一个webapp,需要在后台运行C ++ exe程序。 我为此比较了3种最常用的宝石(Delayed_Jobs,Resque,Sidekiq),发现resque最适合我。

在Countroller中,我有这样的创建方法

    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

在工人班上,我有

    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

现在我的问题是我应该如何检测工作已完成? 我想在通过C ++应用程序生成图像后将生成的图像文件发送给客户端。 哪个用户可以查看/下载。

由于redirect_to model_path(@ model.id)将在create in控制器中的Resque.enqueue(JobWorker,@ model.file.url)之后返回。

我尝试使用resque-status,但这需要在控制器中轮询以检查状态,例如...

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

有什么建议么?? 先感谢您...

如果要使用faye( http://faye.jcoglan.com/ruby.html )这样的异步系统,则可以在完成该过程后向前端发送消息。 在系统代码完成执行后,编写代码以发布消息。

另一个简单的步骤(虽然可能对您而言不可行)是在流程结束时发送电子邮件。 您可以向客户发送电子邮件,让他们知道“该过程已完成,请访问链接以查看结果”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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