module Phonehome
Constants
- JobInfo
Public Class Methods
call(secret_key, id) { || ... }
click to toggle source
# File lib/phonehome.rb, line 21 def self.call(secret_key, id) # id - from job, Event begin debug("Checking connection") result = check_connection?{yield} if result == false puts('Phonehome not responding') return false end debug("Checking secret key") check_secret_key(secret_key){yield} debug("Checking User events") user_events = get_user_events(secret_key).split status = user_events.include?(id.to_s) ? true : false if status == false puts('You try to run not your event!') # Write error to console yield exit end debug("Calling start") start(secret_key, id){yield} # id - from job, Event result_of_work = yield debug("Calling stop." + result_of_work.to_s) stop(id,result_of_work) rescue => e if e.class == RuntimeError && e.to_s =~ /All events are completed/ return false else debug("Sending error to server") send_error(id, e) # id - from API, CurrentEvent # Let the calling method know what happenned raise e end end end
check_connection()
click to toggle source
# File lib/phonehome.rb, line 129 def self.check_connection begin response = excon_get("http://#{@@phonehomeURL}") rescue Excon::Errors::SocketError => error puts "#{error} - Server not responding" sleep(5) retry end end
check_connection?() { || ... }
click to toggle source
# File lib/phonehome.rb, line 139 def self.check_connection? # check connection with server begin response = excon_get("http://#{@@phonehomeURL}") # if all ok, return true return true # and continue run code rescue Excon::Errors::SocketError => error # if server is offline begin # don't know why, but here got exception yield # and fix it by rescue rescue return false end return false # return false and stop running code end end
check_secret_key(secret_key) { || ... }
click to toggle source
# File lib/phonehome.rb, line 120 def self.check_secret_key(secret_key) response = excon_post("http://#{@@phonehomeURL}/check_secret_key/#{secret_key}") if response.data[:body] != 'true' puts 'Your secret key is invalid!' yield exit end end
debug(message)
click to toggle source
# File lib/phonehome.rb, line 179 def self.debug(message) if @@debugEnabled puts("PH:DEBUG:"+message) end end
excon_get(url)
click to toggle source
add retry logic
# File lib/phonehome.rb, line 166 def self.excon_get(url) debug("POST #{url}") Excon.get(url) end
excon_post(url)
click to toggle source
add retry logic
# File lib/phonehome.rb, line 154 def self.excon_post(url) debug("POST #{url}") Excon.post(url) end
excon_post_json(url)
click to toggle source
add retry logic
# File lib/phonehome.rb, line 160 def self.excon_post_json(url) debug("POST JSON #{url}") Excon.post(url, :headers => { "Content-Type" => "application/json" }) end
get_app_name(id)
click to toggle source
# File lib/phonehome.rb, line 109 def self.get_app_name(id) response = excon_post_json("http://#{@@phonehomeURL}/get_name/#{id}") return response.data[:body] end
get_status(type)
click to toggle source
# File lib/phonehome.rb, line 171 def self.get_status(type) if type == 1 return 'working' else return 'completed' end end
get_user_events(secret_key)
click to toggle source
# File lib/phonehome.rb, line 114 def self.get_user_events(secret_key) response = excon_post("http://#{@@phonehomeURL}/get_user_event/#{secret_key}") response.data[:body] return response.data[:body] end
send_error(id, e)
click to toggle source
# File lib/phonehome.rb, line 101 def self.send_error(id, e) debug("Error #{id}") if @events_info.include?(id) job_info = @events_info[id] excon_post("http://#{@@phonehomeURL}/query_update/#{job_info.api_job_id}/failed/#{URI.encode_www_form('error' => e.to_s.gsub("."," "))}") end end
set_debug(debug)
click to toggle source
# File lib/phonehome.rb, line 17 def self.set_debug(debug) @@debugEnabled = debug end
set_url(url)
click to toggle source
# File lib/phonehome.rb, line 11 def self.set_url(url) @@phonehomeURL = url end
start(secret_key, id) { || ... }
click to toggle source
# File lib/phonehome.rb, line 56 def self.start(secret_key, id) # id - from job, Event debug("Starting #{secret_key} #{id}") begin_time = Time.now app_name = get_app_name(id) response = excon_post_json("http://#{@@phonehomeURL}/query/#{secret_key}/#{get_status(1)}/#{id}") if JSON.parse(response.body)['status'] == 'All events are completed' puts "#{id}: All events are completed" yield raise "#{id}: All events are completed" end job_info = JobInfo.new(JSON.parse(response.body)['id'], JSON.parse(response.body)['status'], begin_time) @events_info[id] = job_info debug("job info #{id} #{job_info}") job_info end
stop(id,result_of_work = nil)
click to toggle source
# File lib/phonehome.rb, line 74 def self.stop(id,result_of_work = nil) debug("Stopping #{id}") finish = Time.now status = 'completed' if @events_info.include?(id) job_info = @events_info[id] if job_info.status == 'late' status = 'late_completed' end debug("Stopping #{id} #{status}") text = "#{finish.to_i - job_info.begin_time.to_i} secs" if (result_of_work.nil?) # text = else text = result_of_work.to_s.gsub("."," ") + " (" + text + ")" end debug("Stopping text.size #{text.size}") excon_post("http://#{@@phonehomeURL}/query_update/#{job_info.api_job_id}/#{status}/#{URI.encode_www_form('result' => text)}") else debug("Event with id '#{id}' not found in events_info") pp events_info end end