Sha256: bb6a8e6336e5152f79ce6c0c5a1f06083d507d1f5140223375513cda248a9455

Contents?: true

Size: 1.25 KB

Versions: 16

Compression:

Stored size: 1.25 KB

Contents

require 'sucker_punch'
require 'pact_broker/webhooks/service'
require 'pact_broker/logging'

module PactBroker
  module Webhooks
    class Job

      BACKOFF_TIMES = [10, 60, 120, 300, 600, 1200] #10 sec, 1 min, 2 min, 5 min, 10 min, 20 min => 38 minutes

      include SuckerPunch::Job
      include PactBroker::Logging

      def perform data
        @webhook = data[:webhook]
        @error_count = data[:error_count] || 0
        begin
          webhook_execution_result = PactBroker::Webhooks::Service.execute_webhook_now webhook
          reschedule_job unless webhook_execution_result.success?
        rescue StandardError => e
          handle_error e
        end
      end

      private

      attr_reader :webhook, :error_count

      def handle_error e
        log_error e
        reschedule_job
      end

      def reschedule_job
        case error_count
        when 0...BACKOFF_TIMES.size
          logger.debug "Re-enqeuing job for webhook #{webhook.uuid} to run in #{BACKOFF_TIMES[error_count]} seconds"
          Job.perform_in(BACKOFF_TIMES[error_count], {webhook: webhook, error_count: error_count+1})
        else
          logger.error "Failed to execute webhook #{webhook.uuid} after #{BACKOFF_TIMES.size} times."
        end
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
pact_broker-2.4.2 lib/pact_broker/webhooks/job.rb
pact_broker-2.4.1 lib/pact_broker/webhooks/job.rb
pact_broker-2.4.0 lib/pact_broker/webhooks/job.rb
pact_broker-2.3.0 lib/pact_broker/webhooks/job.rb
pact_broker-2.2.0 lib/pact_broker/webhooks/job.rb
pact_broker-2.1.1 lib/pact_broker/webhooks/job.rb
pact_broker-2.1.0 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.5 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.4 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.3 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.2 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.1 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.0 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.0.beta.8 lib/pact_broker/webhooks/job.rb
pact_broker-2.0.0.beta.7 lib/pact_broker/webhooks/job.rb
pact_broker-1.18.0 lib/pact_broker/webhooks/job.rb