Skip to content

Commit a1e58bf

Browse files
committed
adds schedule_delay instead of fixed period
1 parent e2390f9 commit a1e58bf

24 files changed

Lines changed: 185 additions & 138 deletions

.ruby-gemset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stackify-api-ruby

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.1.2

Gemfile.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
PATH
22
remote: .
33
specs:
4-
stackify-api-ruby (1.0.2)
4+
stackify-api-ruby (1.0.3)
5+
faraday (>= 0.8)
56

67
GEM
78
remote: https://rubygems.org/
89
specs:
10+
faraday (0.9.0)
11+
multipart-post (>= 1.2, < 3)
12+
multipart-post (2.0.0)
913
rake (0.9.6)
1014

1115
PLATFORMS

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ You can set minimal level of logs, which should be caught by gem:
6262

6363
If you want to use proxy for sending request, you can do it in such way:
6464

65-
config.with_proxy = true
66-
config.proxy_host = "127.0.0.1"
67-
config.proxy_port = "8118"
68-
config.proxy_user = nil
69-
config.proxy_pass = nil
65+
config.proxy = { uri: '127.0.0.1:8118', user: 'user_name', password: 'some_password' }
7066

7167
For internal logging stackify-api-ruby uses such logger:
7268

lib/generators/stackify/templates/stackify.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@
66
#config.log_level = :error
77
#config.logger = Logger.new(File.join(Rails.root, 'log', 'stackify.log'))
88
#config.logger.level = Logger::INFO
9-
#config.with_proxy = false
10-
#config.proxy_host = '127.0.0.1'
11-
#config.proxy_port = '8118'
12-
#config.proxy_user = nil
13-
#config.proxy_pass = nil
9+
#config.proxy = {uri: '127.0.0.1:8118', user: 'user_name', password: 'some_password'}
1410
end

lib/stackify-api-ruby.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ module Stackify
1616
autoload :EnvDetails, 'stackify/env_details'
1717
autoload :Scheduler, 'stackify/scheduler'
1818
autoload :ScheduleTask, 'stackify/schedule_task'
19+
autoload :ScheduleDelay, 'stackify/schedule_delay'
1920
autoload :Worker, 'stackify/workers/worker'
2021
autoload :AuthWorker, 'stackify/workers/auth_worker'
2122
autoload :LogsSenderWorker, 'stackify/workers/logs_sender_worker'
23+
autoload :MsgsQueueWorker, 'stackify/workers/msgs_queue_worker'
2224
autoload :AddMsgWorker, 'stackify/workers/add_msg_worker'
2325
autoload :MsgsQueue, 'stackify/msgs_queue'
2426
autoload :LoggerClient, 'stackify/logger_client'
@@ -123,7 +125,7 @@ def run async = true
123125
end
124126

125127
def start_logging
126-
Thread.new { Stackify.logs_sender.start}
128+
msgs_queue
127129
end
128130

129131
def start_metrics

lib/stackify/authorization/authorizable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ module ClassMethods
1010
@@auth_lock = Mutex.new
1111
@@auth_client = nil
1212

13-
def authorize attempts=3, delay_time = 20
13+
def authorize attempts=3
1414
@@auth_lock.synchronize do
1515
return unless @@auth_client.nil?
1616
@@auth_client = Stackify::Authorizable::AuthorizationClient.new
17-
@@auth_client.auth attempts, delay_time
17+
@@auth_client.auth attempts
1818
end
1919
end
2020

@@ -53,7 +53,7 @@ def if_not_authorized failure_msg, &block
5353

5454
def response_string r
5555
return '' if r.nil?
56-
"Code: #{r.try(:code)}, Message: '#{r.try(:msg)}'"
56+
"Status: #{r.try(:status)}, Message: '#{r.try(:body)}'"
5757
end
5858

5959
end

lib/stackify/authorization/authorization_client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize
88
@worker = Stackify::AuthWorker.new
99
end
1010

11-
def auth attempts, delay_time= 20
11+
def auth attempts, delay_time= Stackify::ScheduleDelay.new
1212
task = auth_task attempts
1313
@worker.perform delay_time, task
1414
end
@@ -18,7 +18,7 @@ def auth_task attempts
1818
limit: 1,
1919
attempts: attempts,
2020
success_condition: lambda do |result|
21-
result.try(:code) == '200'
21+
result.try(:status) == 200
2222
end
2323
}
2424
Stackify::ScheduleTask.new properties do

lib/stackify/errors_governor.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
require 'digest'
2+
13
module Stackify
24
class ErrorsGovernor
3-
require 'digest'
45

56
def initialize purge_period=5
67
@history = {}
@@ -17,7 +18,7 @@ def can_send? ex
1718
history_entry = @history[key]
1819
if history_entry[:epoch_minute] == epoch_minute
1920
history_entry[:count] += 1
20-
answer = (history_entry[:count] <= Stackify.configuration.flood_limit) ? true : false
21+
answer = history_entry[:count] <= Stackify.configuration.flood_limit
2122
else
2223
@history[key]={
2324
epoch_minute: epoch_minute,

lib/stackify/http_client.rb

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'uri'
2-
require 'net/http'
3-
require 'net/https'
2+
require 'faraday'
3+
44
module Stackify
55
class HttpClient
66

@@ -16,32 +16,21 @@ class HttpClient
1616
def send_request uri, body, headers = HEADERS
1717
@errors = []
1818
begin
19-
https = get_https uri
20-
req = Net::HTTP::Post.new uri.path, initheader = headers
21-
req.body = body
19+
conn = Faraday.new(proxy: Stackify.configuration.proxy)
2220
Stackify.internal_log :debug, "============Request body=========================="
23-
Stackify.internal_log :debug, req.body
21+
Stackify.internal_log :debug, body
2422
Stackify.internal_log :debug, "=================================================="
25-
@response = https.request req
26-
rescue => ex
23+
@response = conn.post do |req|
24+
req.url URI(uri)
25+
req.headers = headers
26+
req.body = body
27+
end
28+
rescue => ex
2729
@errors << ex
2830
Stackify.log_internal_error('HttpClient: ' + ex.message+ ' Backtrace: '+ Stackify::Backtrace.backtrace_in_line(ex.backtrace))
2931
false
3032
end
3133
end
3234

33-
def get_https uri
34-
if Stackify.configuration.with_proxy
35-
https = Net::HTTP.new uri.host, uri.port,
36-
Stackify.configuration.proxy_host,
37-
Stackify.configuration.proxy_port,
38-
Stackify.configuration.proxy_user,
39-
Stackify.configuration.proxy_pass
40-
else
41-
https = Net::HTTP.new uri.host, uri.port
42-
end
43-
https.use_ssl = true
44-
https
45-
end
4635
end
4736
end

0 commit comments

Comments
 (0)