简体   繁体   English

如何在Rails中获取delay_job的输出

[英]How can I get the output of delayed_job in rails

I have a "Status" page. 我有一个“状态”页面。 On which I am displaying the status of my local machines such as current upload/download speed, is recording going or not. 我正在显示本地计算机的状态(例如当前的上载/下载速度)的录像是否在进行。

For getting above information I am ssh into that machine. 为了获得上述信息,我将SSH放入该计算机。 Here is my sample code for it 这是我的示例代码

Net::SSH.start('localhost','ubuntu', :password => 'ubuntu') do |session|
    upload_speed = session.exec!("speedtest | grep Upload:").chomp.strip
    return upload_speed
end

But it is taking time (about 3-4 minutes) for fetching those status. 但是获取这些状态需要花费时间(大约3-4分钟)。 And it returns me "Connection time out error". 它返回我“连接超时错误”。 So I am trying to add this process in the background. 因此,我尝试在后台添加此过程。 For this I am using delayed_job gem 为此,我正在使用delayed_job gem

Here is my code for it My controller method 这是我的代码我的控制器方法

def unit_additional_status
    @machine = MachineInfo.find(params[:unit_id])    
    stat = Delayed::Job.enqueue(LongerTask.new(@machine), 3, :run_at => 1.seconds.from_now)
end

Here is my longer_task.rb file 这是我的long_task.rb文件

    require 'rubygems'
    require 'net/ssh'

    class LongerTask < Struct.new(:machine)
      def perform
             port = @machine.port
             @status = Hash.new
             Net::SSH.start('localhost','ubuntu', :password => 'ubuntu', :port => port) do |session|
              upload_speed = session.exec!("speedtest | grep Upload:").chomp.strip
              status["upload_speed"].push(upload_speed) 
            end
            @status
      end
end

After execution I have to pass this @status to my controller action so that I can pass it to my status.html.erb view. 执行后,我必须将此@status传递给控制器​​操作,以便将其传递给status.html.erb视图。

So I have a question how can I pass it to my controller method or how can get the output of execution of delayed job. 所以我有一个问题,如何将其传递给控制器​​方法,或者如何获取延迟作业执行的输出。

Also, if any one have better solution then let me know. 另外,如果有人有更好的解决方案,请告诉我。

I am using rails 3.2.14 and ruby 1.8.7 我正在使用rails 3.2.14ruby 1.8.7

You need to create some kind of additional status model, eg Job (status:string, message:string) . 您需要创建某种其他状态模型,例如Job (status:string, message:string) Then you pass an instance of this model to your delayed job task when it is scheduled. 然后,在计划了该模型的实例后,将其传递给延迟的任务。 When the task starts executing, you set the status to 'running'. 当任务开始执行时,将状态设置为“正在运行”。 When it finishes you update the message field with the desired result information and set status to 'finished'. 完成后,您将使用所需的结果信息更新消息字段,并将状态设置为“完成”。 This has several benefits like you have a good overview of your job queue and it can be extended to reflect execution time, errors etc. 这有很多好处,例如您可以很好地了解作业队列,并且可以扩展以反映执行时间,错误等。

To display the machine status in your example, you simply select the latest Job with status='finished' and show its timestamp and message. 要在示例中显示机器状态,您只需选择状态为“已完成”的最新作业,并显示其时间戳和消息即可。

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

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