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