16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/stay_awake/strategies/net_http.rb', line 16
def self.buzz(url)
require 'timeout'
StayAwake.logger.info 'Starting buzzing using net/http.'
@thread = Thread.new do
loop do
begin
Timeout.timeout(30) do
uuid = SecureRandom.uuid
StayAwake.logger.debug "Starting HTTP Request (#{uuid})."
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request_method = StayAwake.config.request_method.to_s.capitalize
request_class = Kernel.const_get("Net::HTTP::#{request_method}")
response = http.request(request_class.new(uri.request_uri))
StayAwake.logger.debug "HTTP Response arrived (#{uuid})."
end
rescue Timeout::Error
StayAwake.logger.error "HTTP Timeout Error after 30 seconds (#{uuid})."
rescue StandardError => e
StayAwake.logger.error e.message
end
sleep StayAwake.config.interval
end
end
end
|