I have the following code set up which will successfully list the different service pids that are running for Delayed_Job. However, I am looking for a way that I can call these actions within my view. The first is a function that will check the different pid files and if they exist then it will return true or false.

Within my view, i have links to the functions that are being called when the user clicks the link.

However, the problem is that whenever I refresh this page, it is executing the methods and automatically starting/stopping the services.

view

  <% if delayed_job_service_running? %>
    <em><b>Service Running</b></em> - 
    <%= link_to "Stop Service", delayed_job_stop %>
  <% else %>
    <em><b>Service Stopped</b></em> - 
    <%= link_to "Start Service", delayed_job_start %>
  <% end %>

helper file

def delayed_job_service_running?
    files = Dir.glob("#{Rails.root}/pids/**/delayed_job*")
    pids = []
    files.each do |file|
        pids << `tail #{file}`
    end
    pids.empty? ? false : true
end

def delayed_job_start
    `script/delayed_job -n 2 start`
end

def delayed_job_stop
    `script/delayed_job stop`
end
有帮助吗?

解决方案

I figured it out. Rather simple. Trick is to make it a callback action through a url.

view

link_to "stop service", "/admin/service/delayed_job_stop"

routes

match "admin/service/:string" => "pages#toggle_service", :as => "toggle_service"

controller

  def toggle_service
    command = params[:string].to_s
    if command == 'delayed_job_stop'
      `script/delayed_job stop`
    elsif  command == 'delayed_job_start'
      `script/delayed_job -n 2 start`
    end    
    redirect_to :back
  end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top