Skip to content

Commit

Permalink
feat(webhook status): moved webhook retry schedule to configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 6, 2017
1 parent 477bd57 commit f2d92f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Configuration
attr_accessor :use_case_sensitive_resource_names, :order_versions_by_date
attr_accessor :semver_formats
attr_accessor :enable_badge_resources, :shields_io_base_url
attr_accessor :webhook_retry_schedule
attr_writer :logger

def initialize
Expand Down Expand Up @@ -48,6 +49,7 @@ def self.default_configuration
# consistently extract an orderable object from the consumer application version number.
config.order_versions_by_date = false
config.semver_formats = ["%M.%m.%p%s%d","%M.%m", "%M"]
config.webhook_retry_schedule = [10, 60, 120, 300, 600, 1200] #10 sec, 1 min, 2 min, 5 min, 10 min, 20 min => 38 minutes
config
end

Expand Down
18 changes: 12 additions & 6 deletions lib/pact_broker/webhooks/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ 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

Expand Down Expand Up @@ -45,23 +43,31 @@ def handle_failure
reschedule_job
update_triggered_webhook_status TriggeredWebhook::STATUS_RETRYING
else
logger.error "Failed to execute webhook #{triggered_webhook.webhook_uuid} after #{BACKOFF_TIMES.size} times."
logger.error "Failed to execute webhook #{triggered_webhook.webhook_uuid} after #{retry_schedule.size} times."
update_triggered_webhook_status TriggeredWebhook::STATUS_FAILURE
end
end

def reschedule_job?
error_count < BACKOFF_TIMES.size
error_count < retry_schedule.size
end

def reschedule_job
logger.debug "Re-enqeuing job for webhook #{triggered_webhook.webhook_uuid} to run in #{BACKOFF_TIMES[error_count]} seconds"
Job.perform_in(BACKOFF_TIMES[error_count], @data.merge(error_count: error_count+1))
logger.debug "Re-enqeuing job for webhook #{triggered_webhook.webhook_uuid} to run in #{retry_schedule[error_count]} seconds"
Job.perform_in(backoff_time, @data.merge(error_count: error_count+1))
end

def update_triggered_webhook_status status
PactBroker::Webhooks::Service.update_triggered_webhook_status triggered_webhook, status
end

def backoff_time
retry_schedule[error_count]
end

def retry_schedule
PactBroker.configuration.webhook_retry_schedule
end
end
end
end

0 comments on commit f2d92f3

Please sign in to comment.