From cb5f4d0cad520629aecaa838ddf1e84d7265ff49 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Thu, 25 Oct 2018 09:49:59 +0100 Subject: 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 --- lib/gitlab/checks/change_access.rb | 2 +- lib/gitlab/checks/timed_logger.rb | 22 +++++++++++++++++----- lib/gitlab/git_access.rb | 16 ++++++++-------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/gitlab/checks/change_access.rb b/lib/gitlab/checks/change_access.rb index 52a72de3b15..7036ae9191d 100644 --- a/lib/gitlab/checks/change_access.rb +++ b/lib/gitlab/checks/change_access.rb @@ -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) diff --git a/lib/gitlab/checks/timed_logger.rb b/lib/gitlab/checks/timed_logger.rb index cbb079a5383..11c08429d3d 100644 --- a/lib/gitlab/checks/timed_logger.rb +++ b/lib/gitlab/checks/timed_logger.rb @@ -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? diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 713a98bb556..1da466cbfd6 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -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 -- cgit v1.2.1