导轨执行脚本作为后台作业 [英] Rails execute script as background job

查看:122
本文介绍了导轨执行脚本作为后台作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经已实现为一个独立功能的Ruby脚本。
现在我想在我的轨道environament执行该脚本,与执行它作为后台作业的难度增加,因为它需要时间处理量很大。

添加delayed_job的宝石之后,我tryied调用下面的语句:

  delay.system(红宝石my_script.rb)

这是我的错误:

 在95ms之完成500内部服务器错误
类型错误(无法转储匿名模块:#<模块:0x007f8a9ce14dc0>):
   应用程序/控制器/ components_controller.rb:49:在​​'创造'


解决方案

从控制器的 self.delay 方法=htt​​ps://开头github上。 COM / collectiveidea / delayed_job的/问题/ 371相对=nofollow>将无法工作,因为DJ将尝试控制器序列化到作业。你最好创建一个类来处理你的任务然后标记其方法为异步的:

 类的AsyncTask
  高清运行
    系统('红宝石my_script.rb')
  结束
  handle_asynchronously:运行
结束

在你的控制器:

  DEF创建
    ...
    AsyncTask.new.run
    ...
结束

请参阅的Queing作业一节中的第二个例子中自述

I've a ruby script that has been implemented as an independent functionality. Now I would like to execute this script in my rails environament, with the added difficulty of executing it as a background job, because it needs a great amount of time processing.

After adding the delayed_job gem, I've tryied calling the following sentence:

delay.system("ruby my_script.rb")

And this is the error I get:

Completed 500 Internal Server Error in 95ms
TypeError (can't dump anonymous module: #<Module:0x007f8a9ce14dc0>):
   app/controllers/components_controller.rb:49:in `create'

解决方案

Calling the self.delay method from your controller won't work, because DJ will try to serialize your controller into the Job. You'd better create a class to handle your task then flag its method as asynchronous :

class AsyncTask
  def run
    system('ruby my_script.rb')
  end
  handle_asynchronously :run
end

In your controller :

def create
    ...
    AsyncTask.new.run
    ...
end

See the second example in the "Queing Jobs" section of the readme.

这篇关于导轨执行脚本作为后台作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆