From 9c9c96d2c9b4b1592bbf4e3da1cb5e1a19b2425b Mon Sep 17 00:00:00 2001 From: Aldrin Martoq Date: Fri, 2 Jun 2017 00:14:52 -0400 Subject: [PATCH] Added integration instructions with Delayed::Job. --- README.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c5f2ccce..603eb3c3 100644 --- a/README.md +++ b/README.md @@ -482,7 +482,38 @@ end ## Delayed::Job -Has been removed. See [apartment-sidekiq](https://github.com/influitive/apartment-sidekiq) for a better backgrounding experience. +It's really easy to write a custom plugin that will work for *all* jobs with no futher code. First, install delayed_job as required. Next, add a tenant column with the following migration: + +```bash +$ rails generate migration AddTenantToDelayedJob tenant:string +``` + +Last, use the following plugin: + +```ruby +# config/initializers/a0_delayed_tenant_plugin.rb +class A0DelayedTenantPlugin < Delayed::Plugin + callbacks do |lifecycle| + # save current tenant before enqueuing the job + lifecycle.before :enqueue do |job| + job.tenant = Apartment::Tenant.current + end + + lifecycle.around :perform do |worker, job, *args, &block| + # Switch to the saved tenant, ocurrs before deserializing this job + Apartment::Tenant.switch(job.tenant) do + # Add aditional context setup HERE, like security context for ex. + block.call(worker, job, *args) + end + end + end +end + +# register this plugin +Delayed::Worker.plugins << A0DelayedTenantPlugin +``` + +And that's all. ## Contributing