Skip to content

Commit

Permalink
Implement per-tenant error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
karlcloudy committed Aug 9, 2024
1 parent 17b531d commit c500c06
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/patches/patches_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class PatchesError < StandardError; end
14 changes: 14 additions & 0 deletions lib/patches/tenant_patch_unsuccessful_error.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/patches/tenant_run_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion lib/patches/tenant_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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

0 comments on commit c500c06

Please sign in to comment.