Sha256: 8a85bbbd358142594e54b24595ff88c0ac6c97a5d71f1fbe4ab400632ae92ef8

Contents?: true

Size: 1.25 KB

Versions: 17

Compression:

Stored size: 1.25 KB

Contents

require 'sidekiq'

module PlatformSdk
  module Sidekiq

    class EcsTaskProtectionMiddleware
      ECS_AGENT_URI = ENV['ECS_AGENT_URI']
      STATE_ENDPOINT = "#{ECS_AGENT_URI}/task-protection/v1/state"
      @@mutex = Mutex.new
      @@active_jobs = 0

      def call(_, _, _)
        @@mutex.synchronize do
          @@active_jobs += 1
          set_task_protection(true) if @@active_jobs == 1
        end

        begin
          yield
        ensure
          @@mutex.synchronize do
            @@active_jobs -= 1
            set_task_protection(false) if @@active_jobs == 0
          end
        end
      end

      private

      def set_task_protection(state)
        begin
          uri = URI.parse(STATE_ENDPOINT)
          request = Net::HTTP::Put.new(uri)
          request['Content-Type'] = 'application/json'
          request.body = { 'ProtectionEnabled' => state }.to_json

          response = Net::HTTP.start(uri.hostname, uri.port) do |http|
            http.request(request)
          end

          unless response.is_a?(Net::HTTPSuccess)
            raise "Failed to update ECS task protection state: #{response.body}"
          end

        rescue StandardError => e
          raise "An error occurred: #{e.message}"
        end
      end
    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
strongmind-platform-sdk-3.22.1 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.22.0 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.8 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.7 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.6 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.5 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.4 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.3 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.2 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.1 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.21.0 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.20.3 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.20.2 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.20.1 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.20.0 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.19.40 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb
strongmind-platform-sdk-3.19.39 lib/platform_sdk/sidekiq/ecs_task_protection_middleware.rb