summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/queue_error_handling_concern.rb
blob: 7d35426d029b458d2557b9755c42761764765cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

module Gitlab
  module Database
    module QueueErrorHandlingConcern
      extend ActiveSupport::Concern

      MAX_LAST_ERROR_LENGTH = 10_000

      included do
        validates :last_error, length: { maximum: MAX_LAST_ERROR_LENGTH },
          if: ->(record) { record.respond_to?(:last_error) }
      end

      def handle_exception!(error)
        transaction do
          increment!(:attempts)
          update!(last_error: format_last_error(error))
        end
      end

      private

      def format_last_error(error)
        [error.message]
          .concat(error.backtrace)
          .join("\n")
          .truncate(MAX_LAST_ERROR_LENGTH)
      end
    end
  end
end