Support Swow for watcher (#5164)

This commit is contained in:
Deeka Wong 2022-10-29 09:47:26 +08:00 committed by GitHub
parent 3f779abf69
commit 2938c296fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 77 additions and 25 deletions

View File

@ -187,7 +187,8 @@
"src/nats/src/Functions.php",
"src/translation/src/Functions.php",
"src/utils/src/Functions.php",
"src/view-engine/src/Functions.php"
"src/view-engine/src/Functions.php",
"src/watcher/src/Functions.php"
],
"psr-4": {
"Hyperf\\Amqp\\": "src/amqp/src/",

View File

@ -22,6 +22,7 @@ if (file_exists($dir . '/vendor/autoload.php')) {
$basePath = $dir;
}
! defined('SWOOLE_HOOK_ALL') && define('SWOOLE_HOOK_ALL', 0);
! defined('BASE_PATH') && define('BASE_PATH', $basePath);
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);

View File

@ -14,6 +14,9 @@
"hyperf/framework": "~3.0.0"
},
"autoload": {
"files": [
"src/Functions.php"
],
"psr-4": {
"Hyperf\\Watcher\\": "src/"
}

View File

@ -14,7 +14,8 @@ namespace Hyperf\Watcher\Driver;
use Hyperf\Engine\Channel;
use Hyperf\Utils\Str;
use Hyperf\Watcher\Option;
use Swoole\Coroutine\System;
use function Hyperf\Watcher\exec;
class FindDriver extends AbstractDriver
{
@ -27,16 +28,16 @@ class FindDriver extends AbstractDriver
parent::__construct($option);
if ($this->isDarwin()) {
$ret = System::exec('which gfind');
$ret = exec('which gfind');
if (empty($ret['output'])) {
throw new \InvalidArgumentException('gfind not exists. You can `brew install findutils` to install it.');
}
} else {
$ret = System::exec('which find');
$ret = exec('which find');
if (empty($ret['output'])) {
throw new \InvalidArgumentException('find not exists.');
}
$ret = System::exec('find --help', true);
$ret = exec('find --help');
$this->isSupportFloatMinutes = ! str_contains($ret['output'] ?? '', 'BusyBox');
}
}
@ -73,7 +74,7 @@ class FindDriver extends AbstractDriver
{
$changedFiles = [];
$dest = implode(' ', $targets);
$ret = System::exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -print');
$ret = exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -print');
if ($ret['code'] === 0 && strlen($ret['output'])) {
$stdout = trim($ret['output']);

View File

@ -14,7 +14,8 @@ namespace Hyperf\Watcher\Driver;
use Hyperf\Engine\Channel;
use Hyperf\Utils\Str;
use Hyperf\Watcher\Option;
use Swoole\Coroutine\System;
use function Hyperf\Watcher\exec;
class FindNewerDriver extends AbstractDriver
{
@ -27,13 +28,13 @@ class FindNewerDriver extends AbstractDriver
public function __construct(protected Option $option)
{
parent::__construct($option);
$ret = System::exec('which find');
$ret = exec('which find');
if (empty($ret['output'])) {
throw new \InvalidArgumentException('find not exists.');
}
// create two files
System::exec('echo 1 > ' . $this->getToModifyFile());
System::exec('echo 1 > ' . $this->getToScanFile());
exec('echo 1 > ' . $this->getToModifyFile());
exec('echo 1 > ' . $this->getToScanFile());
}
public function watch(Channel $channel): void
@ -48,8 +49,8 @@ class FindNewerDriver extends AbstractDriver
++$this->count;
// update mtime
if ($changedFiles) {
System::exec('echo 1 > ' . $this->getToModifyFile());
System::exec('echo 1 > ' . $this->getToScanFile());
exec('echo 1 > ' . $this->getToModifyFile());
exec('echo 1 > ' . $this->getToScanFile());
}
foreach ($changedFiles as $file) {
@ -75,7 +76,7 @@ class FindNewerDriver extends AbstractDriver
$shell = $shell . sprintf('find %s -newer %s -type f', $dest, $file) . $symbol;
}
$ret = System::exec($shell);
$ret = exec($shell);
if ($ret['code'] === 0 && strlen($ret['output'])) {
$stdout = $ret['output'];
$lineArr = explode(PHP_EOL, $stdout);

View File

@ -15,7 +15,8 @@ use Hyperf\Engine\Channel;
use Hyperf\Engine\Coroutine;
use Hyperf\Utils\Str;
use Hyperf\Watcher\Option;
use Swoole\Coroutine\System;
use function Hyperf\Watcher\exec;
class FswatchDriver extends AbstractDriver
{
@ -24,7 +25,7 @@ class FswatchDriver extends AbstractDriver
public function __construct(protected Option $option)
{
parent::__construct($option);
$ret = System::exec('which fswatch');
$ret = exec('which fswatch');
if (empty($ret['output'])) {
throw new \InvalidArgumentException('fswatch not exists. You can `brew install fswatch` to install it.');
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Watcher;
use RuntimeException;
if (function_exists('exec')) {
/**
* @return mixed
*/
function exec(string $command)
{
if (class_exists(\Swoole\Coroutine\System::class)) {
return \Swoole\Coroutine\System::exec($command);
}
if (function_exists('\exec')) {
\exec($command, $output, $code);
$output = implode(PHP_EOL, $output);
return compact('code', 'output');
}
if (function_exists('\passthru')) {
ob_start();
\passthru($command, $code);
$output = ob_get_clean();
ob_end_clean();
return compact('code', 'output');
}
throw new RuntimeException('No available function to run command.');
}
}

View File

@ -21,8 +21,6 @@ use Hyperf\Utils\Filesystem\Filesystem;
use Hyperf\Watcher\Driver\DriverInterface;
use PhpParser\PrettyPrinter\Standard;
use Psr\Container\ContainerInterface;
use Swoole\Coroutine\System;
use Swoole\Process;
use Symfony\Component\Console\Output\OutputInterface;
class Watcher
@ -72,7 +70,7 @@ class Watcher
$this->restart(false);
}
} else {
$ret = System::exec(sprintf('%s %s/vendor/hyperf/watcher/collector-reload.php %s', $this->option->getBin(), BASE_PATH, $file));
$ret = exec(sprintf('%s %s/vendor/hyperf/watcher/collector-reload.php %s', $this->option->getBin(), BASE_PATH, $file));
if ($ret['code'] === 0) {
$this->output->writeln('Class reload success.');
} else {
@ -86,7 +84,7 @@ class Watcher
public function dumpautoload()
{
$ret = System::exec('composer dump-autoload -o --no-scripts -d ' . BASE_PATH);
$ret = exec('composer dump-autoload -o --no-scripts -d ' . BASE_PATH);
$this->output->writeln($ret['output'] ?? '');
}
@ -107,8 +105,8 @@ class Watcher
$pid = $this->filesystem->get($file);
try {
$this->output->writeln('Stop server...');
if (Process::kill((int) $pid, 0)) {
Process::kill((int) $pid, SIGTERM);
if (posix_kill((int) $pid, 0)) {
posix_kill((int) $pid, SIGTERM);
}
} catch (\Throwable) {
$this->output->writeln('Stop server failed. Please execute `composer dump-autoload -o`');

View File

@ -18,7 +18,8 @@ use Hyperf\Watcher\Driver\FswatchDriver;
use Hyperf\Watcher\Option;
use HyperfTest\Watcher\Stub\ContainerStub;
use PHPUnit\Framework\TestCase;
use Swoole\Coroutine\System;
use function Hyperf\Watcher\exec;
/**
* @internal
@ -34,7 +35,7 @@ class FswatchDriverTest extends TestCase
try {
$driver = new FswatchDriver($option);
Coroutine::create(fn () => $driver->watch($channel));
System::exec('echo 1 > /tmp/.env');
exec('echo 1 > /tmp/.env');
$this->assertStringEndsWith('.env', $channel->pop($option->getScanIntervalSeconds() + 0.1));
} catch (\InvalidArgumentException $e) {
if (str_contains($e->getMessage(), 'fswatch not exists')) {

View File

@ -19,7 +19,8 @@ use Hyperf\Watcher\Option;
use HyperfTest\Watcher\Stub\ContainerStub;
use HyperfTest\Watcher\Stub\ScanFileDriverStub;
use PHPUnit\Framework\TestCase;
use Swoole\Coroutine\System;
use function Hyperf\Watcher\exec;
/**
* @internal
@ -37,7 +38,7 @@ class ScanFileDriverTest extends TestCase
$driver->watch($channel);
System::exec('echo 1 > /tmp/.env');
exec('echo 1 > /tmp/.env');
$this->assertStringEndsWith('.env', $channel->pop($option->getScanIntervalSeconds() + 0.1));
$channel->close();
}