diff --git a/app/jobs/cleanup_account_job.rb b/app/jobs/cleanup_account_job.rb index f18aa6907..1807ede26 100644 --- a/app/jobs/cleanup_account_job.rb +++ b/app/jobs/cleanup_account_job.rb @@ -1,4 +1,6 @@ class CleanupAccountJob < ActiveJob::Base + non_tenant_job + def perform(account) # Solr endpoint pulls its connection info from account directly cleanup_solr(account) diff --git a/app/jobs/create_account_inline_job.rb b/app/jobs/create_account_inline_job.rb index 14bbecd42..4d2c89ad5 100644 --- a/app/jobs/create_account_inline_job.rb +++ b/app/jobs/create_account_inline_job.rb @@ -1,4 +1,6 @@ class CreateAccountInlineJob < ActiveJob::Base + non_tenant_job + def perform(account) CreateSolrCollectionJob.perform_now(account) CreateFcrepoEndpointJob.perform_now(account) diff --git a/app/jobs/create_fcrepo_endpoint_job.rb b/app/jobs/create_fcrepo_endpoint_job.rb index 2550d8097..39861fd13 100644 --- a/app/jobs/create_fcrepo_endpoint_job.rb +++ b/app/jobs/create_fcrepo_endpoint_job.rb @@ -1,4 +1,6 @@ class CreateFcrepoEndpointJob < ActiveJob::Base + non_tenant_job + def perform(account) name = account.tenant.parameterize diff --git a/app/jobs/create_redis_namespace_job.rb b/app/jobs/create_redis_namespace_job.rb index db54e3520..948460ae1 100644 --- a/app/jobs/create_redis_namespace_job.rb +++ b/app/jobs/create_redis_namespace_job.rb @@ -1,4 +1,6 @@ class CreateRedisNamespaceJob < ActiveJob::Base + non_tenant_job + def perform(account) name = account.tenant.parameterize diff --git a/app/jobs/create_solr_collection_job.rb b/app/jobs/create_solr_collection_job.rb index bfc813996..bde159ec5 100644 --- a/app/jobs/create_solr_collection_job.rb +++ b/app/jobs/create_solr_collection_job.rb @@ -1,5 +1,7 @@ # Create a new account-specific Solr collection using the base templates class CreateSolrCollectionJob < ActiveJob::Base + non_tenant_job + ## # @param [Account] def perform(account) diff --git a/config/settings/test.yml b/config/settings/test.yml index 4a80fc1dd..a4e182e85 100644 --- a/config/settings/test.yml +++ b/config/settings/test.yml @@ -1,6 +1,9 @@ zookeeper: connection_str: 'localhost:9985/configs' +solr: + url: "http://127.0.0.1:8985/solr/" + multitenancy: enabled: true # admin_host: 'localhost' diff --git a/lib/active_job_tenant.rb b/lib/active_job_tenant.rb index 9b66d4809..7a93ed9fe 100644 --- a/lib/active_job_tenant.rb +++ b/lib/active_job_tenant.rb @@ -15,6 +15,14 @@ def deserialize(job_data) job.current_account = nil end end + + def non_tenant_job? + @non_tenant_job + end + + def non_tenant_job + @non_tenant_job = true + end end def serialize @@ -22,13 +30,19 @@ def serialize end def perform_now - switch do + if non_tenant_job? super + else + switch do + super + end end end private + delegate :non_tenant_job?, to: :class + def current_account @current_account ||= Account.find_by(tenant: current_tenant) end diff --git a/spec/lib/active_job_tenant_spec.rb b/spec/lib/active_job_tenant_spec.rb index e8c7c7da2..e00e19395 100644 --- a/spec/lib/active_job_tenant_spec.rb +++ b/spec/lib/active_job_tenant_spec.rb @@ -18,12 +18,6 @@ def perform end describe 'tenant context' do - it 'switches to the tenant database' do - expect(Apartment::Tenant).to receive(:switch).with('x') - - subject.perform_now - end - it 'evaluates in the context of a tenant and account' do expect(subject.perform_now).to eq account end @@ -38,4 +32,30 @@ def perform expect(delayed_subject.tenant).to eq 'x' end end + + describe '#perform_now' do + context 'a non-tenant-job' do + subject do + Class.new(ActiveJob::Base) do + non_tenant_job + + def perform; end + end + end + + it 'runs the job in the public, non-tenant scope' do + expect(Apartment::Tenant).not_to receive(:switch).with('x') + + subject.perform_now + end + end + + context 'a normal job' do + it 'runs the job in the context of a tenant' do + expect(Apartment::Tenant).to receive(:switch).with('x') + + subject.perform_now + end + end + end end