Skip to content

Commit

Permalink
Add a flag for non-tenant-based jobs to avoid switching into a tenant…
Browse files Browse the repository at this point in the history
… context
  • Loading branch information
cbeer committed May 17, 2017
1 parent b9565a4 commit ad63755
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/jobs/cleanup_account_job.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/create_account_inline_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CreateAccountInlineJob < ActiveJob::Base
non_tenant_job

def perform(account)
CreateSolrCollectionJob.perform_now(account)
CreateFcrepoEndpointJob.perform_now(account)
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/create_fcrepo_endpoint_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CreateFcrepoEndpointJob < ActiveJob::Base
non_tenant_job

def perform(account)
name = account.tenant.parameterize

Expand Down
2 changes: 2 additions & 0 deletions app/jobs/create_redis_namespace_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CreateRedisNamespaceJob < ActiveJob::Base
non_tenant_job

def perform(account)
name = account.tenant.parameterize

Expand Down
2 changes: 2 additions & 0 deletions app/jobs/create_solr_collection_job.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 3 additions & 0 deletions config/settings/test.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
16 changes: 15 additions & 1 deletion lib/active_job_tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@ 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
super.merge('tenant' => Apartment::Tenant.current)
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
Expand Down
32 changes: 26 additions & 6 deletions spec/lib/active_job_tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit ad63755

Please sign in to comment.