Skip to content

Commit

Permalink
feat(webhooks): gracefully handle scenario where triggered webhook ha…
Browse files Browse the repository at this point in the history
…s been deleted while webhook was being executed
  • Loading branch information
bethesque committed Jun 18, 2019
1 parent fe25acc commit 052055d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 11 additions & 5 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
require 'pact_broker/webhooks/triggered_webhook'
require 'pact_broker/webhooks/latest_triggered_webhook'
require 'pact_broker/webhooks/execution'
require 'pact_broker/logging'

module PactBroker
module Webhooks

class Repository
include PactBroker::Logging

include Repositories

Expand Down Expand Up @@ -125,10 +126,15 @@ def update_triggered_webhook_status triggered_webhook, status
end

def create_execution triggered_webhook, webhook_execution_result
Execution.create(
triggered_webhook: triggered_webhook,
success: webhook_execution_result.success?,
logs: webhook_execution_result.logs)
# TriggeredWebhook may have been deleted since the webhook execution started
if TriggeredWebhook.where(id: triggered_webhook.id).any?
Execution.create(
triggered_webhook: triggered_webhook,
success: webhook_execution_result.success?,
logs: webhook_execution_result.logs)
else
logger.info("Could not save webhook execution for triggered webhook with id #{triggered_webhook.id} as it has been delete from the database")
end
end

def delete_triggered_webhooks_by_pacticipant pacticipant
Expand Down
17 changes: 16 additions & 1 deletion spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ module Webhooks

let(:webhook_domain) { Repository.new.find_by_uuid td.webhook.uuid }
let(:webhook_execution_result) { instance_double("PactBroker::Domain::WebhookExecutionResult", success?: true, logs: "logs") }
let(:repository) { Repository.new }

subject { Repository.new.create_execution td.triggered_webhook, webhook_execution_result }
subject { repository.create_execution td.triggered_webhook, webhook_execution_result }

it "saves a new webhook execution " do
expect { subject }.to change { Execution.count }.by(1)
Expand All @@ -444,6 +445,20 @@ module Webhooks
it "sets the logs" do
expect(subject.logs).to eq "logs"
end

context "when the triggered webhook has been deleted in the meantime" do
before do
TriggeredWebhook.where(id: td.triggered_webhook.id).delete
allow(repository).to receive(:logger).and_return(logger)
end

let(:logger) { double('logger') }

it "just logs the error" do
expect(logger).to receive(:info).with(/triggered webhook with id #{td.triggered_webhook.id}/)
subject
end
end
end

describe "delete_triggered_webhooks_by_webhook_uuid" do
Expand Down

0 comments on commit 052055d

Please sign in to comment.