make(Dispatcher::class)); } public function __construct(Repository $config, QueueManager $queue) { $this->config = $config; $this->queue = $queue; $this->setUpPayloadGenerator(); } protected static function setUpJobListener($dispatcher) { $dispatcher->listen(JobProcessing::class, function ($event) { $tenantId = $event->job->payload()['tenant_id'] ?? null; // The job is not tenant-aware if (! $tenantId) { return; } // Tenancy is already initialized for the tenant (e.g. dispatchNow was used) if (tenancy()->initialized && tenant()->getTenantKey() === $tenantId) { return; } // Tenancy was either not initialized, or initialized for a different tenant. // Therefore, we initialize it for the correct tenant. tenancy()->initialize(tenancy()->find($tenantId)); }); } protected function setUpPayloadGenerator() { $bootstrapper = &$this; if (! $this->queue instanceof QueueFake) { $this->queue->createPayloadUsing(function ($connection) use (&$bootstrapper) { return $bootstrapper->getPayload($connection); }); } } public function bootstrap(Tenant $tenant) { // } public function revert() { // } public function getPayload(string $connection) { if (! tenancy()->initialized) { return []; } if ($this->config["queue.connections.$connection.central"]) { return []; } $id = tenant()->getTenantKey(); return [ 'tenant_id' => $id, ]; } }