Skip to content

Commit 341b850

Browse files
slashd0tdarinhoward
authored andcommitted
Rspec issue (#25)
* RT-2599 Fix the authorization error issue in Passenger * Disable the stackify api instance when a rake or spec command is executed
1 parent 7856832 commit 341b850

File tree

12 files changed

+60
-40
lines changed

12 files changed

+60
-40
lines changed

lib/stackify-api-ruby.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,27 @@ def internal_log level, msg
103103
Stackify.logger.send(level.downcase.to_sym, Stackify::INTERNAL_LOG_PREFIX){ msg }
104104
end
105105

106-
def run async = true
107-
if Stackify.is_valid?
108-
at_exit { make_remained_job }
109-
t1 = Thread.new { Stackify.authorize }
110-
case Stackify.configuration.mode
111-
when MODES[:both]
112-
t2 = start_logging
113-
t3 = start_metrics
114-
when MODES[:logging]
115-
t2 = start_logging
116-
when MODES[:metrics]
117-
t3 = start_metrics
118-
end
119-
unless async
106+
def run
107+
Stackify::Utils.is_api_enabled
108+
if Stackify.configuration.api_enabled
109+
if Stackify.is_valid?
110+
at_exit { make_remained_job }
111+
t1 = Thread.new { Stackify.authorize }
112+
case Stackify.configuration.mode
113+
when MODES[:both]
114+
t2 = start_logging
115+
t3 = start_metrics
116+
when MODES[:logging]
117+
t2 = start_logging
118+
when MODES[:metrics]
119+
t3 = start_metrics
120+
end
121+
120122
t1.join
121-
t2.join if t2
123+
t3.join if t3
124+
else
125+
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
122126
end
123-
else
124-
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
125127
end
126128
end
127129

lib/stackify/authorization/authorizable.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,5 @@ def response_string r
5656
return '' if r.nil?
5757
"Status: #{r.try(:status)}, Message: '#{r.try(:body)}'"
5858
end
59-
6059
end
61-
6260
end

lib/stackify/authorization/authorization_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def auth_task attempts
2222
end
2323
}
2424
Stackify::ScheduleTask.new properties do
25-
Stackify.internal_log :debug, 'AthorizationClient: trying to authorize...'
25+
Stackify.internal_log :debug, '[AuthorizationClient] trying to authorize...'
2626
send_request BASE_URI, Stackify::EnvDetails.instance.auth_info.to_json
2727
end
2828
end

lib/stackify/engine.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Stackify
33
class Engine < ::Rails::Engine
44

55
if Rails.version > '3.1'
6-
76
initializer 'Stackify set up of logger', group: :all do
87
::Rails.logger = ::Stackify::LoggerProxy.new ::Rails.logger
98
Stackify.run

lib/stackify/http_client.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ def send_request uri, body, headers = HEADERS
3131
false
3232
end
3333
end
34-
3534
end
3635
end

lib/stackify/logs_sender.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def send_logs_task attempts = nil, msgs
2323
Stackify::ScheduleTask.new properties do
2424
failure_msg = 'LogsSender: tried to send logs'
2525
Stackify.if_not_authorized failure_msg do
26-
Stackify.internal_log :info, 'LogsSender: trying to send logs to Stackify...'
26+
Stackify.internal_log :info, '[LogsSender] trying to send logs to Stackify...'
2727
send_request LOGS_URI, gather_and_pack_data(msgs).to_json
2828
end
2929
end

lib/stackify/metrics/metrics_client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize
1313
end
1414

1515
def start
16+
Stackify.internal_log :debug, '[MetricClient]: start'
1617
if Stackify::Utils.is_mode_on? Stackify::MODES[:metrics]
1718
worker = Stackify::Worker.new 'Metrics client - processing of metrics'
1819
Stackify.internal_log :debug, 'Metrics client: processing of metrics is started'

lib/stackify/msgs_queue.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@ module Stackify
22
class MsgsQueue < SizedQueue
33
include MonitorMixin
44

5+
attr_accessor :worker
6+
57
CHUNK_MIN_WEIGHT = 50
68
ERROR_SIZE = 10
79
LOG_SIZE = 1
810
DELAY_WAITING = 1
911

1012
def initialize
1113
super(Stackify.configuration.queue_max_size)
14+
start_worker
15+
end
16+
17+
alias :old_push :push
18+
19+
def start_worker
1220
if Stackify::Utils.is_mode_on? Stackify::MODES[:logging]
1321
@send_interval = ScheduleDelay.new
14-
worker = MsgsQueueWorker.new
22+
@worker = MsgsQueueWorker.new
1523
task = update_send_interval_task
16-
worker.async_perform @send_interval, task
24+
@worker.async_perform @send_interval, task
1725
else
1826
Stackify.internal_log :warn, '[MsgsQueue]: Logging is disabled at configuration!'
1927
end
2028
end
2129

22-
alias :old_push :push
23-
2430
def push_remained_msgs
31+
Stackify.internal_log :debug, "[MsgsQueue] push_remained_msgs() alive? = #{@worker.alive?}"
2532
wait_until_all_workers_will_add_msgs
2633
self.synchronize do
27-
Stackify.internal_log :info, 'All remained logs are going to be sent'
34+
Stackify.internal_log :info, '[MsgsQueue] All remained logs are going to be sent'
2835
Stackify.shutdown_all
2936
if self.length > 0
3037
Stackify.logs_sender.send_logs(pop_all)
@@ -34,6 +41,11 @@ def push_remained_msgs
3441
end
3542

3643
def add_msg msg
44+
Stackify.internal_log :debug, "[MsgsQueue] add_msg() Is worker <#{@worker.name}> alive? = #{@worker.alive?}"
45+
if !@worker.alive?
46+
start_worker
47+
Stackify.internal_log :debug, "[MsgsQueue] add_msg() Newly created worker <#{@worker.name}>"
48+
end
3749
self.synchronize do
3850
Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
3951
old_push(msg)
@@ -73,7 +85,6 @@ def update_send_interval_task
7385
Stackify::ScheduleTask.new properties do
7486
processed_count = calculate_processed_msgs_count
7587
i = @send_interval.update_by_sent_num! processed_count
76-
Stackify.internal_log :debug, "MsgsQueue: send_interval is updated to #{i}"
7788
i
7889
end
7990
end
@@ -104,7 +115,6 @@ def push_one_chunk
104115
break
105116
end
106117
end
107-
108118
Stackify.logs_sender.send_logs(chunk) if chunk.length > 0
109119
chunk_weight
110120
end

lib/stackify/utils/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Stackify
33
class Configuration
44

55
attr_accessor :api_key, :app_name, :app_location, :env, :log_level, :logger,
6-
:proxy, :mode, :base_api_url
6+
:proxy, :mode, :base_api_url, :api_enabled
77

88
attr_reader :errors, :send_interval, :flood_limit, :queue_max_size
99

@@ -15,6 +15,7 @@ def initialize
1515
@flood_limit = 100
1616
@queue_max_size = 10000
1717
@send_interval = 60
18+
@api_enabled = true
1819
@log_level = :info
1920
@mode = MODES[:both]
2021
@logger = Logger.new(STDOUT)

lib/stackify/utils/methods.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@ def self.rounded_current_time
1010
end
1111

1212
def self.is_mode_on? mode
13-
Stackify.configuration.mode == Stackify::MODES[:both] || Stackify.configuration.mode == mode
13+
Stackify.configuration.mode = mode || Stackify::MODES[:both]
1414
end
1515

1616
def self.do_only_if_authorized_and_mode_is_on mode, &block
17-
if Stackify.authorized?
18-
if is_mode_on? mode
19-
yield
17+
if Stackify.configuration.api_enabled
18+
if Stackify.authorized?
19+
if is_mode_on? mode
20+
yield
21+
else
22+
Stackify.internal_log :warn, "#{caller[0]}: Skipped because mode - #{mode.to_s} is disabled at configuration"
23+
end
2024
else
21-
Stackify.internal_log :warn, "#{caller[0]}: Skipped because mode - #{mode.to_s} is disabled at configuration"
25+
Stackify.internal_log :warn, "#{caller[0]}: Skipped due to authorization failure"
2226
end
23-
else
24-
Stackify.internal_log :warn, "#{caller[0]}: Skipped due to authorization failure"
2527
end
2628
end
27-
end
29+
30+
def self.is_api_enabled
31+
exclude = %w/rake rspec irb/
32+
cmd = $PROGRAM_NAME.to_s.split('/').pop
33+
found = exclude.select{|e| e =~ /#{cmd}/i}
34+
Stackify.configuration.api_enabled = false if found.count > 0
35+
end
36+
end

0 commit comments

Comments
 (0)