After updating from symfony4 to symfony6, $_SERVER does not seem to be populated by GCP variables
P粉617597173
P粉617597173 2024-01-10 17:25:14
0
1
397

So, after updating from symfony4 to symfony6, I tried deploying on the preprod server, but it didn't work well because our Kernel.php no longer seems to have GCP variables in the $_SERVER global variable.

This is our kernel configuration where we override the getCacheDir method to determine the cache path to use with symfony4.

public function getCacheDir(): string
{

    if ($this->onAppEngine()) 
    return "/tmp/" . $this->environment . '/cache';

    return $this->getProjectDir() . '/var/cache/' . $this->environment;
}

private function onAppEngine(): bool
{
    return isset($_SERVER['GAE_INSTANCE']);
}
First thing to mention is that if I remove my condition (i.e. return the /tmp path directly in getCacheDir) everything works fine (but this doesn't work in a development environment because it's not the correct path for the cache in development ) so I'm pretty sure (but I could be wrong) that the $server var I want is no longer populated.

So this behavior is really weird, I have to mention that our website is multi-lingual. When I get to the base url (example.com) before redirecting to the localized url, I try to put a dump ($server["gae_instance"]) and here, I can see that we do have the $_server var ( I saw the value of $_SERVER['GAE_INSTANCE'] dumped before the redirect)

So know, it seems that we do have $_SERVER GAE_INSTANCE on the first request (example.com) but once redirected (to example.com/fr) it no longer exists (gives wrong cache path, I get the error)

I do think this is a change between symfony4 and symfony6 since we didn't make any changes to gcp (except the php version of the application, but we passed from php7.4 to php8.1 and in the google docs, it Doesn't seem to affect something https://cloud.google.com/appengine/docs/standard/php-gen2/runtime?hl=fr#php-8.1)

I must mention that we used to have a translation package (jmsi18n) which I removed since we are now using the symfony translation package, (I don't really think this is the source of the problem, but I don't think want to miss something thing.)

#edit

#Here is some test code and the resulting logs (I tried print_r and syslog since I wasn't sure which one was logging)

First, I tried returning the cache directory unconditionally and there were many logs to follow step by step, everything seemed to be working fine, all expected logs were sent (and the website was running)

###Then I tried the same code (with some additional logs) but in the condition it returns and even if we enter the condition the error is appended and it gets really weird, it might be linked but the kernel is a The request fires multiple times (and the gcp logs show two 500 requests on /fr, but we pass all expected conditions each time) ###
P粉617597173
P粉617597173

reply all(1)
P粉217784586

So I finally figured out what the problem was, with the help of JensV, I still don't understand everything, it might be related to cache warming, but I'm not 100% sure.

Digging deeper into my code to find the cause of the error, I ended up investigating the Symfony kernel (vendor/symfony/http-kernel/Kernel.php) and found this line in the return of the getKernelParameters method, which I didn't quite understand Why does it interfere, not even sure if it's relevant

In fact, we overridden getCacheDir and getBuildDir, essentially, getBuildDir returns the getCacheDir() method, but we overridden the getBuildDir method to locate a different path (/workspace/var/cache) and the past path worked .

So my intuition is that getCacheDir and getBuildDir should behave the same (since this is native behavior)

So I restored getBuildDir returning $this->getCacheDir() (I thought I could simply remove that method since we no longer override anything)

Everything works fine now (I even restored the initial isset($_server['gae_instance']) condition along with some other workarounds we did for other libraries)

I'm still pretty sure it was getCacheDir being called (since we never touched the getBuildDir() method, and we were able to get good behavior by simply returning cacheDir unconditionally, while getBuildDir was still returning workspace /var/ cache path instead of /tmp)

So I really don't understand the meaning here, but it finally worked out!

Final code:

public function getCacheDir(): string
{
    if ($this->onAppEngine()) {
        return "/tmp/" . $this->environment . '/cache';
    }

    return $this->getProjectDir() . '/var/cache/' . $this->environment;
}

public function getBuildDir(): string
{
    return $this->getCacheDir();
}

private function onAppEngine(): bool
{
    return isset($_SERVER["GAE_INSTANCE"]);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!