Welcome to mirror list, hosted at ThFree Co, Russian Federation.

upload_and_cleanup_reports.rb « memory « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27d94df478cc81504ddd628afd393db47fcbb4f9 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

module Gitlab
  module Memory
    class UploadAndCleanupReports
      DEFAULT_SLEEP_TIME_SECONDS = 900 # 15 minutes

      def initialize(
        uploader:,
        reports_path:,
        logger:,
        sleep_time_seconds: ENV['GITLAB_DIAGNOSTIC_REPORTS_UPLOADER_SLEEP_S']&.to_i || DEFAULT_SLEEP_TIME_SECONDS)

        @uploader = uploader
        @reports_path = reports_path
        @sleep_time_seconds = sleep_time_seconds
        @alive = true
        @logger = logger
      end

      attr_reader :uploader, :reports_path, :sleep_time_seconds, :logger

      def call
        log_started

        loop do
          sleep(sleep_time_seconds)

          files_to_process.each { |path| upload_and_cleanup!(path) }
        end
      end

      private

      def upload_and_cleanup!(path)
        uploader.upload(path)
      rescue StandardError, Errno::ENOENT => error
        log_exception(error)
      ensure
        cleanup!(path)
      end

      def cleanup!(path)
        File.unlink(path) if File.exist?(path)
      rescue Errno::ENOENT
        # Path does not exist: Ignore. We already check `File.exist?`. Rescue to be extra safe.
      end

      def files_to_process
        Dir.entries(reports_path)
          .map { |path| File.join(reports_path, path) }
          .select { |path| File.file?(path) }
      end

      def log_started
        logger.info(log_labels.merge(perf_report_status: "started"))
      end

      def log_exception(error)
        logger.error(log_labels.merge(perf_report_status: "error", error: error.message))
      end

      def log_labels
        {
          message: "Diagnostic reports",
          class: self.class.name,
          pid: $$
        }
      end
    end
  end
end