Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reboot kernel & Multi tenancy #56

Open
greggailly opened this issue Nov 11, 2021 · 2 comments
Open

Reboot kernel & Multi tenancy #56

greggailly opened this issue Nov 11, 2021 · 2 comments

Comments

@greggailly
Copy link

Hi,
We are trying to use roadrunner on a multi tenant symfony project.
Multi-tenancy is handled by the kernel, we inject database url dynamically depending on the request. This works fine with the PHP dev server however we cannot get it to work with roadrunner (missing database url).
I guess this comes from the fact that the kernel is not always rebooted. So we tried to set the strategy to always but this didn't have any effect. Furthermore we tried to add $this->kernel->reboot(null); at the beginning of the while loop in the Worker.php to make sure the container is rebuild but didn't seem to solve our problem either.
Does anybody have any ideas on how to handle this ?
Thanks in advance !

@Baldinof
Copy link
Owner

Baldinof commented Nov 12, 2021 via email

@greggailly
Copy link
Author

greggailly commented Nov 12, 2021

To manage multi tenancy on Symfony side we are using what is described in the following article: https://symfony.com/doc/current/configuration/override_dir_structure.html
More specifically we inject at runtime a parameter inside the container using the configureContainer method in the Kernel:

// $url = custom db url created after having read file specific to each "client"
$container->parameters()->set('db_url', $url);

So we consider the only "dynamic" part of our multi tenancy is handle by the container when being build so we use the getCacheDir method to make sure that on each request, cache corresponding to the correct client is loaded and the the db_url parameter in the container is OK.

 public function getCacheDir()
    {
        $request = Request::createFromGlobals();
        $this->client = $request->headers->get('x-client');
        return "/server/data/cache/" . $this->client . "/" . $this->environment;
    }

Process is simplified but basically we get the client directory from a header added by our reverse proxy and make sure that the container is loaded for this specific client.

That being said we need to have a new Kernel booted for each request otherwise the cache fetched could be from a previous request.

So at the end for the moment we ended up writing our own RR worker.
It boots the kernel inside the while loop to make sure it is created depending on the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants