title: Single shared queue worker in Laravel multi-tenant app
body: <p>I'm building a multi-tenant Laravel application (on Laravel 5.3) that allows each tenant to have its own set of configurations for any supported Laravel settings. This is currently achieved by overriding the default Laravel <code>Application</code> with my own implementation that provides a custom configuration loader (overrides the default <code>Illuminate\Foundation\Bootstrap\LoadConfiguration</code>). The application detects the current tenant from the environment (either PHP's <code>$_ENV</code> or the <code>.env</code> file) on bootstrap and then loads the appropriate configuration files for the detected tenant.</p>
  
  <p>The above approach works great for both the HTTP and Console kernels where each request/command has a limited life-cycle but I'm not sure how to approach the queue worker. I would like to have a single queue worker for all the tenants and I've already implemented a custom queue connector to add additional metadata when a queue job is scheduled, to make it possible to identify the tenant when the worker receives it.</p>
  
  <p>The part on which I'm looking for your help is how to run each queue job in an isolated environment which I can bootstrap with my custom configuration. </p>
  
  <p>A few possible solutions that I see would be:</p>
  
  <ul>
  <li><p>to run a custom queue worker that runs as a daemon and gets the job from the queue, but executes the job in a separate PHP process (created via <code>exec()</code>); once the job is executed, the worker gathers the results (status, exceptions, etc.) and finishes the job in the parent process (e.g. deletes the job, etc.)</p></li>
  <li><p>similar to the above, but run the job in a separate PHP thread instead of a separate process using <code>RunKit Sandbox</code></p></li>
  <li><p>implement a solution that "reboots" the application once a queue job has been received (e.g. reloads configurations for the current tenant, resets any resolved dependencies, etc.)</p></li>
  </ul>
  
  <p>What's important is that I'd like for this multi-tenant job execution to be transparent for the job itself so that jobs that are not designed to run in a multi-tenant environment (e.g. jobs from third party packages like Laravel Scout) can be handled without any modification.</p>
  
  <p>Any suggestions on how to approach this?</p>