From 989e6e3cdfc7832ce1b5ff15ebfad67b5fba03b6 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 7 Feb 2018 08:26:03 +1100 Subject: [PATCH] feat(matrix): refresh head matrix asynchronously to speed up pact publishing and tagging --- .../matrix/refresh_head_matrix_job.rb | 16 ++++++++++++++++ lib/pact_broker/matrix/repository.rb | 10 +++++++--- lib/pact_broker/matrix/row.rb | 4 ++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 lib/pact_broker/matrix/refresh_head_matrix_job.rb diff --git a/lib/pact_broker/matrix/refresh_head_matrix_job.rb b/lib/pact_broker/matrix/refresh_head_matrix_job.rb new file mode 100644 index 000000000..a2fe536ac --- /dev/null +++ b/lib/pact_broker/matrix/refresh_head_matrix_job.rb @@ -0,0 +1,16 @@ +require 'sucker_punch' +require 'pact_broker/matrix/head_row' + +module PactBroker + module Matrix + class RefreshHeadMatrixJob + + include SuckerPunch::Job + include PactBroker::Logging + + def perform params + PactBroker::Matrix::HeadRow.refresh(params[:params]) + end + end + end +end diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 5c8ad19d6..79b262317 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -2,8 +2,10 @@ require 'pact_broker/matrix/row' require 'pact_broker/matrix/latest_row' require 'pact_broker/matrix/head_row' +require 'pact_broker/matrix/refresh_head_matrix_job' require 'pact_broker/error' + module PactBroker module Matrix @@ -23,7 +25,7 @@ class Repository def refresh params PactBroker::Matrix::Row.refresh(params) - PactBroker::Matrix::HeadRow.refresh(params) + RefreshHeadMatrixJob.perform_in(1, params: params) end # Return the latest matrix row (pact/verification) for each consumer_version_number/provider_version_number @@ -128,11 +130,13 @@ def look_up_versions_for_latest_and_tag(selectors, options) end end.collect do | selector | if selector[:pacticipant_name] - selector[:pacticipant_id] = PactBroker::Domain::Pacticipant.find(name: selector[:pacticipant_name]).id + pacticipant = PactBroker::Domain::Pacticipant.find(name: selector[:pacticipant_name]) + selector[:pacticipant_id] = pacticipant ? pacticipant.id : nil end if selector[:pacticipant_name] && selector[:pacticipant_version_number] - selector[:pacticipant_version_id] = version_repository.find_by_pacticipant_name_and_number(selector[:pacticipant_name], selector[:pacticipant_version_number]).id + version = version_repository.find_by_pacticipant_name_and_number(selector[:pacticipant_name], selector[:pacticipant_version_number]) + selector[:pacticipant_version_id] = version ? version.id : nil end if selector[:pacticipant_version_number].nil? diff --git a/lib/pact_broker/matrix/row.rb b/lib/pact_broker/matrix/row.rb index 1a7fcf995..7af35e81c 100644 --- a/lib/pact_broker/matrix/row.rb +++ b/lib/pact_broker/matrix/row.rb @@ -1,6 +1,7 @@ require 'pact_broker/repositories/helpers' require 'pact_broker/webhooks/latest_triggered_webhook' require 'pact_broker/tags/tag_with_latest_flag' +require 'pact_broker/logging' module PactBroker module Matrix @@ -16,8 +17,11 @@ class Row < Sequel::Model(:materialized_matrix) dataset_module do include PactBroker::Repositories::Helpers + include PactBroker::Logging def refresh params + logger.debug("Refreshing #{model.table_name} for #{params}") + source_view_name = model.table_name.to_s.gsub('materialized_', '').to_sym if params[:consumer_name] || params[:provider_name] criteria = {}