Class: CheckTaskr::HttpJsonAction
- Inherits:
-
JobsAction
- Object
- JobsAction
- CheckTaskr::HttpJsonAction
- Includes:
- Socket::Constants
- Defined in:
- lib/check-taskr/task/http_json.rb
Instance Attribute Summary collapse
-
#error_code ⇒ Object
Returns the value of attribute error_code.
-
#error_msg ⇒ Object
Returns the value of attribute error_msg.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#method ⇒ Object
Returns the value of attribute method.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#post_data ⇒ Object
Returns the value of attribute post_data.
Attributes inherited from JobsAction
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(options) ⇒ HttpJsonAction
constructor
A new instance of HttpJsonAction.
Methods inherited from JobsAction
Constructor Details
#initialize(options) ⇒ HttpJsonAction
Returns a new instance of HttpJsonAction.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/check-taskr/task/http_json.rb', line 26 def initialize() @name ||= [:name] @ip = [:ip] @port = [:port] || 80 @path = [:path] || "/" @method = [:method] || :get @post_data = [:post_data] @error_code = [:error_code] || @@default_error_code @error_msg = [:error_msg] || @@default_error_msg end |
Instance Attribute Details
#error_code ⇒ Object
Returns the value of attribute error_code.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def error_code @error_code end |
#error_msg ⇒ Object
Returns the value of attribute error_msg.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def error_msg @error_msg end |
#ip ⇒ Object
Returns the value of attribute ip.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def ip @ip end |
#method ⇒ Object
Returns the value of attribute method.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def method @method end |
#path ⇒ Object
Returns the value of attribute path.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def port @port end |
#post_data ⇒ Object
Returns the value of attribute post_data.
22 23 24 |
# File 'lib/check-taskr/task/http_json.rb', line 22 def post_data @post_data end |
Instance Method Details
#execute ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/check-taskr/task/http_json.rb', line 37 def execute log = Log4r::Logger['default'] hash = {:stat => 0, :ip => @ip, :msg => "OK", :error_id => @error_code } begin Net::HTTP.start(@ip, @port) do |http| http.read_timeout = 5 if @method == :get response = http.get(@path) end case @method when :get response = http.get(@path) when :post response = http.post(@path, @post_data) end body = response.body log.debug "body=#{body}" hash = JSON.load(body) # hash[:timestamp] = Time.now.to_i #if hash["stat"] && hash["stat"].to_i > 0 hash.each do |k, v| v[:error_id] = @error_code v[:ip] = @ip end #end end rescue Exception => e hash[:stat] = 2 hash[:timestamp] = Time.now.to_i hash[:msg] = "HTTP #{@method.to_s} #{@path}出现异常:#{e}" log.error hash.to_json end hash end |