Skip to content

Commit 618905e

Browse files
committed
Implement per-tenant error handling.
1 parent 17b531d commit 618905e

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

lib/patches/patches_error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class PatchesError < StandardError; end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
class TenantPatchUnsuccessfulError < PatchesError
4+
attr_reader :tenant, :patch, :error
5+
6+
def initialize(message = nil, tenant:, path:, exception:)
7+
message ||= "Error applying patch '#{path}' for tenant '#{tenant}': #{exception.message}"
8+
super(message)
9+
10+
@tenant = tenant
11+
@path = path
12+
@exception = exception
13+
end
14+
end

lib/patches/tenant_run_concern.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ module Patches
22
module TenantRunConcern
33
def run(tenant_name, path = nil)
44
Apartment::Tenant.switch(tenant_name) do
5-
Patches::Runner.new(path).perform
5+
begin
6+
Patches::Runner.new(path).perform
7+
rescue StandardError => e
8+
Patches.logger.error(e.message)
9+
Patches.logger.error(e.backtrace.join("\n"))
10+
raise(TenantPatchUnsuccessfulError, tenant: tenant_name, patch: path, exception: e)
11+
end
612
end
713
end
814
end

lib/patches/tenant_runner.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def initialize(path: nil, tenants: nil)
99

1010
def perform
1111
Patches.logger.info("Patches tenant runner for: #{tenants.join(',')}")
12+
failures = []
1213
tenants.each do |tenant|
1314
if parallel?
1415
Patches::TenantWorker.perform_async(
@@ -17,9 +18,14 @@ def perform
1718
application_version: Patches::Config.configuration.application_version
1819
)
1920
else
20-
run(tenant, path)
21+
begin
22+
run(tenant, path)
23+
rescue TenantPatchUnsuccessfulError => e
24+
failures << e
25+
end
2126
end
2227
end
28+
raise_failures(failures) if failures.any?
2329
end
2430

2531
def tenants
@@ -31,4 +37,11 @@ def tenants
3137
def parallel?
3238
Patches::Config.configuration.sidekiq_parallel
3339
end
40+
41+
def raise_failures(failures)
42+
message = 'Patching failed for one or more tenants: '
43+
message += failures.map { |f| "#{f.tenant} (#{f.path}, #{f.exception.message})" }.join(', ')
44+
45+
raise PatchesError(message)
46+
end
3447
end

0 commit comments

Comments
 (0)