From 10fb30fe033b9a521775332ca18f32e551533836 Mon Sep 17 00:00:00 2001 From: Mehmood Ahmad <31419912+mehmoodak@users.noreply.github.com> Date: Thu, 14 Sep 2023 02:14:22 +0500 Subject: [PATCH] Fixes fatal error while reading config file of the integration (#4857) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 FIX: fatal error while updating configmap * 👌 IMPROVE: use clearstatcache to clear the cache of a specific file instead whole cache * 🐛 FIX: use @file_get_contents to suppress warning if file does not exist --- integrations/integration-vip-config.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/integrations/integration-vip-config.php b/integrations/integration-vip-config.php index ef09d0bd1d..028bcf9913 100644 --- a/integrations/integration-vip-config.php +++ b/integrations/integration-vip-config.php @@ -78,13 +78,29 @@ private function set_config( string $slug ): void { * @return null|mixed */ protected function get_vip_config_from_file( string $slug ) { - $config_file_path = ABSPATH . 'config/integrations-config/' . $slug . '-config.php'; + $config_file_directory = ABSPATH . 'config/integrations-config'; + $config_file_name = $slug . '-config.php'; + $config_file_path = $config_file_directory . '/' . $config_file_name; + + /** + * Clear cache to always read data from latest config file. + * + * Kubernetes ConfigMap updates the file via symlink instead of actually replacing the file and + * PHP cache can hold a reference to the old symlink that can cause fatal if we use require + * on it. + */ + if ( false === @file_get_contents( $config_file_path ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + clearstatcache( true, $config_file_directory . '/' . $config_file_name ); + // Clears cache for files created by k8s ConfigMap. + clearstatcache( true, $config_file_directory . '/..data' ); + clearstatcache( true, $config_file_directory . '/..data/' . $config_file_name ); + } if ( ! is_readable( $config_file_path ) ) { return null; } - return require_once $config_file_path; + return require $config_file_path; } /**