Skip to content

Commit

Permalink
fix: high cpu load on idle
Browse files Browse the repository at this point in the history
  • Loading branch information
Napolskih committed Nov 2, 2017
1 parent 229ed9f commit 46b03be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/treasury/bg_executor/bg_executor_daemon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def terminate

while($running)
daemon.execute_job
sleep 0.1
sleep 5
end

puts 'Exit'
12 changes: 9 additions & 3 deletions lib/treasury/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Worker
STATE_STOPPED = 'stopped'.freeze

REFRESH_FIELDS_LIST_PERIOD = Rails.env.staging? || !Rails.env.production? ? 1.minute : 1.minute
IDLE_MAX_LAG = 5.minutes
PROCESS_LOOP_NORMAL_SLEEP_TIME = Rails.env.test? ? 0 : 0.05.seconds
IDLE_MAX_LAG = 2.minutes
PROCESS_LOOP_NORMAL_SLEEP_TIME = Rails.env.test? ? 0 : 0.seconds
PROCESS_LOOP_IDLE_SLEEP_TIME = Rails.env.test? ? 0 : 5.seconds

LOGGER_FILE_NAME = "#{ROOT_LOGGER_DIR}/workers/%{name}_worker".freeze
Expand Down Expand Up @@ -68,6 +68,8 @@ def process_fields

idle = true
total_lag = 0
processed_queues = 0

@processing_fields.each do |field|
begin
begin
Expand All @@ -89,6 +91,7 @@ def process_fields

total_lag += @consumers_info[processor.consumer_name].try(:[], :seconds_lag).to_i
idle &&= events_processed.zero?
processed_queues += 1
end
rescue Pgq::Errors::QueueOrSubscriberNotFoundError, Processors::Errors::InconsistencyDataError => e
# обработка исключений, требующих переиницилизации поля
Expand All @@ -103,7 +106,10 @@ def process_fields
end
end

idle &&= total_lag < IDLE_MAX_LAG unless Rails.env.test?
unless Rails.env.test? || processed_queues.zero?
idle &&= total_lag / processed_queues < IDLE_MAX_LAG
end

!idle
end

Expand Down

0 comments on commit 46b03be

Please sign in to comment.