Build hot reload artifacts before worker restart
This commit is contained in:
@@ -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')) {
|
||||
|
||||
Reference in New Issue
Block a user