Commit cb5f4d0c authored by Tiago Botelho's avatar Tiago Botelho

Refactors TimedLogger to be more OOP compliant

Adds a #full_message method so that external classes
do not have access to the state of the logger.

Adds a #append_message to always append to the array in-place
parent 084a8b61
...@@ -47,7 +47,7 @@ module Gitlab ...@@ -47,7 +47,7 @@ module Gitlab
@protocol = protocol @protocol = protocol
@logger = logger @logger = logger
@logger.log << "Running checks for ref: #{@branch_name || @tag_name}" @logger.append_message("Running checks for ref: #{@branch_name || @tag_name}")
end end
def exec(skip_commits_check: false) def exec(skip_commits_check: false)
......
...@@ -5,15 +5,18 @@ module Gitlab ...@@ -5,15 +5,18 @@ module Gitlab
class TimedLogger class TimedLogger
TimeoutError = Class.new(StandardError) TimeoutError = Class.new(StandardError)
attr_reader :start_time attr_reader :start_time, :header, :log, :timeout
attr_accessor :log, :timeout
def initialize(start_time: Time.now, log: [], timeout:) def initialize(start_time: Time.now, log: [], timeout:, header: "")
@start_time = start_time @start_time = start_time
@timeout = timeout @timeout = timeout
@header = header
@log = log @log = log
end end
# Adds trace of method being tracked with
# the correspondent time it took to run it
#
def log_timed(log_message, start = Time.now) def log_timed(log_message, start = Time.now)
check_timeout_reached check_timeout_reached
...@@ -21,12 +24,12 @@ module Gitlab ...@@ -21,12 +24,12 @@ module Gitlab
yield yield
log << log_message + time_suffix_message(start: start) append_message(log_message + time_suffix_message(start: start))
rescue GRPC::DeadlineExceeded, TimeoutError rescue GRPC::DeadlineExceeded, TimeoutError
args = { cancelled: true } args = { cancelled: true }
args[:start] = start if timed args[:start] = start if timed
log << log_message + time_suffix_message(args) append_message(log_message + time_suffix_message(args))
raise TimeoutError raise TimeoutError
end end
...@@ -41,6 +44,15 @@ module Gitlab ...@@ -41,6 +44,15 @@ module Gitlab
(start_time + timeout.seconds) - Time.now (start_time + timeout.seconds) - Time.now
end end
def full_message
header + log.join("\n")
end
# We always want to append in-place on the log
def append_message(message)
log << message
end
private private
def time_expired? def time_expired?
......
...@@ -27,6 +27,12 @@ module Gitlab ...@@ -27,6 +27,12 @@ module Gitlab
cannot_push_to_read_only: "You can't push code to a read-only GitLab instance." cannot_push_to_read_only: "You can't push code to a read-only GitLab instance."
}.freeze }.freeze
LOG_HEADER = <<~MESSAGE
Push operation timed out
Timing information for debugging purposes:
MESSAGE
INTERNAL_TIMEOUT = 50.seconds.freeze INTERNAL_TIMEOUT = 50.seconds.freeze
DOWNLOAD_COMMANDS = %w{git-upload-pack git-upload-archive}.freeze DOWNLOAD_COMMANDS = %w{git-upload-pack git-upload-archive}.freeze
PUSH_COMMANDS = %w{git-receive-pack}.freeze PUSH_COMMANDS = %w{git-receive-pack}.freeze
...@@ -46,7 +52,7 @@ module Gitlab ...@@ -46,7 +52,7 @@ module Gitlab
end end
def check(cmd, changes) def check(cmd, changes)
@logger = Checks::TimedLogger.new(timeout: INTERNAL_TIMEOUT) @logger = Checks::TimedLogger.new(timeout: INTERNAL_TIMEOUT, header: LOG_HEADER)
@changes = changes @changes = changes
check_protocol! check_protocol!
...@@ -284,13 +290,7 @@ module Gitlab ...@@ -284,13 +290,7 @@ module Gitlab
change_access.exec change_access.exec
rescue Checks::TimedLogger::TimeoutError rescue Checks::TimedLogger::TimeoutError
header = <<~MESSAGE raise TimeoutError, logger.full_message
Push operation timed out
Timing information for debugging purposes:
MESSAGE
raise TimeoutError, header + logger.log.join("\n")
end end
def deploy_key def deploy_key
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment