Skip to content

Commit

Permalink
feat(webhook status): set any triggered webhooks in 'retrying' status…
Browse files Browse the repository at this point in the history
… to 'failed' on startup
  • Loading branch information
bethesque committed Sep 12, 2017
1 parent 9f46d86 commit 1f2305b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
7 changes: 4 additions & 3 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize &block
@configuration = PactBroker.configuration
yield configuration
post_configure
migrate_database
prepare_database
prepare_app
end

Expand All @@ -48,13 +48,15 @@ def post_configure
configure_sucker_punch
end

def migrate_database
def prepare_database
if configuration.auto_migrate_db
logger.info "Migrating database"
PactBroker::DB.run_migrations configuration.database_connection
else
logger.info "Skipping database migrations"
end
require 'pact_broker/webhooks/service'
PactBroker::Webhooks::Service.fail_retrying_triggered_webhooks
end

def configure_database_connection
Expand Down Expand Up @@ -135,6 +137,5 @@ def running_app
@app_builder
end
end

end
end
4 changes: 4 additions & 0 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def find_latest_triggered_webhooks consumer, provider
.values
.collect(&:last)
end

def fail_retrying_triggered_webhooks
TriggeredWebhook.retrying.update(status: TriggeredWebhook::STATUS_FAILURE)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def self.run_later webhooks, pact
def self.find_latest_triggered_webhooks consumer, provider
webhook_repository.find_latest_triggered_webhooks consumer, provider
end

def self.fail_retrying_triggered_webhooks
webhook_repository.fail_retrying_triggered_webhooks
end
end
end
end
16 changes: 16 additions & 0 deletions lib/pact_broker/webhooks/triggered_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ class TriggeredWebhook < Sequel::Model(:triggered_webhooks)

dataset_module do
include PactBroker::Repositories::Helpers

def retrying
where(status: STATUS_RETRYING)
end

def successful
where(status: STATUS_SUCCESS)
end

def failed
where(status: STATUS_FAILURE)
end

def not_run
where(status: STATUS_NOT_RUN)
end
end

associate(:one_to_many, :webhook_executions, :class => "PactBroker::Webhooks::Execution", :key => :triggered_webhook_id, :primary_key => :id, :order => :id)
Expand Down
20 changes: 12 additions & 8 deletions spec/lib/pact_broker/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
module PactBroker
describe App do

class TestApp < PactBroker::App
before do
allow(PactBroker::DB).to receive(:run_migrations)
end

class TestApp < PactBroker::App
def configure_database_connection
# do nothing
end

def migrate_database
# do nothing
# do nothing so we don't screw up our test connection
end

end

let(:app) do
Expand All @@ -21,6 +19,13 @@ def migrate_database
end
end

describe "on start up" do
it "fails any retrying triggered webhooks left over when the app shut down" do
expect(PactBroker::Webhooks::Service).to receive(:fail_retrying_triggered_webhooks)
app
end
end

it "adds the X-Pact-Broker-Version header" do
get "/"
expect(last_response.headers['X-Pact-Broker-Version']).to match /\d/
Expand Down Expand Up @@ -208,6 +213,5 @@ def self.calls
expect { subject }.to_not change { PactBroker::Domain::Pacticipant.count }
end
end

end
end
19 changes: 19 additions & 0 deletions spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,25 @@ module Webhooks
end
end
end

describe "fail_retrying_triggered_webhooks" do
before do
td.create_pact_with_hierarchy
.create_webhook
.create_triggered_webhook(status: TriggeredWebhook::STATUS_RETRYING)
.create_triggered_webhook(status: TriggeredWebhook::STATUS_SUCCESS)
.create_triggered_webhook(status: TriggeredWebhook::STATUS_NOT_RUN)
.create_triggered_webhook(status: TriggeredWebhook::STATUS_FAILURE)
end

it "sets the triggered_webhooks with retrying status to failed" do
Repository.new.fail_retrying_triggered_webhooks
expect(TriggeredWebhook.failed.count).to eq 2
expect(TriggeredWebhook.retrying.count).to eq 0
expect(TriggeredWebhook.successful.count).to eq 1
expect(TriggeredWebhook.not_run.count).to eq 1
end
end
end
end
end

0 comments on commit 1f2305b

Please sign in to comment.