Implement logging for process execution

This commit is contained in:
netkas 2024-12-04 13:44:15 -05:00
parent c4e07a21a6
commit 3019739048

View file

@ -2,14 +2,36 @@
namespace PsyncLib; namespace PsyncLib;
use LogLib\Logger;
use RuntimeException; use RuntimeException;
use Throwable; use Throwable;
class Psync class Psync
{ {
private static ?Logger $logger = null;
private static int $sharedMemorySize = 65536; private static int $sharedMemorySize = 65536;
private static int $sharedMemoryPermissions = 0644; private static int $sharedMemoryPermissions = 0644;
private static array $promises = []; private static array $promises = [];
private static int $lastClean = 0;
/**
* Retrieves the singleton instance of the Logger.
*
* Checks if the static $logger property is null, and if so, initializes it with
* a new Logger instance configured with a specific name. Ensures that the same
* Logger instance is returned on subsequent calls.
*
* @return Logger The singleton Logger instance for logging activities.
*/
private static function getLogger(): Logger
{
if(self::$logger === null)
{
self::$logger = new Logger('net.nosial.psync');
}
return self::$logger;
}
/** /**
* Executes a callable within a forked process while handling * Executes a callable within a forked process while handling
@ -22,6 +44,7 @@
*/ */
public static function do(callable $callable, array $args = []): P public static function do(callable $callable, array $args = []): P
{ {
self::getLogger()->debug(sprintf('[%s]: Preparing to call %s', posix_getpid(), self::callableToString($callable)));
$shm_key = ftok(__FILE__, chr(mt_rand(0, 255))); // Generate a more unique key $shm_key = ftok(__FILE__, chr(mt_rand(0, 255))); // Generate a more unique key
$try = 0; $try = 0;
$shm = false; $shm = false;
@ -49,6 +72,7 @@
} }
elseif ($pid === 0) elseif ($pid === 0)
{ {
self::getLogger()->debug(sprintf('[%s]: Executing %s', posix_getpid(), self::callableToString($callable)));
// Child process // Child process
try try
{ {
@ -57,15 +81,18 @@
// Write the length of the serialized data and the data itself // Write the length of the serialized data and the data itself
$data = pack('L', strlen($serialized)) . $serialized; // Pack the length as a 4-byte integer $data = pack('L', strlen($serialized)) . $serialized; // Pack the length as a 4-byte integer
shmop_write($shm, $data, 0); // Write to shared memory shmop_write($shm, $data, 0); // Write to shared memory
self::getLogger()->debug(sprintf('[%s]: Finished executing %s', posix_getpid(), self::callableToString($callable)));
} }
catch (Throwable $e) catch (Throwable $e)
{ {
self::getLogger()->error(sprintf('[%s]: Exception thrown for %s: %s', posix_getpid(), self::callableToString($callable), $e->getMessage()), $e);
$error = serialize($e); // Serialize exception if any $error = serialize($e); // Serialize exception if any
$data = pack('L', strlen($error)) . $error; // Pack the length as a 4-byte integer $data = pack('L', strlen($error)) . $error; // Pack the length as a 4-byte integer
shmop_write($shm, $data, 0); shmop_write($shm, $data, 0);
} }
finally finally
{ {
self::getLogger()->debug(sprintf('[%s]: Resource closure at %s', posix_getpid(), self::callableToString($callable)));
shmop_delete($shm); // Delete shared memory shmop_delete($shm); // Delete shared memory
exit(0); // Exit the child process exit(0); // Exit the child process
} }
@ -73,10 +100,34 @@
// Parent process: return the P object immediately // Parent process: return the P object immediately
$p = new P($pid, $shm); $p = new P($pid, $shm);
self::getLogger()->debug(sprintf('[%s]: Promise created for %s: %s', posix_getpid(), self::callableToString($callable), "test"));
self::$promises[$p->getUuid()] = $p; self::$promises[$p->getUuid()] = $p;
return $p; return $p;
} }
/**
* Converts a*/
private static function callableToString(callable $callable): string
{
if(is_string($callable))
{
return $callable;
}
if(is_array($callable))
{
foreach($callable as $item)
{
if(is_string($item))
{
return $item;
}
}
}
return '';
}
/** /**
* Checks if the process is completed. * Checks if the process is completed.
* *
@ -208,6 +259,12 @@
*/ */
public static function clean(): int public static function clean(): int
{ {
if(time() - self::$lastClean < 8)
{
return 0;
}
self::$lastClean = time();
$count = 0; $count = 0;
foreach(self::$promises as $uuid => $p) foreach(self::$promises as $uuid => $p)
{ {