Making queues tenant aware | laravel-multitenancy | Spatie       

 SPATIE  

  Laravel Multitenancy 
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-multitenancy](https://spatie.be/docs/laravel-multitenancy/v4)  Basic-usage  Making queues tenant aware

 Version   v4   v3   v2   v1      

 Other versions for crawler [v4](https://spatie.be/docs/laravel-multitenancy/v4) [v3](https://spatie.be/docs/laravel-multitenancy/v3) [v2](https://spatie.be/docs/laravel-multitenancy/v2) [v1](https://spatie.be/docs/laravel-multitenancy/v1) 

  Making queues tenant aware    
- [ Introduction ](https://spatie.be/docs/laravel-multitenancy/v4/introduction)
- [ Upgrade guide ](https://spatie.be/docs/laravel-multitenancy/v4/upgrade-guide)
- [ Support us ](https://spatie.be/docs/laravel-multitenancy/v4/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-multitenancy/v4/requirements)
- [ Questions and issues ](https://spatie.be/docs/laravel-multitenancy/v4/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-multitenancy/v4/changelog)
- [ ](https://spatie.be/docs/laravel-multitenancy/v4/installation-setup)

Installation
------------

- [ Base installation ](https://spatie.be/docs/laravel-multitenancy/v4/installation/base-installation)
- [ Using a single database ](https://spatie.be/docs/laravel-multitenancy/v4/installation/using-a-single-database)
- [ Using multiple databases ](https://spatie.be/docs/laravel-multitenancy/v4/installation/using-multiple-databases)
- [ Determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v4/installation/determining-current-tenant)

Basic usage
-----------

- [ Automatically determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v4/basic-usage/automatically-determining-the-current-tenant)
- [ Working with the current tenant ](https://spatie.be/docs/laravel-multitenancy/v4/basic-usage/working-with-the-current-tenant)
- [ Making queues tenant aware ](https://spatie.be/docs/laravel-multitenancy/v4/basic-usage/making-queues-tenant-aware)

Advanced usage
--------------

- [ Ensuring a current tenant has been set ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/ensuring-a-current-tenant-has-been-set)
- [ Looping over a collection of tenants ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/looping-over-a-collection-of-tenants)
- [ Making Artisan command tenant aware ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/making-artisan-commands-tenant-aware)
- [ Using a custom tenant model ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/using-a-custom-tenant-model)
- [ Listening for events ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/listening-for-events)
- [ Using tenant specific facades ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/using-tenant-specific-facades)
- [ Executing code for tenants and landlords ](https://spatie.be/docs/laravel-multitenancy/v4/advanced-usage/executing-code-for-tenants-and-landlords)

Using tasks to prepare the environment
--------------------------------------

- [ Overview ](https://spatie.be/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview)
- [ Creating your own task ](https://spatie.be/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/creating-your-own-task)
- [ Switching databases ](https://spatie.be/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/switching-databases)
- [ Switching route cache paths ](https://spatie.be/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/switching-route-cache-paths)
- [ Prefixing cache ](https://spatie.be/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/prefixing-cache)

 Making queues tenant aware
==========================

###  On this page 

1. [ Make specific jobs tenant aware ](#content-make-specific-jobs-tenant-aware)
2. [ Making specific jobs not tenant aware ](#content-making-specific-jobs-not-tenant-aware)
3. [ Queueing Closures ](#content-queueing-closures)
4. [ When the tenant cannot be retrieved ](#content-when-the-tenant-cannot-be-retrieved)
5. [ The current tenant of the surrounding code ](#content-the-current-tenant-of-the-surrounding-code)

The package can make queues tenant aware. To enable this behaviour, set the `queues_are_tenant_aware_by_default` key in the `multitenancy` config file to `true`.

When the behaviour is enabled, the package will keep track of which tenant is the current one when a job is dispatched. That tenant will automatically be made the current one inside that job.

Make specific jobs tenant aware
-----------------------------------------------------------------------------------------------------------------------------------------------------

If you don't want to make all jobs tenant aware, you must set the `queues_are_tenant_aware_by_default` config key to `false`. Jobs that should be tenant aware should implement the empty marker interface `Spatie\Multitenancy\Jobs\TenantAware` or should be added to the config `tenant_aware_jobs`.

```
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\Multitenancy\Jobs\TenantAware;

class TestJob implements ShouldQueue, TenantAware
{
    public function handle()
    {
        // do the work
    }
}
```

or, using the config `multitenancy.php`:

```
'tenant_aware_jobs' => [
    TestJob::class,
],
```

Making specific jobs not tenant aware
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Jobs that never should be tenant aware should implement the empty marker interface `Spatie\Multitenancy\Jobs\NotTenantAware` or should be added to the config `not_tenant_aware_jobs`.

```
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\Multitenancy\Jobs\NotTenantAware;

class TestJob implements ShouldQueue, NotTenantAware
{
    public function handle()
    {
        // do the work
    }
}
```

or, using the config `multitenancy.php`:

```
'not_tenant_aware_jobs' => [
    TestJob::class,
],
```

Queueing Closures
-----------------------------------------------------------------------------------------------------------

Dispatch a closure is slightly different from a job class because here, you can't implement `TenantAware` or `NotTenantAware` interfaces. The package can handle the queue closures by enabling the `queues_are_tenant_aware_by_default`, but if you enjoy keeping to `false` parameter, you can dispatch a tenant-aware job closure like so:

```
$tenant = Tenant::current();

dispatch(function () use ($tenant) {
    $tenant->execute(function () {
        // Your job
    });
});
```

When the tenant cannot be retrieved
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

If a tenant aware job is unable to retrieve the tenant, because the tenant was deleted before the job was processed, for example, the job will fail with an instance of `Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob`.

On the other hand, a job that is not tenant aware runs without a current tenant. As such, it is important that your jobs make no assumptions about the active tenant unless they are tenant aware.

The current tenant of the surrounding code
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Processing a job never leaves the surrounding code with a different current tenant than it had before. Whatever tenant was current before the job started is restored when the job finishes, both when it succeeds and when it fails.

This matters when a job runs in the same process as the code that dispatched it, which is the case on the `sync` connection and when using `dispatchSync()`.

```
$tenant->makeCurrent();

SomeNotTenantAwareJob::dispatchSync(); // runs without a current tenant

Tenant::current(); // still $tenant
```

Retrying a failed job works the same way. `queue:retry` needs the tenant of the failed job to be current while it pushes that job back onto the queue, but once the command has finished the tenant of the code that ran it is restored. This matters if you retry jobs from a request with `Artisan::call('queue:retry')`.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
