summaryrefslogtreecommitdiff
path: root/app/workers/wait_for_cluster_creation_worker.rb
blob: 5aa3bbdaa9d82d1797fbcf6b9342ae1192883a86 (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
class WaitForClusterCreationWorker
  include Sidekiq::Worker
  include ClusterQueue

  INITIAL_INTERVAL = 2.minutes
  EAGER_INTERVAL = 10.seconds
  TIMEOUT = 20.minutes

  def perform(cluster_id)
    Gcp::Cluster.find_by_id(cluster_id).try do |cluster|
      Ci::FetchGcpOperationService.new.execute(cluster) do |operation|
        case operation.status
        when 'RUNNING'
          if TIMEOUT < Time.now.utc - operation.start_time.to_time.utc
            return cluster.make_errored!("Cluster creation time exceeds timeout; #{TIMEOUT}")
          end

          WaitForClusterCreationWorker.perform_in(EAGER_INTERVAL, cluster.id)
        when 'DONE'
          Ci::FinalizeClusterCreationService.new.execute(cluster)
        else
          return cluster.make_errored!("Unexpected operation status; #{operation.status} #{operation.status_message}")
        end
      end
    end
  end
end