From 13ad2b0ef81d03a4287d3bb57fbe70fb6febab3f Mon Sep 17 00:00:00 2001 From: Netkas Date: Fri, 16 Jun 2023 03:38:51 -0400 Subject: [PATCH] Many changes. --- .idea/scopes/TamerLib.xml | 3 + .idea/scopes/Test_Directory.xml | 3 + src/TamerLib/Classes/Utilities.php | 67 ++++++++----- src/TamerLib/Classes/WorkerSupervisor.php | 1 + src/TamerLib/Classes/subproc | 31 ++---- src/TamerLib/Enums/JobStatus.php | 31 +++--- src/TamerLib/Enums/TamerMode.php | 2 + .../Exceptions/JobManagerException.php | 3 +- src/TamerLib/Objects/JobPacket.php | 98 ------------------- src/TamerLib/Objects/ServerConfiguration.php | 3 +- src/TamerLib/Objects/WorkerConfiguration.php | 8 -- src/TamerLib/tm.php | 13 +-- tests/ExampleClass.php | 59 +++++++++++ tests/tamer_standalone.php | 2 +- tests/worker.php | 2 +- 15 files changed, 148 insertions(+), 178 deletions(-) create mode 100644 .idea/scopes/TamerLib.xml create mode 100644 .idea/scopes/Test_Directory.xml create mode 100644 tests/ExampleClass.php diff --git a/.idea/scopes/TamerLib.xml b/.idea/scopes/TamerLib.xml new file mode 100644 index 0000000..02ab785 --- /dev/null +++ b/.idea/scopes/TamerLib.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/scopes/Test_Directory.xml b/.idea/scopes/Test_Directory.xml new file mode 100644 index 0000000..e23b24a --- /dev/null +++ b/.idea/scopes/Test_Directory.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/TamerLib/Classes/Utilities.php b/src/TamerLib/Classes/Utilities.php index f3cc29f..09f5e48 100644 --- a/src/TamerLib/Classes/Utilities.php +++ b/src/TamerLib/Classes/Utilities.php @@ -1,17 +1,21 @@ -. Otherwise, the process name will be + * net.nosial.tamerlib. * - * @param mixed $data * @return string */ - public static function serialize($data) + public static function getName(): string { - SerializableClosure::enterContext(); - SerializableClosure::wrapClosures($data); - $data = serialize($data); - SerializableClosure::exitContext(); - return $data; + if(tm::getMode() === TamerMode::WORKER) + { + if(self::$pid === null) + { + // Very unlikely that getmypid() will return 0. + /** @noinspection ProperNullCoalescingOperatorUsageInspection */ + self::$pid = getmypid() ?? (string)null; + } + + return sprintf('net.nosial.tamerlib:%s', self::$pid); + } + + return 'net.nosial.tamerlib'; } + /** - * Unserialize + * Returns the latest output from the given process either from the output or error output. + * If no output is available, an empty string is returned. * - * @param string $data - * @param array|null $options - * @return mixed + * @param Process $process + * @return string */ - public static function unserialize($data, array $options = null) + public static function getLatestOutput(Process $process): string { - SerializableClosure::enterContext(); - $data = $options === null || PHP_MAJOR_VERSION < 7 ? unserialize($data) : unserialize($data, $options); - SerializableClosure::unwrapClosures($data); - SerializableClosure::exitContext(); - return $data; + $output = $process->getIncrementalOutput(); + $error_output = $process->getIncrementalErrorOutput(); + + if (!empty($error_output)) + { + $output .= PHP_EOL . $error_output; + } + + return empty($output) ? (string)null : $output; } } \ No newline at end of file diff --git a/src/TamerLib/Classes/WorkerSupervisor.php b/src/TamerLib/Classes/WorkerSupervisor.php index f98afe9..3341a9e 100644 --- a/src/TamerLib/Classes/WorkerSupervisor.php +++ b/src/TamerLib/Classes/WorkerSupervisor.php @@ -11,6 +11,7 @@ use TamerLib\Objects\ServerConfiguration; use TamerLib\Objects\WorkerConfiguration; use TamerLib\Objects\WorkerInstance; + use TamerLib\tm; class WorkerSupervisor { diff --git a/src/TamerLib/Classes/subproc b/src/TamerLib/Classes/subproc index efdae81..76759ea 100644 --- a/src/TamerLib/Classes/subproc +++ b/src/TamerLib/Classes/subproc @@ -1,28 +1,9 @@ -getMessage(), $e); - } - } \ No newline at end of file + \TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::WORKER); + \TamerLib\tm::run(); \ No newline at end of file diff --git a/src/TamerLib/Enums/JobStatus.php b/src/TamerLib/Enums/JobStatus.php index f7d3c30..9c8c2f8 100644 --- a/src/TamerLib/Enums/JobStatus.php +++ b/src/TamerLib/Enums/JobStatus.php @@ -4,20 +4,27 @@ final class JobStatus { + /** + * The Job is waiting to be picked up by a worker. + * Processing Mode + */ public const WAITING = 10; + + /** + * The Job is currently being processed by a worker. + * Processing Mode + */ public const PROCESSING = 20; + + /** + * The Job has been finished by a worker. + * Finished Mode + */ public const FINISHED = 30; + + /** + * The Job has failed to be processed by a worker. + * Finished Mode + */ public const FAILED = 40; - public const REJECTED = 50; - - public const PROCESSING_STATES = [ - self::WAITING, - self::PROCESSING, - self::REJECTED // This is a special case, as the job gets pushed back into the queue for reprocessing. - ]; - - public const FINISHED_STATES = [ - self::FINISHED, - self::FAILED, - ]; } \ No newline at end of file diff --git a/src/TamerLib/Enums/TamerMode.php b/src/TamerLib/Enums/TamerMode.php index 3edb5a1..b2f3c78 100644 --- a/src/TamerLib/Enums/TamerMode.php +++ b/src/TamerLib/Enums/TamerMode.php @@ -8,6 +8,8 @@ public const WORKER = 'worker'; + public const NONE = 'none'; + public const ALL = [ self::CLIENT, self::WORKER diff --git a/src/TamerLib/Exceptions/JobManagerException.php b/src/TamerLib/Exceptions/JobManagerException.php index b6c5e17..5e1490d 100644 --- a/src/TamerLib/Exceptions/JobManagerException.php +++ b/src/TamerLib/Exceptions/JobManagerException.php @@ -2,9 +2,10 @@ namespace TamerLib\Exceptions; + use Exception; use Throwable; - class JobManagerException extends \Exception + class JobManagerException extends Exception { /** * @param string $message diff --git a/src/TamerLib/Objects/JobPacket.php b/src/TamerLib/Objects/JobPacket.php index 7f04ca3..2139ffe 100644 --- a/src/TamerLib/Objects/JobPacket.php +++ b/src/TamerLib/Objects/JobPacket.php @@ -6,7 +6,6 @@ use Exception; use TamerLib\Enums\JobStatus; - use TamerLib\Enums\JobType; class JobPacket { @@ -20,11 +19,6 @@ */ private $channel; - /** - * @var int - */ - private $job_type; - /** * @var string */ @@ -60,11 +54,6 @@ */ private $return_channel; - /** - * @var int - */ - private $created_at; - /** * @param array $data * @throws Exception @@ -73,7 +62,6 @@ { $this->id = $data['id'] ?? random_int(1000000000, 9999999999); $this->channel = $data['channel'] ?? 0; - $this->job_type = $data['job_type'] ?? JobType::CLOSURE; $this->status = $data['status'] ?? JobStatus::WAITING; $this->worker_id = $data['worker_id'] ?? null; $this->parameters = $data['parameters'] ?? null; @@ -81,7 +69,6 @@ $this->return_value = $data['return_value'] ?? null; $this->exception = $data['exception'] ?? null; $this->return_channel = $data['return_channel'] ?? null; - $this->created_at = $data['created_at'] ?? time(); } /** @@ -94,23 +81,6 @@ return (int)$this->id; } - /** - * Sets the ID of the JobPacket, if null is passed, a random ID will be generated - * - * @param int|null $id - * @throws Exception - */ - public function setId(?int $id=null): void - { - if($id !== null) - { - $this->id = random_int(1000000000, 9999999999); - return; - } - - $this->id = (int)$id; - } - /** * Returns the channel the JobPacket is assigned to * @@ -140,24 +110,6 @@ $this->channel = (int)$channel; } - /** - * Returns the Job Type of the JobPacket - * - * @return int - */ - public function getJobType(): int - { - return (int)$this->job_type; - } - - /** - * @param int $job_type - */ - public function setJobType(int $job_type): void - { - $this->job_type = $job_type; - } - /** * @return int */ @@ -166,14 +118,6 @@ return (int)$this->status; } - /** - * @param int $status - */ - public function setStatus(int $status): void - { - $this->status = (int)$status; - } - /** * @return string|null */ @@ -182,14 +126,6 @@ return (string)$this->worker_id; } - /** - * @param string|null $worker_id - */ - public function setWorkerId(?string $worker_id): void - { - $this->worker_id = $worker_id; - } - /** * @return mixed|null */ @@ -230,14 +166,6 @@ return $this->return_value; } - /** - * @param mixed|null $return_value - */ - public function setReturnValue(mixed $return_value): void - { - $this->return_value = $return_value; - } - /** * @return string|null */ @@ -246,14 +174,6 @@ return $this->exception; } - /** - * @param string|null $exception - */ - public function setException(?string $exception): void - { - $this->exception = $exception; - } - /** * @return string|null */ @@ -271,22 +191,6 @@ } - /** - * @return int|mixed - */ - public function getCreatedAt(): mixed - { - return $this->created_at; - } - - /** - * @param int|mixed $created_at - */ - public function setCreatedAt(mixed $created_at): void - { - $this->created_at = $created_at; - } - /** * Returns an array representation of the JobPacket * @@ -297,7 +201,6 @@ return [ 'id' => $this->getId(), 'channel' => $this->getChannel(), - 'job_type' => $this->getJobType(), 'status' => $this->getStatus(), 'worker_id' => $this->getWorkerId(), 'parameters' => $this->getParameters(), @@ -305,7 +208,6 @@ 'return_value' => $this->getReturnValue(), 'exception' => $this->getException(), 'return_channel' => $this->getReturnChannel(), - 'created_at' => $this->getCreatedAt() ]; } } \ No newline at end of file diff --git a/src/TamerLib/Objects/ServerConfiguration.php b/src/TamerLib/Objects/ServerConfiguration.php index 4fafa52..6fed427 100644 --- a/src/TamerLib/Objects/ServerConfiguration.php +++ b/src/TamerLib/Objects/ServerConfiguration.php @@ -8,7 +8,6 @@ use LogLib\Log; use RuntimeException; use TamerLib\Classes\Utilities; - use TamerLib\Exceptions\NoAvailablePortException; class ServerConfiguration { @@ -55,7 +54,7 @@ } catch(Exception $e) { - Log::warning(Utilities::getName(), 'No available port found. Using random port.'); + Log::warning(Utilities::getName(), 'No available port found. Using random port.', $e); try { diff --git a/src/TamerLib/Objects/WorkerConfiguration.php b/src/TamerLib/Objects/WorkerConfiguration.php index 6834ad8..c9ec179 100644 --- a/src/TamerLib/Objects/WorkerConfiguration.php +++ b/src/TamerLib/Objects/WorkerConfiguration.php @@ -89,14 +89,6 @@ return $this->worker_id; } - /** - * @param string $worker_id - */ - public function setWorkerId(string $worker_id): void - { - $this->worker_id = $worker_id; - } - /** * @return string */ diff --git a/src/TamerLib/tm.php b/src/TamerLib/tm.php index ba3306e..da11340 100644 --- a/src/TamerLib/tm.php +++ b/src/TamerLib/tm.php @@ -164,7 +164,7 @@ } catch(Exception $e) { - throw new ServerException('Failed to initialize the server.', 0, $e); + throw new ServerException('Failed to initialize the server.', $e); } } @@ -228,7 +228,7 @@ */ public static function monitor(int $timeout=0): void { - if($timeout > 0) + if($timeout > 0 || $timeout === -1) { try { @@ -293,6 +293,7 @@ self::$supervisor->spawnWorker($path, $count, $channel); } + var_dump('monitoring'); self::monitor(-1); } @@ -302,9 +303,9 @@ * @param string $function * @param array $arguments * @param int $channel - * @return mixed + * @return int */ - public static function do(string $function, array $arguments, int $channel=0): mixed + public static function do(string $function, array $arguments, int $channel=0): int { if(self::$mode !== TamerMode::CLIENT) { @@ -558,11 +559,11 @@ } /** - * Invokes the call() function, returns the Job ID. + * Invokes the do() function, returns the Job ID. * * @param string $name * @param array $arguments - * @return mixed + * @return int */ public static function __callStatic(string $name, array $arguments) { diff --git a/tests/ExampleClass.php b/tests/ExampleClass.php new file mode 100644 index 0000000..8efbf75 --- /dev/null +++ b/tests/ExampleClass.php @@ -0,0 +1,59 @@ +data = []; + } + + /** + * Sets a value in the data array + * + * @param string $key + * @param mixed $value + * @return void + */ + public function set(string $key, mixed $value): void + { + $this->data[$key] = $value; + } + + /** + * Gets a value from the data array + * + * @param string $key + * @return mixed + */ + public function get(string $key): mixed + { + return $this->data[$key]; + } + + /** + * Checks if a key exists in the data array + * + * @param string $key + * @return bool + */ + public function exists(string $key): bool + { + return isset($this->data[$key]); + } + + /** + * @return void + */ + public function clear(): void + { + $this->data = []; + } + } \ No newline at end of file diff --git a/tests/tamer_standalone.php b/tests/tamer_standalone.php index bf35e27..f13c94d 100644 --- a/tests/tamer_standalone.php +++ b/tests/tamer_standalone.php @@ -10,7 +10,7 @@ use TamerLib\tm; // Start as client mode, if no configuration is passed on then // Tamer will spawn its own Redis server and use it. - tm::initalize(TamerMode::CLIENT); + tm::initialize(TamerMode::CLIENT); tm::createWorker(20, __DIR__ . DIRECTORY_SEPARATOR . 'worker.php'); $total_sleep = 0; diff --git a/tests/worker.php b/tests/worker.php index 3f2d2af..bb94c34 100644 --- a/tests/worker.php +++ b/tests/worker.php @@ -5,7 +5,7 @@ import('net.nosial.tamerlib'); // Initialize as a worker, will fail if the process is executed directly - \TamerLib\tm::initalize(\TamerLib\Enums\TamerMode::WORKER); + \TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::WORKER); // Callback Examples \TamerLib\tm::addFunction('sleep', function($sleep_time){