Ruby on Rails在后台工作〔resque+resque status〕

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

本文关键字:resque+resque status 工作 后台 on Rails Ruby      更新时间:2023-10-16

我正在使用ruby on rails构建一个需要在后台运行C++exe程序的Web应用程序。我比较了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++应用程序生成图像后将生成的图像文件发送到客户端。哪个用户可以查看/下载。

由于重定向到model_path(@model.id)将在创建控制器中的Resque.enqueue(JobWorker,@model.file.url)之后返回。

我尝试使用resque状态,但这需要在控制器中进行轮询以检查状态,如。。。

    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)因此,您可以在流程完成时向前端发送消息。编写代码以在系统代码完成执行后发布消息。

不过,另一个简单的步骤可能对你来说不可行,那就是在流程结束时发送一封电子邮件。您可以向客户发送一封电子邮件,让他们知道"流程已完成,请访问链接查看结果。"