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.
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.
useIlluminate\Contracts\Queue\ShouldQueue;
useSpatie\Multitenancy\Jobs\TenantAware;
classTestJobimplements ShouldQueue, TenantAware
{
publicfunctionhandle()
{
// do the work
}
}
Jobs that never should be tenant aware should implement the empty marker interface Spatie\Multitenancy\Jobs\NotTenantAware.
useIlluminate\Contracts\Queue\ShouldQueue;
useSpatie\Multitenancy\Jobs\NotTenantAware;
classTestJobimplements ShouldQueue, NotTenantAware
{
publicfunctionhandle()
{
// do the work
}
}
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 will make no modifications to the current tenant, which may still be set from a previous job. As such, it is important that your jobs make no assumptions about the active tenant unless they are tenant aware.