<?php
/**
* Cleans the runtime environment for scheduled runs, with fallback logic.
*
* @return bool True on success, false on failure.
*/
function cleanRuntimeEnvironment(): bool
{
// Attempt to clear the opcache.
if (opcache_reset() === false) {
error_log("Failed to clear opcache: " . error_get_last()['message']);
// Fallback: Try unloading all functions.
unload_all_functions();
if (opcache_reset() === false) {
error_log("Failed to clear opcache after unloading functions: " . error_get_last()['message']);
return false; //Final fallback failure
}
}
// Unload all functions to ensure a clean state.
unload_all_functions();
// Clear the autoloader.
spl_autoload_register(null);
//Clear session data.
session_unset();
session_destroy();
//Clear the error log
if (file_put_contents('php_error.log', '') === false) {
error_log("Failed to clear php_error.log: " . error_get_last()['message']);
}
return true;
}
/**
* Unloads all functions.
*/
function unload_all_functions(): void
{
$functions = get_declared_functions();
foreach ($functions as $function) {
if (function_exists($function)) {
unload_function($function);
}
}
}
/**
* Unloads a single function.
*
* @param string $function The name of the function to unload.
*/
function unload_function(string $function): void
{
if (unload_function_by_name($function) === false) {
error_log("Failed to unload function: " . $function);
}
}
/**
* Unloads a function by name.
* @param string $functionName
* @return bool
*/
function unload_function_by_name(string $functionName): bool
{
$result = @unload_function($functionName);
if ($result === false) {
return false;
}
return true;
}
?>
Add your comment