lab.nexedi.com will be down from Thursday, 20 March 2025, 07:30:00 UTC for a duration of approximately 2 hours

base_url_finder.rb 1.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# frozen_string_literal: true

module Security
  module TrainingProviders
    class BaseUrlFinder
      include Gitlab::Utils::StrongMemoize
      include ReactiveCaching

      self.reactive_cache_refresh_interval = 1.minute
      self.reactive_cache_lifetime = 10.minutes
      self.reactive_cache_work_type = :external_dependency

Subashis's avatar
Subashis committed
13 14
      def initialize(project, provider, identifier_external_id)
        @project = project
15
        @provider = provider
Subashis's avatar
Subashis committed
16
        @identifier_external_id = identifier_external_id
17 18 19 20 21 22 23 24 25 26 27
      end

      def execute
        if response_url.nil?
          { name: provider.name, url: response_url, status: "pending" }
        else
          { name: provider.name, url: response_url[:url], status: "completed" } if response_url[:url]
        end
      end

      def self.from_cache(id)
Subashis's avatar
Subashis committed
28
        project_id, provider_id, identifier_external_id = id.split('-')
29 30 31 32

        project = Project.find(project_id)
        provider = ::Security::TrainingProvider.find(provider_id)

Subashis's avatar
Subashis committed
33
        new(project, provider, identifier_external_id)
34 35 36 37
      end

      private

Subashis's avatar
Subashis committed
38
      attr_reader :project, :provider, :identifier_external_id
39 40 41 42 43 44 45 46 47 48 49 50 51 52

      def response_url
        strong_memoize(:response_url) do
          with_reactive_cache(full_url) {|data| data}
        end
      end

      def full_url
        raise NotImplementedError, 'full_url must be overwritten to return training url'
      end

      # Required for ReactiveCaching; Usage overridden by
      # self.reactive_cache_worker_finder
      def id
Subashis's avatar
Subashis committed
53
        "#{project.id}-#{provider.id}-#{identifier_external_id}"
54 55 56 57
      end
    end
  end
end