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
@protocol = protocol
@logger = logger
@logger.log << "Running checks for ref: #{@branch_name || @tag_name}"
@logger.append_message("Running checks for ref: #{@branch_name || @tag_name}")
end
def exec(skip_commits_check: false)
......
......@@ -5,15 +5,18 @@ module Gitlab
class TimedLogger
TimeoutError = Class.new(StandardError)
attr_reader :start_time
attr_accessor :log, :timeout
attr_reader :start_time, :header, :log, :timeout
def initialize(start_time: Time.now, log: [], timeout:)
def initialize(start_time: Time.now, log: [], timeout:, header: "")
@start_time = start_time
@timeout = timeout
@header = header
@log = log
end
# Adds trace of method being tracked with
# the correspondent time it took to run it
#
def log_timed(log_message, start = Time.now)
check_timeout_reached
......@@ -21,12 +24,12 @@ module Gitlab
yield
log << log_message + time_suffix_message(start: start)
append_message(log_message + time_suffix_message(start: start))
rescue GRPC::DeadlineExceeded, TimeoutError
args = { cancelled: true }
args[:start] = start if timed
log << log_message + time_suffix_message(args)
append_message(log_message + time_suffix_message(args))
raise TimeoutError
end
......@@ -41,6 +44,15 @@ module Gitlab
(start_time + timeout.seconds) - Time.now
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
def time_expired?
......
......@@ -27,6 +27,12 @@ module Gitlab
cannot_push_to_read_only: "You can't push code to a read-only GitLab instance."
}.freeze
LOG_HEADER = <<~MESSAGE
Push operation timed out
Timing information for debugging purposes:
MESSAGE
INTERNAL_TIMEOUT = 50.seconds.freeze
DOWNLOAD_COMMANDS = %w{git-upload-pack git-upload-archive}.freeze
PUSH_COMMANDS = %w{git-receive-pack}.freeze
......@@ -46,7 +52,7 @@ module Gitlab
end
def check(cmd, changes)
@logger = Checks::TimedLogger.new(timeout: INTERNAL_TIMEOUT)
@logger = Checks::TimedLogger.new(timeout: INTERNAL_TIMEOUT, header: LOG_HEADER)
@changes = changes
check_protocol!
......@@ -284,13 +290,7 @@ module Gitlab
change_access.exec
rescue Checks::TimedLogger::TimeoutError
header = <<~MESSAGE
Push operation timed out
Timing information for debugging purposes:
MESSAGE
raise TimeoutError, header + logger.log.join("\n")
raise TimeoutError, logger.full_message
end
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