Skip to content

Commit

Permalink
detect grpc fork support
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Jul 10, 2024
1 parent 9a17344 commit fb5660f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/Environment/Fork/ForkWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
*/
class ForkWorker extends SocketWorker
{
/**
* @return bool
*/
public static function isSupported(): bool
{
if (!extension_loaded("pcntl")) {
return false;
}

// GRPC only supports forks if explicitly enabled using the GRPC_ENABLE_FORK_SUPPORT environment variable
// see https://github.com/grpc/grpc/issues/13412
// If you don't use GRPC, but have the extension loaded, you can always define your workers manually
// or override the automatic worker selection using the TASKMASTER_WORKER environment variable
if (extension_loaded("grpc") && getenv("GRPC_ENABLE_FORK_SUPPORT") !== "1") {
return false;
}

return true;
}

/**
* @return WorkerInstanceInterface
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Environment/Process/ProcessWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class ProcessWorker extends SocketWorker
{
/**
* @return bool
*/
public static function isSupported(): bool
{
return function_exists("proc_open") && PHP_OS_FAMILY !== "Windows";
}

/**
* @return ProcessWorkerInstance
*/
Expand Down
5 changes: 2 additions & 3 deletions src/Taskmaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ protected function getSelectableStreams(): array
*
* @param SocketInterface|null $socket
* @return resource|null
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
protected function getSelectableReadStreamFromSocket(?SocketInterface $socket): mixed
{
Expand Down Expand Up @@ -463,9 +462,9 @@ public function autoDetectWorkers(int $count, bool $loadFromEnv = true, bool $al
}

if (!$worker) {
if (extension_loaded("pcntl")) {
if (ForkWorker::isSupported()) {
$worker = new ForkWorker();
} elseif (function_exists("proc_open") && PHP_OS_FAMILY !== "Windows") {
} elseif (ProcessWorker::isSupported()) {
$worker = new ProcessWorker();
} else {
$worker = new SyncWorker();
Expand Down

0 comments on commit fb5660f

Please sign in to comment.