我有以下代码设置,它将成功列出为Delayed_Job运行的不同服务pid。但是,我正在寻找一种方法,我可以在我的视图中调用这些操作。第一个是检查不同pid文件的函数,如果它们存在,那么它将返回true或false。
在我看来,我有链接到用户点击链接时调用的函数。
然而,问题在于每当我刷新此页面时,它都会执行方法并自动启动/停止服务。
查看
<% 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 %>
帮助文件
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
答案 0 :(得分:0)
我明白了。相当简单。诀窍是通过网址将其作为回调动作。
查看强>
link_to "stop service", "/admin/service/delayed_job_stop"
<强>路由强>
match "admin/service/:string" => "pages#toggle_service", :as => "toggle_service"
<强>控制器强>
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