client_management/app/controllers/admin/plow_controller.rb

73 lines
2.1 KiB
Ruby
Raw Normal View History

2023-07-01 03:59:47 +00:00
class Admin::PlowController < ApplicationController
2023-04-20 16:11:21 +00:00
include ReverseProxy::Controller
2023-04-21 16:09:36 +00:00
SocketFile = "#{Rails.root}/tmp/plow.sock"
Binary = File.expand_path("../../../../bin/plow", __FILE__)
LogFile = "#{Rails.root}/tmp/plow_summary.log"
before_action :setup_setting, only: [:setting, :save_setting]
2023-07-01 03:59:47 +00:00
before_action :check_login
skip_before_action :verify_authenticity_token
2023-04-20 16:11:21 +00:00
def index
2024-05-07 15:46:25 +00:00
reverse_proxy "unix://#{SocketFile}", path: '/' do |config|
2023-04-20 16:11:21 +00:00
end
end
def show
2023-04-21 16:09:36 +00:00
path = request.env['ORIGINAL_FULLPATH']#.gsub("/admin/plow", "")
2024-05-07 15:46:25 +00:00
reverse_proxy "unix://#{SocketFile}", path: path do |config|
2023-04-21 16:09:36 +00:00
end
end
def setting
if File.exist?(LogFile)
@logContent = File.read(LogFile).gsub("\n", "<br>").html_safe
2023-04-20 16:11:21 +00:00
end
end
2023-04-21 16:09:36 +00:00
def save_setting
@setting.update_attributes(params[:plow_setting].permit!)
if File.exist?(SocketFile)
2024-05-07 15:46:25 +00:00
if @plow_pid != 0
2023-04-21 16:09:36 +00:00
Process.kill(:INT, @plow_pid)
end
`rm #{SocketFile}`
end
if params['act'] == 'Start Test'
uri = URI.parse(@setting.url)
host = uri.host
ip = @setting.ip
concurrent = @setting.concurrent
2023-04-23 09:27:04 +00:00
rate = @setting.rate
2023-04-21 16:09:36 +00:00
scheme = uri.scheme
Thread.new do
if !ip.blank?
2023-04-23 09:27:04 +00:00
`ulimit -n 100000 && #{Binary} #{scheme}://#{ip}#{uri.request_uri} --host=#{host} --listen=#{SocketFile} -c#{concurrent} --rate #{rate} --summary > #{LogFile}`
2023-04-21 16:09:36 +00:00
else
2023-04-23 09:27:04 +00:00
`ulimit -n 100000 && #{Binary} #{@setting.url} --listen=#{SocketFile} -c#{concurrent} --rate #{rate} --summary > #{LogFile}`
2023-04-21 16:09:36 +00:00
end
end
max_wait = 10
while 1
sleep(1)
setup_setting
max_wait -= 1
if @enable_plow
break
end
end
end
2024-02-28 13:54:20 +00:00
redirect_to admin_plow_setting_path
2023-04-21 16:09:36 +00:00
end
private
2023-07-01 03:59:47 +00:00
def check_login
user = current_user
2023-07-01 04:01:58 +00:00
if user.nil? || !user.is_admin?
2023-07-01 03:59:47 +00:00
return render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :formats => [:html]
end
end
2023-04-21 16:09:36 +00:00
def setup_setting
@setting = PlowSetting.first
@plow_pid = `fuser #{SocketFile}`.gsub(/\n/, '').to_i
@enable_plow = @plow_pid > 0
end
2023-04-20 16:11:21 +00:00
end