Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement per-tenant error handling. #41

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading