diff --git a/lib/patches/patches_error.rb b/lib/patches/patches_error.rb new file mode 100644 index 0000000..25653ef --- /dev/null +++ b/lib/patches/patches_error.rb @@ -0,0 +1 @@ +class PatchesError < StandardError; end diff --git a/lib/patches/tenant_patch_unsuccessful_error.rb b/lib/patches/tenant_patch_unsuccessful_error.rb new file mode 100644 index 0000000..4006547 --- /dev/null +++ b/lib/patches/tenant_patch_unsuccessful_error.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class TenantPatchUnsuccessfulError < PatchesError + attr_reader :tenant, :patch, :error + + def initialize(message = nil, tenant:, path:, exception:) + message ||= "Error applying patch '#{path}' for tenant '#{tenant}': #{exception.message}" + super(message) + + @tenant = tenant + @path = path + @exception = exception + end +end diff --git a/lib/patches/tenant_run_concern.rb b/lib/patches/tenant_run_concern.rb index 9f9e73d..dc66638 100644 --- a/lib/patches/tenant_run_concern.rb +++ b/lib/patches/tenant_run_concern.rb @@ -4,6 +4,10 @@ def run(tenant_name, path = nil) Apartment::Tenant.switch(tenant_name) do Patches::Runner.new(path).perform end + rescue StandardError => e + Patches.logger.error(e.message) + Patches.logger.error(e.backtrace.join("\n")) + raise(TenantPatchUnsuccessfulError, tenant: tenant_name, path: path, exception: e) end end end diff --git a/lib/patches/tenant_runner.rb b/lib/patches/tenant_runner.rb index 77043e8..b12449f 100644 --- a/lib/patches/tenant_runner.rb +++ b/lib/patches/tenant_runner.rb @@ -9,6 +9,7 @@ def initialize(path: nil, tenants: nil) def perform Patches.logger.info("Patches tenant runner for: #{tenants.join(',')}") + failures = [] tenants.each do |tenant| if parallel? Patches::TenantWorker.perform_async( @@ -17,9 +18,14 @@ def perform application_version: Patches::Config.configuration.application_version ) else - run(tenant, path) + begin + run(tenant, path) + rescue TenantPatchUnsuccessfulError => e + failures << e + end end end + raise_failures(failures) if failures.any? end def tenants @@ -31,4 +37,11 @@ def tenants def parallel? Patches::Config.configuration.sidekiq_parallel end + + def raise_failures(failures) + message = 'Patching failed for one or more tenants: ' + message += failures.map { |f| "#{f.tenant} (#{f.path}, #{f.exception.message})" }.join(', ') + + raise(PatchesError, message) + end end