[英]Rails - running delayed_job on sh script
I have Rails 4.2 running on Unicorn. 我有在独角兽上运行的Rails 4.2。
I need to call an sh script from my application, but I need my controller to call the SH script and not wait for the SH script to run. 我需要从应用程序中调用sh脚本,但是我需要控制器来调用SH脚本,而不要等待SH脚本运行。
I currently call the sh script with system(@jobCall)
我目前用system(@jobCall)
调用sh脚本
For this I installed the delayed_job gem. 为此,我安装了delay_job gem。 How do I run the system(@jobCall)
without waiting for the job to finish? 如何在不等待作业完成的情况下运行system(@jobCall)
?
Full create method code below: 完整的创建方法代码如下:
def create
@job = Job.new(job_params)
respond_to do |format|
if @job.save
@jobCall = 'sh /home/some.user/kitchen.sh'
system(@jobCall) ##need to call this and not wait for job to finish
format.html { redirect_to @job, notice: 'Job ran'}
format.json { render :show, status: :created, location: @job }
else
format.html { render :new }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
I think what you need is to run the job in the background using threads . 我认为您需要使用线程在后台运行作业。 Although, you may want another way of getting the job result, not render or redirect if you don't need to wait for it to finish. 虽然,您可能需要另一种获取作业结果的方法,但是如果不需要等待完成,则不要渲染或重定向。
I am not familiar with Ruby / Rails, but just calling system() will not do what you want, because system() does not use the delayed_job gem. 我不熟悉Ruby / Rails,但是仅调用system()并不能满足您的要求,因为system()不使用delay_job gem。 Take a look at the documentation for delayed_job . 看一下delay_job的文档 。
Basic notion is something like this: 基本概念是这样的:
Queuing Jobs 排队工作
Call .delay.method(params) on any object and it will be processed in the background. 在任何对象上调用.delay.method(params),它将在后台处理。
# without delayed_job @user.activate!(@device) # with delayed_job @user.delay.activate!(@device)
Edit: Not sure whether this works (because I'm lacking any experience with Ruby on Rails), but it should be something like this: 编辑:不确定这是否有效(因为我缺乏使用Ruby on Rails的经验),但是应该是这样的:
class Foo
def sh_script
system('sh /home/some.user/kitchen.sh')
end
handle_asynchronously :sh_script, :priority => 20
end
and then 接着
@f = new Foo.new()
@f.delay.sh_script()
... should do the trick. ...应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.