Compare commits
2
Commits
3fe9ab06c3
...
d3ca24c4c5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3ca24c4c5 | ||
|
|
ca68418e0d |
@@ -7,6 +7,7 @@ use Kiri\Di\Inject\Container;
|
||||
use Kiri\Error\StdoutLogger;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Router\Router;
|
||||
use Kiri\Router\RouteArtifactState;
|
||||
use Kiri\Server\Events\OnWorkerStart;
|
||||
use Kiri\Server\Processes\AbstractProcess;
|
||||
use Kiri\Server\Processes\OnProcessInterface;
|
||||
@@ -298,7 +299,12 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
}
|
||||
|
||||
di(HotReloadState::class)->store($changedFiles);
|
||||
di(StdoutLogger::class)->println('detected file changes, reloading server: ' . $preview);
|
||||
di(StdoutLogger::class)->println('detected file changes, building hot reload artifacts: ' . $preview);
|
||||
|
||||
if (!$this->buildChangedFiles($changedFiles)) {
|
||||
di(StdoutLogger::class)->println('hot reload build failed, keep old workers running');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->reloadWorkers()) {
|
||||
di(StdoutLogger::class)->println('reload server failed: master pid not found or signal failed');
|
||||
@@ -308,6 +314,54 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function buildChangedFiles(array $changedFiles): bool
|
||||
{
|
||||
$entry = $this->detectConsoleEntry();
|
||||
if ($entry === null) {
|
||||
di(StdoutLogger::class)->println('hot reload build failed: entry script not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
$artifactType = defined('ROUTER_TYPE_HTTP') ? ROUTER_TYPE_HTTP : 'http';
|
||||
$artifactState = di(RouteArtifactState::class);
|
||||
$artifactState->clearBuild($artifactType);
|
||||
|
||||
$php = PHP_BINARY ?: 'php';
|
||||
$command = escapeshellarg($php) . ' ' . escapeshellarg($entry) . ' sw:file-build';
|
||||
$output = [];
|
||||
$returnCode = 0;
|
||||
exec($command . ' 2>&1', $output, $returnCode);
|
||||
|
||||
foreach ($output as $line) {
|
||||
if ($line !== '') {
|
||||
di(StdoutLogger::class)->println('[file-build] ' . $line);
|
||||
}
|
||||
}
|
||||
|
||||
if ($returnCode !== 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $artifactState->coversChangedFiles($artifactType, $changedFiles);
|
||||
}
|
||||
|
||||
private function detectConsoleEntry(): ?string
|
||||
{
|
||||
$candidates = [
|
||||
$_SERVER['SCRIPT_FILENAME'] ?? '',
|
||||
defined('APP_PATH') ? APP_PATH . 'kiri.php' : '',
|
||||
defined('APP_PATH') ? APP_PATH . 'bin/kiri' : '',
|
||||
defined('APP_PATH') ? APP_PATH . 'artisan' : '',
|
||||
];
|
||||
|
||||
foreach ($candidates as $candidate) {
|
||||
if (is_string($candidate) && $candidate !== '' && is_file($candidate)) {
|
||||
return $candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
private function invalidateChangedFiles(array $changedFiles): void
|
||||
{
|
||||
if (!function_exists('opcache_invalidate')) {
|
||||
|
||||
@@ -7,6 +7,8 @@ use Swoole\Process;
|
||||
trait ReloadWorkers
|
||||
{
|
||||
|
||||
private static ?int $cachedMasterPid = null;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -22,6 +24,11 @@ trait ReloadWorkers
|
||||
|
||||
private function getMasterPid(): ?int
|
||||
{
|
||||
if (self::$cachedMasterPid !== null && $this->isCachedMasterPid(self::$cachedMasterPid)) {
|
||||
return self::$cachedMasterPid;
|
||||
}
|
||||
|
||||
self::$cachedMasterPid = null;
|
||||
$processes = $this->listProcesses();
|
||||
$candidates = [];
|
||||
|
||||
@@ -41,6 +48,7 @@ trait ReloadWorkers
|
||||
|> array_unique(...)
|
||||
|> array_values(...) as $pid) {
|
||||
if ($this->isProcessAlive($pid) && $this->isMasterPid($pid, $processes)) {
|
||||
self::$cachedMasterPid = $pid;
|
||||
return $pid;
|
||||
}
|
||||
}
|
||||
@@ -48,6 +56,26 @@ trait ReloadWorkers
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isCachedMasterPid(int $pid): bool
|
||||
{
|
||||
if (!$this->isProcessAlive($pid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$command = $this->readProcessCommand($pid);
|
||||
return $command !== '' && $this->isMasterProcessCommand($command) && $this->isSameAppProcess($pid);
|
||||
}
|
||||
|
||||
private function readProcessCommand(int $pid): string
|
||||
{
|
||||
$cmdline = @file_get_contents('/proc/' . $pid . '/cmdline');
|
||||
if (is_string($cmdline) && $cmdline !== '') {
|
||||
return trim(str_replace("\0", ' ', $cmdline));
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function isMasterPid(int $pid, array $processes): bool
|
||||
{
|
||||
foreach ($processes as $process) {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Kiri\Di\HotReloadState;
|
||||
use Kiri\Router\RouteArtifactState;
|
||||
use Kiri\Router\Router;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Throwable;
|
||||
|
||||
class FileBuildCommand extends Command
|
||||
{
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setName('sw:file-build')
|
||||
->setDescription('build hot reload artifacts for changed files');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
|
||||
putenv('KIRI_FILE_BUILD=1');
|
||||
|
||||
$changedFiles = di(HotReloadState::class)->peek();
|
||||
$startedAt = microtime(true);
|
||||
|
||||
try {
|
||||
$syntaxError = $this->lintChangedFiles($changedFiles);
|
||||
if ($syntaxError !== null) {
|
||||
$output->writeln('<error>file build failed: ' . $syntaxError . '</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
di(Router::class)->scan_build_route();
|
||||
di(HotReloadState::class)->store($changedFiles);
|
||||
di(RouteArtifactState::class)->markBuild(ROUTER_TYPE_HTTP, $changedFiles);
|
||||
|
||||
$elapsed = round((microtime(true) - $startedAt) * 1000, 2);
|
||||
$output->writeln(sprintf('file build complete: %d changed files, %sms', count($changedFiles), $elapsed));
|
||||
return Command::SUCCESS;
|
||||
} catch (Throwable $throwable) {
|
||||
if (class_exists('Kiri')) {
|
||||
\Kiri::getLogger()->json_log($throwable);
|
||||
}
|
||||
$output->writeln('<error>file build failed: ' . $throwable->getMessage() . '</error>');
|
||||
return Command::FAILURE;
|
||||
} finally {
|
||||
putenv('KIRI_FILE_BUILD');
|
||||
}
|
||||
}
|
||||
|
||||
private function lintChangedFiles(array $changedFiles): ?string
|
||||
{
|
||||
foreach ($changedFiles as $file) {
|
||||
if (!is_string($file) || !str_ends_with($file, '.php') || !is_file($file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = [];
|
||||
$returnCode = 0;
|
||||
exec(escapeshellarg(PHP_BINARY ?: 'php') . ' -l ' . escapeshellarg($file) . ' 2>&1', $output, $returnCode);
|
||||
if ($returnCode !== 0) {
|
||||
return implode("\n", $output);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,10 @@ class ServerProviders extends Providers
|
||||
public function onImport(): void
|
||||
{
|
||||
$server = $this->container->get(ServerCommand::class);
|
||||
$builder = $this->container->get(FileBuildCommand::class);
|
||||
|
||||
$console = $this->container->get(Application::class);
|
||||
$console->addCommand($server);
|
||||
$console->addCommand($builder);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user