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 Application
with my own implementation that provides a custom configuration loader (overrides the default Illuminate\Foundation\Bootstrap\LoadConfiguration
). The application detects the current tenant from the environment (either PHP's $_ENV
or the .env
file) on bootstrap and then loads the appropriate configuration files for the detected tenant.
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.
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.
A few possible solutions that I see would be:
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
exec()
); 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.)similar to the above, but run the job in a separate PHP thread instead of a separate process using
RunKit Sandbox
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.)
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.
Any suggestions on how to approach this?