Skip to content

Commit

Permalink
fix: ensure webhook dependencies are saved before executing
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 14, 2018
1 parent 2d7e85a commit 9a89d07
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Job

def perform data
@data = data
@triggered_webhook = data[:triggered_webhook]
@triggered_webhook = PactBroker::Webhooks::TriggeredWebhook.find(id: data[:triggered_webhook].id)
@error_count = data[:error_count] || 0
begin
webhook_execution_result = PactBroker::Webhooks::Service.execute_triggered_webhook_now triggered_webhook, execution_options
Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def self.run_later webhooks, pact, event_name
begin
triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, RESOURCE_CREATION)
logger.info "Scheduling job for #{webhook.description} with uuid #{webhook.uuid}"
Job.perform_async triggered_webhook: triggered_webhook
# Bit of a dodgey hack to make sure the request transaction has finished before we execute the webhook
Job.perform_in(5, triggered_webhook: triggered_webhook)
rescue StandardError => e
log_error e
end
Expand Down
13 changes: 8 additions & 5 deletions spec/lib/pact_broker/webhooks/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ module Webhooks
PactBroker.configuration.webhook_retry_schedule = [10, 60, 120, 300, 600, 1200]
allow(PactBroker::Webhooks::Service).to receive(:execute_triggered_webhook_now).and_return(result)
allow(PactBroker::Webhooks::Service).to receive(:update_triggered_webhook_status)
allow(PactBroker::Webhooks::TriggeredWebhook).to receive(:find).and_return(triggered_webhook)
end

let(:triggered_webhook) { instance_double("PactBroker::Webhooks::TriggeredWebhook", webhook_uuid: '1234', id: 1) }
let(:result) { instance_double("PactBroker::Domain::WebhookExecutionResult", success?: success)}
let(:success) { true }

subject { Job.new.perform(triggered_webhook: triggered_webhook) }

it "reloads the TriggeredWebhook object to make sure it has a fresh copy" do
expect(PactBroker::Webhooks::TriggeredWebhook).to receive(:find).with(id: 1)
subject
end

context "when the job succeeds" do
subject { Job.new.perform(triggered_webhook: triggered_webhook) }

it "does not reschedule the job" do
expect(Job).to_not receive(:perform_in)
Expand All @@ -34,8 +41,6 @@ module Webhooks
allow(PactBroker::Webhooks::Service).to receive(:execute_triggered_webhook_now).and_raise("an error")
end

subject { Job.new.perform(triggered_webhook: triggered_webhook) }

it "reschedules the job in 10 seconds" do
expect(Job).to receive(:perform_in).with(10, {triggered_webhook: triggered_webhook, error_count: 1})
subject
Expand All @@ -51,8 +56,6 @@ module Webhooks
context "when the webhook execution result is not successful for the first time" do
let(:success) { false }

subject { Job.new.perform(triggered_webhook: triggered_webhook) }

it "reschedules the job in 10 seconds" do
expect(Job).to receive(:perform_in).with(10, {triggered_webhook: triggered_webhook, error_count: 1})
subject
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/webhooks/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module Webhooks
before do
allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_provider_and_event_name).and_return(webhooks)
allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:create_triggered_webhook).and_return(triggered_webhook)
allow(Job).to receive(:perform_async)
allow(Job).to receive(:perform_in)
end

subject { Service.execute_webhooks pact, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED }
Expand Down Expand Up @@ -72,7 +72,7 @@ module Webhooks

context "when there is a scheduling error" do
before do
allow(Job).to receive(:perform_async).and_raise("an error")
allow(Job).to receive(:perform_in).and_raise("an error")
end

it "logs the error" do
Expand Down

0 comments on commit 9a89d07

Please sign in to comment.