ruby - Keep page alive while running in background using threads -
my sinatra-based ruby app works fine locally, however, when push cloudfoundry, page times out before app prints results on page (says: "hc.run.company.com took long 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 '/url/:url' url = params[:url].to_s # store 'url' parameter is_verbose = params[:v].to_s # store 'v' parameter # stream :keep_open |out| # print 'out = ' # debug # p out # debug # check_status(url, is_verbose) |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 |service| if service.is_matched?("#{uri_str}") tool_name = service.class.name.split('::').last.downcase # fetch name of class tool_name @@result = service.check_tool_specific("#{tool_name}","#{uri_str}", "#{verbose(is_verbose)}") end end if @@result.nil? return 'no results, went wrong!' end return "#{@@result.to_json}" when net::httpredirection location = response['location'] check_status(location, limit - 1) else puts "it doesn't work!" puts "#{response.code}" end rescue socketerror => e; #do e.message return { :url => uri_str, :timestamp => time.now.strftime('%y-%m-%d %t'), :results => 'cannot resolve hostname' } end run! if app_file == $0 end
i trying have thread feeds browser keep alive (http code 200?) while it's executing return check_status(url, is_verbose)
or @ if service.is_matched?("#{uri_str}")
. reason why trying because app triggers jenkins job , waits finish app takes necessary information puts in json format (which output), outputs return "#{@@result.to_json}"
.
i've been reading on how but, can't make work, (i've tried doing sockets , failed miserably, tried doing this , didn't work either).
any kind of guidance appreciated.
update:
this code takes longest time (waits jenkins job finish pull required information):
# require 'em-http-request' class waitforjob def self.job(current_build_json, last_build_json, start_job) job_result_hash = hash.new{|hsh,key| hsh[key] = {} } # initialize hash storing results start_job.send_request # start jenkins job get_current_build_number = checkjson.get_from_json("#{current_build_json.send_request}", 'nextbuildnumber') # fetch nextbuildnumber job starts (as doesn't increment while it's in queue); nextbuildnumber going currentbuildnumber current_build_number = get_current_build_number.to_i # save nextbuildnumber separate variable comparison get_last_build_number = checkjson.get_from_json("#{last_build_json.send_request}", 'number') get_last_build_duration = checkjson.get_from_json("#{last_build_json.send_request}", 'duration') get_last_build_result = nil loop timeout::timeout(120) # em.run sleep(5) # debug get_last_build_number = checkjson.get_from_json("#{last_build_json.send_request}", 'number') get_last_build_result = checkjson.get_from_json("#{last_build_json.send_request}", 'result') get_last_build_duration = checkjson.get_from_json("#{last_build_json.send_request}", 'duration') # conn = em::httprequest.new('http://localhost:9000/') # start = time.now # r1 = conn.get :query => {delay: 1.5}, :keepalive => true # r2 = conn.get :query => {delay: 1.0} # r2.callback # p time.now - start # => 1.5 - keep-alive + pipelining # em.stop # end # end end break if !get_last_build_result.nil? && !get_last_build_duration.zero? && (current_build_number == get_last_build_number) # end loop when job done end job_name = "#{checkjson.get_from_json("#{last_build_json.send_request}", 'fulldisplayname')}" # fetch job's name job_name = job_name.split(/ |\./) # splits job_name using '.' , ' ' delimiters job_result_hash['job_type'] = "#{job_name[3]}" # takes last part of jenkins job name (ex: dev.eng-paas.devtools.testing_integration_job) job_result_hash['build_number'] = "#{current_build_number}" # return build number can used in different situations job_result_hash['job_duration'] = "#{checkjson.get_from_json("#{last_build_json.send_request}", 'duration')}" # fetches duration of job job_result_hash['job_result'] = "#{checkjson.get_from_json("#{last_build_json.send_request}", 'result').downcase}" # fetches if job successful/unstable/failure return job_result_hash end end
Comments
Post a Comment