Skip to content

Commit 9a89d07

Browse files
committed
fix: ensure webhook dependencies are saved before executing
1 parent 2d7e85a commit 9a89d07

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

lib/pact_broker/webhooks/job.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Job
1111

1212
def perform data
1313
@data = data
14-
@triggered_webhook = data[:triggered_webhook]
14+
@triggered_webhook = PactBroker::Webhooks::TriggeredWebhook.find(id: data[:triggered_webhook].id)
1515
@error_count = data[:error_count] || 0
1616
begin
1717
webhook_execution_result = PactBroker::Webhooks::Service.execute_triggered_webhook_now triggered_webhook, execution_options

lib/pact_broker/webhooks/service.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def self.run_later webhooks, pact, event_name
100100
begin
101101
triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, RESOURCE_CREATION)
102102
logger.info "Scheduling job for #{webhook.description} with uuid #{webhook.uuid}"
103-
Job.perform_async triggered_webhook: triggered_webhook
103+
# Bit of a dodgey hack to make sure the request transaction has finished before we execute the webhook
104+
Job.perform_in(5, triggered_webhook: triggered_webhook)
104105
rescue StandardError => e
105106
log_error e
106107
end

spec/lib/pact_broker/webhooks/job_spec.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ module Webhooks
88
PactBroker.configuration.webhook_retry_schedule = [10, 60, 120, 300, 600, 1200]
99
allow(PactBroker::Webhooks::Service).to receive(:execute_triggered_webhook_now).and_return(result)
1010
allow(PactBroker::Webhooks::Service).to receive(:update_triggered_webhook_status)
11+
allow(PactBroker::Webhooks::TriggeredWebhook).to receive(:find).and_return(triggered_webhook)
1112
end
1213

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

18+
subject { Job.new.perform(triggered_webhook: triggered_webhook) }
19+
20+
it "reloads the TriggeredWebhook object to make sure it has a fresh copy" do
21+
expect(PactBroker::Webhooks::TriggeredWebhook).to receive(:find).with(id: 1)
22+
subject
23+
end
24+
1725
context "when the job succeeds" do
18-
subject { Job.new.perform(triggered_webhook: triggered_webhook) }
1926

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

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

54-
subject { Job.new.perform(triggered_webhook: triggered_webhook) }
55-
5659
it "reschedules the job in 10 seconds" do
5760
expect(Job).to receive(:perform_in).with(10, {triggered_webhook: triggered_webhook, error_count: 1})
5861
subject

spec/lib/pact_broker/webhooks/service_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Webhooks
4040
before do
4141
allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_provider_and_event_name).and_return(webhooks)
4242
allow_any_instance_of(PactBroker::Webhooks::Repository).to receive(:create_triggered_webhook).and_return(triggered_webhook)
43-
allow(Job).to receive(:perform_async)
43+
allow(Job).to receive(:perform_in)
4444
end
4545

4646
subject { Service.execute_webhooks pact, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED }
@@ -72,7 +72,7 @@ module Webhooks
7272

7373
context "when there is a scheduling error" do
7474
before do
75-
allow(Job).to receive(:perform_async).and_raise("an error")
75+
allow(Job).to receive(:perform_in).and_raise("an error")
7676
end
7777

7878
it "logs the error" do

0 commit comments

Comments
 (0)