- if running_job[:bytes_logged] > Rails.configuration.crunch_limit_log_bytes_per_job
- # Don't log anything if the hard cap has already been exceeded
- return false
- end
-
- now = Time.now
- throttle_period = Rails.configuration.crunch_log_throttle_period
-
- if running_job[:log_throttle_bytes_skipped] > 0
- # We've skipped some log in the current time period already, so continue to
- # skip the log
- running_job[:log_throttle_bytes_skipped] += line.size
- return false
- end
-
- # Count lines and bytes logged in this period, and total bytes logged for the job
- running_job[:log_throttle_lines_so_far] += 1
- running_job[:log_throttle_bytes_so_far] += line.size
- running_job[:bytes_logged] += line.size
-
- if running_job[:log_throttle_bytes_so_far] > Rails.configuration.crunch_log_throttle_bytes or
- running_job[:log_throttle_lines_so_far] > Rails.configuration.crunch_log_throttle_lines
- # We've exceeded the per-period throttle, so start skipping
- running_job[:log_throttle_bytes_skipped] += line.size
-
- # Replace log line with a message about skipping the log
- remaining_time = throttle_period - (now - running_job[:log_throttle_timestamp])
- if running_job[:log_throttle_bytes_so_far] > Rails.configuration.crunch_log_throttle_bytes
- line.replace "Exceeded rate #{Rails.configuration.crunch_log_throttle_bytes} bytes per #{throttle_period} seconds (crunch_log_throttle_bytes), logging will be silenced for the next #{remaining_time.round} seconds\n"
- else
- line.replace "Exceeded rate #{Rails.configuration.crunch_log_throttle_lines} lines per #{throttle_period} seconds (crunch_log_throttle_lines), logging will be silenced for the next #{remaining_time.round} seconds\n"
+ message = false
+ linesize = line.size
+ if running_job[:log_throttle_is_open]
+ running_job[:log_throttle_lines_so_far] += 1
+ running_job[:log_throttle_bytes_so_far] += linesize
+ running_job[:bytes_logged] += linesize
+
+ if (running_job[:bytes_logged] >
+ Rails.configuration.crunch_limit_log_bytes_per_job)
+ message = "Exceeded log limit #{Rails.configuration.crunch_limit_log_bytes_per_job} bytes (crunch_limit_log_bytes_per_job). Log will be truncated."
+ running_job[:log_throttle_reset_time] = Time.now + 100.years
+ running_job[:log_throttle_is_open] = false
+
+ elsif (running_job[:log_throttle_bytes_so_far] >
+ Rails.configuration.crunch_log_throttle_bytes)
+ remaining_time = running_job[:log_throttle_reset_time] - Time.now
+ message = "Exceeded rate #{Rails.configuration.crunch_log_throttle_bytes} bytes per #{Rails.configuration.crunch_log_throttle_period} seconds (crunch_log_throttle_bytes). Logging will be silenced for the next #{remaining_time.round} seconds.\n"
+ running_job[:log_throttle_is_open] = false
+
+ elsif (running_job[:log_throttle_lines_so_far] >
+ Rails.configuration.crunch_log_throttle_lines)
+ remaining_time = running_job[:log_throttle_reset_time] - Time.now
+ message = "Exceeded rate #{Rails.configuration.crunch_log_throttle_lines} lines per #{Rails.configuration.crunch_log_throttle_period} seconds (crunch_log_throttle_lines), logging will be silenced for the next #{remaining_time.round} seconds.\n"
+ running_job[:log_throttle_is_open] = false