使用线程在后台运行时保持页面活动

时间:2016-11-10 19:35:39

标签: ruby multithreading jenkins sinatra

我的基于Sinatra的ruby应用在本地运行得很好,但是,当我将其推送到CloudFoundry时,页面会在应用在页面上打印结果之前超时(说:“{{1这是我的hc.run.company.com took too long to respond. HTTP ERROR 504

main.rb

我正在尝试在执行require 'rest-client' require 'sinatra' require 'open-uri' require 'net/http' require 'dir' require 'json' # require 'sinatra/async' # require 'socket' class DevMonit < Sinatra::Base # register Sinatra::Async @@services = Array.new def self.register(service) @@services << service end get '/url/:url' do url = params[:url].to_s # Store 'url' parameter is_verbose = params[:v].to_s # Store 'v' parameter # stream :keep_open do |out| # print 'out = ' # DEBUG # p out # DEBUG # check_status(url, is_verbose) do |x| # out << "x=#{x}" # out.close # end # end return check_status(url, is_verbose) end def verbose(is_verbose) if !is_verbose.nil? && is_verbose == 'true' return true else return false end end def check_status(uri_str, is_verbose, limit = 10) response = RestClient.get "#{uri_str}" case response.code when 200...300 @@services.each do |service| if service.is_matched?("#{uri_str}") tool_name = service.class.name.split('::').last.downcase # Fetch the name of the Class for the tool_name @@result = service.check_tool_specific("#{tool_name}","#{uri_str}", "#{verbose(is_verbose)}") end end if @@result.nil? return 'No results, something went wrong!' end return "#{@@result.to_json}" when Net::HTTPRedirection then location = response['location'] check_status(location, limit - 1) else puts "It doesn't work!" puts "#{response.code}" end rescue SocketError => e; #do something with e.message return { :url => uri_str, :timestamp => Time.now.strftime('%Y-%m-%d %T'), :results => 'Cannot resolve the hostname' } end run! if app_file == $0 end return check_status(url, is_verbose)时为浏览器提供一些东西以使其保持活动状态(HTTP CODE 200?)。我试图这样做的原因是因为应用程序触发Jenkins作业并等待它完成,然后应用程序获取必要的信息将其置于json格式(这是输出),然后从{{{ 1}}。

我一直reading关于如何做到这一点但是,我无法使它工作,(我已经尝试用套接字做了一些事情并且失败了,我也尝试过像{{3这也不起作用。)

赞赏任何形式的指导。

更新

这是需要最长时间的代码(等待Jenkins工作完成以获取所需信息):

if service.is_matched?("#{uri_str}")

0 个答案:

没有答案