Sha256: 6e69169e0a0efe032fbbbc2c8d99238369cf0d55be00763cd8c5f20f0f8608a5

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module ActsAsTenant::Sidekiq
  # Get the current tenant and store in the message to be sent to Sidekiq.
  class Client
    def call(worker_class, msg, queue, redis_pool)
      if ActsAsTenant.current_tenant.present?
        msg["acts_as_tenant"] ||=
          {
            "class" => ActsAsTenant.current_tenant.class.name,
            "id" => ActsAsTenant.current_tenant.id
          }
      end

      yield
    end
  end

  # Pull the tenant out and run the current thread with it.
  class Server
    def call(worker_class, msg, queue)
      if msg.has_key?("acts_as_tenant")
        account = msg["acts_as_tenant"]["class"].constantize.find msg["acts_as_tenant"]["id"]
        ActsAsTenant.with_tenant account do
          yield
        end
      else
        yield
      end
    end
  end
end

Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.add ActsAsTenant::Sidekiq::Client
  end
end

Sidekiq.configure_server do |config|
  config.client_middleware do |chain|
    chain.add ActsAsTenant::Sidekiq::Client
  end
  config.server_middleware do |chain|
    if defined?(Sidekiq::Middleware::Server::RetryJobs)
      chain.insert_before Sidekiq::Middleware::Server::RetryJobs, ActsAsTenant::Sidekiq::Server
    elsif defined?(Sidekiq::Batch::Server)
      chain.insert_before Sidekiq::Batch::Server, ActsAsTenant::Sidekiq::Server
    else
      chain.add ActsAsTenant::Sidekiq::Server
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_as_tenant-0.5.2 lib/acts_as_tenant/sidekiq.rb
acts_as_tenant-0.5.1 lib/acts_as_tenant/sidekiq.rb
acts_as_tenant-0.5.0 lib/acts_as_tenant/sidekiq.rb