Class: Corn::Post
- Inherits:
-
Object
- Object
- Corn::Post
- Defined in:
- lib/corn/post.rb
Instance Method Summary collapse
- #enqueue(data, name) ⇒ Object
- #http_post(data, name) ⇒ Object
-
#initialize ⇒ Post
constructor
A new instance of Post.
- #start_post_thread ⇒ Object
- #submit_url ⇒ Object
- #terminate ⇒ Object
Constructor Details
#initialize ⇒ Post
Returns a new instance of Post.
5 6 7 8 |
# File 'lib/corn/post.rb', line 5 def initialize @queue = Queue.new @thread = start_post_thread end |
Instance Method Details
#enqueue(data, name) ⇒ Object
27 28 29 |
# File 'lib/corn/post.rb', line 27 def enqueue(data, name) @queue << [data, name] end |
#http_post(data, name) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/corn/post.rb', line 31 def http_post(data, name) uri = URI.parse(submit_url) req = Net::HTTP::Post.new(uri.path) req.set_form_data("data" => data, 'client_id' => Corn.client_id, 'name' => name) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ENV['CORN_SKIP_SSL_VERIFY'] end res = http.request(req) Corn.logger.info("Corn report submitted to #{submit_url}") unless res.is_a?(Net::HTTPSuccess) Corn.logger.error("Post failed: #{res.message}(#{res.code}), response body: \n#{res.body}") end rescue Exception => e Corn.logger.error("post to #{submit_url} failed: #{e.message}\n#{e.backtrace.join("\n")}") end |
#start_post_thread ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/corn/post.rb', line 14 def start_post_thread Thread.start do begin loop do http_post(*@queue.pop) sleep 1 end rescue => e Corn.logger.error("Corn post thread stopped by error #{e.message}\n#{e.backtrace.join("\n")}") end end end |
#submit_url ⇒ Object
50 51 52 |
# File 'lib/corn/post.rb', line 50 def submit_url Corn.submit_url end |
#terminate ⇒ Object
10 11 12 |
# File 'lib/corn/post.rb', line 10 def terminate @thread.terminate rescue nil end |