From f20e018bf44a3b5d95233198a5bc7bddb7a7ada1 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 22 Jul 2020 12:12:26 +1000 Subject: [PATCH] NEP-10148 TenantWorker forward compatibility with application version constraints (#27) --- CHANGELOG.md | 4 ++ lib/patches.rb | 1 + lib/patches/application_version_validation.rb | 9 +++++ lib/patches/tenant_worker.rb | 9 ++++- lib/patches/version.rb | 2 +- lib/patches/worker.rb | 11 +----- spec/tenant_worker_spec.rb | 39 +++++++++++++++++-- 7 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 lib/patches/application_version_validation.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 19c83d4..412500b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [3.4.0] - 2020-07-21 +### Added +- `Patches::TenantWorker` application version constraint forward compatibility + ## [3.3.0] - 2020-07-20 ### Added - Application version constraints diff --git a/lib/patches.rb b/lib/patches.rb index 0d5aedb..cd06ba4 100644 --- a/lib/patches.rb +++ b/lib/patches.rb @@ -22,6 +22,7 @@ def self.logger=(log) require "patches/base" require "patches/config" require "patches/tenant_run_concern" +require "patches/application_version_validation" require "patches/tenant_worker" if defined?(Sidekiq) require "patches/engine" if defined?(Rails) require "patches/patch" diff --git a/lib/patches/application_version_validation.rb b/lib/patches/application_version_validation.rb new file mode 100644 index 0000000..4016df4 --- /dev/null +++ b/lib/patches/application_version_validation.rb @@ -0,0 +1,9 @@ +module Patches + module ApplicationVersionValidation + def valid_application_version?(application_version) + return true unless application_version + return true unless Patches::Config.configuration.application_version + Patches::Config.configuration.application_version == application_version + end + end +end diff --git a/lib/patches/tenant_worker.rb b/lib/patches/tenant_worker.rb index 01fbcd7..87c8d4d 100644 --- a/lib/patches/tenant_worker.rb +++ b/lib/patches/tenant_worker.rb @@ -3,10 +3,15 @@ class Patches::TenantWorker include Sidekiq::Worker include Patches::TenantRunConcern + include Patches::ApplicationVersionValidation sidekiq_options Patches::Config.configuration.sidekiq_options - def perform(tenant_name, path) - run(tenant_name, path) + def perform(tenant_name, path, params = {}) + if valid_application_version?(params['application_version']) + run(tenant_name, path) + else + self.class.perform_in(Patches::Config.configuration.retry_after_version_mismatch_in, tenant_name, path, params) + end end end diff --git a/lib/patches/version.rb b/lib/patches/version.rb index 10e9496..d4474c7 100644 --- a/lib/patches/version.rb +++ b/lib/patches/version.rb @@ -1,6 +1,6 @@ module Patches MAJOR = 3 - MINOR = 3 + MINOR = 4 PATCH = 0 VERSION = [MAJOR, MINOR, PATCH].compact.join(".").freeze end diff --git a/lib/patches/worker.rb b/lib/patches/worker.rb index 4eda2c9..5c5ef5d 100644 --- a/lib/patches/worker.rb +++ b/lib/patches/worker.rb @@ -2,22 +2,15 @@ class Patches::Worker include Sidekiq::Worker + include Patches::ApplicationVersionValidation sidekiq_options Patches::Config.configuration.sidekiq_options def perform(runner, params = {}) - if valid_application_version?(params) + if valid_application_version?(params['application_version']) runner.constantize.new.perform else self.class.perform_in(Patches::Config.configuration.retry_after_version_mismatch_in, runner, params) end end - - private - - def valid_application_version?(params) - return true unless params['application_version'] - return true unless Patches::Config.configuration.application_version - Patches::Config.configuration.application_version == params['application_version'] - end end diff --git a/spec/tenant_worker_spec.rb b/spec/tenant_worker_spec.rb index 02203c2..8219cf8 100644 --- a/spec/tenant_worker_spec.rb +++ b/spec/tenant_worker_spec.rb @@ -3,9 +3,42 @@ describe Patches::TenantWorker do describe '#perform' do - specify do - is_expected.to receive(:run).with('test', 'path') - subject.perform('test', 'path') + let(:runner) { instance_double(Patches::Runner, perform: true) } + + before do + allow(Patches::Config.configuration).to receive(:application_version) { application_version } + end + + context 'when application_version config is set' do + let(:application_version) { '5828321' } + + context 'when application version matches' do + it 'runs patches' do + expect(subject).to receive(:run).with('test', 'path') + subject.perform('test', 'path', 'application_version' => application_version) + end + end + + context 'when application_version does not match' do + it 'does not run patches' do + expect(subject).not_to receive(:run) + subject.perform('test', 'path', 'application_version' => 'd8f190c') + end + + it 'reschedules the job' do + expect(Patches::TenantWorker).to receive(:perform_in).with(1.minute, 'test', 'path', 'application_version' => 'd8f190c') + subject.perform('test', 'path', 'application_version' => 'd8f190c') + end + end + end + + context 'when application config is not set' do + let(:application_version) { nil } + + it 'runs patches' do + expect(subject).to receive(:run).with('test', 'path') + subject.perform('test', 'path', 'application_version' => 'd8f190c') + end end end end