简体   繁体   中英

Rails - running delayed_job on sh script

I have Rails 4.2 running on Unicorn.

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.

I currently call the sh script with system(@jobCall)

For this I installed the delayed_job gem. How do I run the system(@jobCall) without waiting for the job to finish?

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. Take a look at the documentation for delayed_job .

Basic notion is something like this:

Queuing Jobs

Call .delay.method(params) on any object and it will be processed in the background.

 # 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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