summaryrefslogtreecommitdiff
path: root/app/services/clusters/applications/schedule_update_service.rb
blob: 4f130f76b870c3a01ca931fa18d1eda90363a315 (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
# frozen_string_literal: true

module Clusters
  module Applications
    class ScheduleUpdateService
      BACKOFF_DELAY = 2.minutes

      attr_accessor :application, :project

      def initialize(application, project)
        @application = application
        @project = project
      end

      def execute
        return unless application
        return unless application.managed_prometheus?

        if recently_scheduled?
          worker_class.perform_in(BACKOFF_DELAY, application.name, application.id, project.id, Time.current)
        else
          worker_class.perform_async(application.name, application.id, project.id, Time.current)
        end
      end

      private

      def worker_class
        ::ClusterUpdateAppWorker
      end

      def recently_scheduled?
        return false unless application.last_update_started_at

        application.last_update_started_at.utc >= Time.current.utc - BACKOFF_DELAY
      end
    end
  end
end