Class: Corn::Post
- Inherits:
-
Object
- Object
- Corn::Post
- Defined in:
- lib/corn/post.rb
Instance Method Summary collapse
- #enqueue(*args) ⇒ Object
- #http_post(data) ⇒ Object
-
#initialize(interval) ⇒ Post
constructor
A new instance of Post.
- #start_post_thread(interval) ⇒ Object
- #submit_url ⇒ Object
- #terminate ⇒ Object
Constructor Details
#initialize(interval) ⇒ Post
Returns a new instance of Post.
8 9 10 11 |
# File 'lib/corn/post.rb', line 8 def initialize(interval) @queue = Queue.new @thread = start_post_thread(interval) end |
Instance Method Details
#enqueue(*args) ⇒ Object
36 37 38 |
# File 'lib/corn/post.rb', line 36 def enqueue(*args) @queue << args end |
#http_post(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/corn/post.rb', line 40 def http_post(data) uri = URI.parse(submit_url) req = Net::HTTP::Post.new(uri.path) req.set_form_data(data.merge('client_id' => Corn.client_id)) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true if Corn.ssl_verify_peer? http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.ca_file = Corn.ssl_ca_file if Corn.ssl_ca_file http.ca_path = Corn.ssl_ca_path if Corn.ssl_ca_path else http.verify_mode = OpenSSL::SSL::VERIFY_NONE end 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.}(#{res.code}), response body: \n#{res.body}") end rescue => e Corn.logger.error("post to #{submit_url} failed: #{e.}\n#{e.backtrace.join("\n")}") end |
#start_post_thread(interval) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/corn/post.rb', line 17 def start_post_thread(interval) if interval <= 0 Corn.logger.info("Corn post interval <= 0 sec, change it to 1 sec") interval = 1 else Corn.logger.info("Corn post interval #{interval} sec(s)") end Thread.start do begin loop do http_post(*@queue.pop) sleep interval end rescue => e Corn.logger.error("Corn post thread stopped by error #{e.}\n#{e.backtrace.join("\n")}") end end end |
#submit_url ⇒ Object
65 66 67 |
# File 'lib/corn/post.rb', line 65 def submit_url Corn.submit_url end |
#terminate ⇒ Object
13 14 15 |
# File 'lib/corn/post.rb', line 13 def terminate @thread.terminate end |