diff --git a/lib/pact_broker/webhooks/job.rb b/lib/pact_broker/webhooks/job.rb index a01117efc..b395a555f 100644 --- a/lib/pact_broker/webhooks/job.rb +++ b/lib/pact_broker/webhooks/job.rb @@ -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 diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index b767374b2..5f02b7cbe 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -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 diff --git a/spec/lib/pact_broker/webhooks/job_spec.rb b/spec/lib/pact_broker/webhooks/job_spec.rb index cca213f01..387bb771b 100644 --- a/spec/lib/pact_broker/webhooks/job_spec.rb +++ b/spec/lib/pact_broker/webhooks/job_spec.rb @@ -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) @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/webhooks/service_spec.rb b/spec/lib/pact_broker/webhooks/service_spec.rb index e5b9ab5c5..abd062c31 100644 --- a/spec/lib/pact_broker/webhooks/service_spec.rb +++ b/spec/lib/pact_broker/webhooks/service_spec.rb @@ -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 } @@ -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