From 1f2305bd96121cca9b045b6e84507972555859d0 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 12 Sep 2017 18:02:10 +1000 Subject: [PATCH] feat(webhook status): set any triggered webhooks in 'retrying' status to 'failed' on startup --- lib/pact_broker/app.rb | 7 ++++--- lib/pact_broker/webhooks/repository.rb | 4 ++++ lib/pact_broker/webhooks/service.rb | 4 ++++ lib/pact_broker/webhooks/triggered_webhook.rb | 16 +++++++++++++++ spec/lib/pact_broker/app_spec.rb | 20 +++++++++++-------- .../pact_broker/webhooks/repository_spec.rb | 19 ++++++++++++++++++ 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 3c5743794..3dc70843a 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -23,7 +23,7 @@ def initialize &block @configuration = PactBroker.configuration yield configuration post_configure - migrate_database + prepare_database prepare_app end @@ -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 @@ -135,6 +137,5 @@ def running_app @app_builder end end - end end diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index c817ff42a..82c2f7ad7 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -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 diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index 35af3d0ae..5afdb425b 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -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 diff --git a/lib/pact_broker/webhooks/triggered_webhook.rb b/lib/pact_broker/webhooks/triggered_webhook.rb index 00857e7b6..ac4a1a5f7 100644 --- a/lib/pact_broker/webhooks/triggered_webhook.rb +++ b/lib/pact_broker/webhooks/triggered_webhook.rb @@ -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) diff --git a/spec/lib/pact_broker/app_spec.rb b/spec/lib/pact_broker/app_spec.rb index 7e07fe5f1..07bb24147 100644 --- a/spec/lib/pact_broker/app_spec.rb +++ b/spec/lib/pact_broker/app_spec.rb @@ -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 @@ -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/ @@ -208,6 +213,5 @@ def self.calls expect { subject }.to_not change { PactBroker::Domain::Pacticipant.count } end end - end end diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index d513edf1e..70ed3ab8e 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -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