Refactored WorkerSupervisor & subproc

This commit is contained in:
Netkas 2023-06-16 03:48:23 -04:00
parent 13cade66cc
commit 21d0075ed5
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
2 changed files with 21 additions and 18 deletions

View file

@ -11,7 +11,6 @@
use TamerLib\Objects\ServerConfiguration;
use TamerLib\Objects\WorkerConfiguration;
use TamerLib\Objects\WorkerInstance;
use TamerLib\tm;
class WorkerSupervisor
{
@ -37,8 +36,8 @@
/**
* Generates a worker configuration object.
*
* @param int $channel
* @return WorkerConfiguration
* @param int $channel The channel to use for the worker.
* @return WorkerConfiguration The generated worker configuration.
*/
private function generateWorkerConfiguration(int $channel=0): WorkerConfiguration
{
@ -55,12 +54,12 @@
/**
* Spawns a specified number of workers for a given path.
*
* @param string $path
* @param int $count
* @param int $channel
* @param bool $check
* @param string $path Optional. The path to the worker file, defaults to subproc.
* @param int $count Optional. The number of workers to spawn, defaults to 8.
* @param int $channel Optional. The channel to use for the workers, defaults to 0.
* @param bool $check Optional. Whether to check if the workers have started, defaults to true.
* @throws WorkerFailedException Thrown if the worker fails to start.
* @return void
* @throws WorkerFailedException
*/
public function spawnWorker(string $path, int $count=8, int $channel=0, bool $check=true): void
{
@ -70,7 +69,6 @@
}
Log::verbose(Utilities::getName(), sprintf('Spawning %s workers for %s', $count, $path));
$spawned_workers = [];
for($i = 0; $i < $count; $i++)
@ -91,9 +89,9 @@
/**
* Checks if the workers have started.
*
* @param array $workers
* @param array $workers The workers to check.
* @throws WorkerFailedException Thrown if the worker fails to start.
* @return void
* @throws WorkerFailedException
*/
private function checkWorkers(array $workers): void
{
@ -125,10 +123,10 @@
/**
* Spawns a closure as a worker process. (built-in worker)
*
* @param int $count
* @param int $channel
* @param int $count The number of workers to spawn.
* @param int $channel The channel to use for the workers.
* @throws WorkerFailedException Thrown if the worker fails to start.
* @return void
* @throws WorkerFailedException
*/
public function spawnClosure(int $count=8, int $channel=0): void
{
@ -145,6 +143,11 @@
*/
public function printUpdates(): void
{
if(count($this->workers) === 0)
{
return;
}
/** @var WorkerInstance $worker */
foreach($this->workers as $worker)
{

View file

@ -1,8 +1,8 @@
<?PHP /** @noinspection PhpFullyQualifiedNameUsageInspection */
/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** @noinspection PhpFullyQualifiedNameUsageInspection */
<?PHP
require 'ncc';
/** @noinspection PhpFullyQualifiedNameUsageInspection */
require 'ncc';
import('net.nosial.tamerlib');
\TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::WORKER);