Consider the current tenant with delayed jobs
This commit is contained in:
committed by
Javi Martín
parent
275ddb08d8
commit
52ebeb7ba6
@@ -18,7 +18,7 @@ Apartment.configure do |config|
|
||||
# Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
|
||||
# A typical example would be a Customer or Tenant model that stores each Tenant's information.
|
||||
#
|
||||
config.excluded_models = %w[Tenant]
|
||||
config.excluded_models = %w[Delayed::Job Tenant]
|
||||
|
||||
# In order to migrate all of your Tenants you need to provide a list of Tenant names to Apartment.
|
||||
# You can make this dynamic by providing a Proc object to be called on migrations.
|
||||
|
||||
@@ -14,3 +14,15 @@ Delayed::Worker.read_ahead = 10
|
||||
Delayed::Worker.default_queue_name = "default"
|
||||
Delayed::Worker.raise_signal_exceptions = :term
|
||||
Delayed::Worker.logger = Logger.new(File.join(Rails.root, "log", "delayed_job.log"))
|
||||
|
||||
class ApartmentDelayedJobPlugin < Delayed::Plugin
|
||||
callbacks do |lifecycle|
|
||||
lifecycle.before(:enqueue) { |job| job.tenant = Tenant.current_schema }
|
||||
|
||||
lifecycle.around(:perform) do |worker, job, *args, &block|
|
||||
Tenant.switch(job.tenant) { block.call(worker, job, *args) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Delayed::Worker.plugins << ApartmentDelayedJobPlugin
|
||||
|
||||
5
db/migrate/20190214103106_add_tenant_to_delayed_job.rb
Normal file
5
db/migrate/20190214103106_add_tenant_to_delayed_job.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class AddTenantToDelayedJob < ActiveRecord::Migration[4.2]
|
||||
def change
|
||||
add_column :delayed_jobs, :tenant, :string
|
||||
end
|
||||
end
|
||||
@@ -561,6 +561,7 @@ ActiveRecord::Schema.define(version: 2022_09_15_154808) do
|
||||
t.string "queue"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "tenant"
|
||||
t.index ["priority", "run_at"], name: "delayed_jobs_priority"
|
||||
end
|
||||
|
||||
|
||||
@@ -51,4 +51,23 @@ describe Mailer do
|
||||
expect(user.subscriptions_token).to eq "subscriptions_token_value"
|
||||
end
|
||||
end
|
||||
|
||||
describe "multitenancy" do
|
||||
it "uses the current tenant when using delayed jobs", :delay_jobs do
|
||||
allow(ActionMailer::Base).to receive(:default_url_options).and_return({ host: "consul.dev" })
|
||||
create(:tenant, schema: "delay")
|
||||
|
||||
Tenant.switch("delay") do
|
||||
Setting["org_name"] = "Delayed tenant"
|
||||
|
||||
Mailer.delay.user_invite("test@consul.dev")
|
||||
end
|
||||
|
||||
Delayed::Worker.new.work_off
|
||||
body = ActionMailer::Base.deliveries.last.body.to_s
|
||||
expect(body).to match "Delayed tenant"
|
||||
expect(body).to match "href=\"http://delay.consul.dev/"
|
||||
expect(body).to match "src=\"http://delay.consul.dev/"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user