Skip to content

Commit

Permalink
Optimize terminal layout
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Feb 1, 2025
1 parent da927b3 commit ffbacee
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RIP_HTTP_LISTEN=http://127.0.0.1:8008
RIP_HTTP_LISTEN=http://127.0.0.1:8000
RIP_HTTP_WORKERS=1
RIP_HTTP_RELOAD=1
RIP_WATCH=0
47 changes: 18 additions & 29 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace Laravel\Ripple;

use Illuminate\Support\Facades\Config;
use Laravel\Ripple\Virtual\Virtual;
use Revolt\EventLoop\UnsupportedFeatureException;
use Ripple\Channel\Channel;
use Ripple\File\Lock;
use Ripple\Utils\Output;
use Throwable;

use function base_path;
use function cli_set_process_title;
Expand All @@ -27,6 +27,7 @@
use function Co\wait;
use function config_path;
use function file_exists;
use function fwrite;
use function shell_exec;
use function sprintf;
use function storage_path;
Expand All @@ -35,21 +36,22 @@
use const SIGINT;
use const SIGQUIT;
use const SIGTERM;
use const STDOUT;

class Client
{
/*** @var \Laravel\Ripple\Inspector */
public readonly Inspector $inspector;

/*** @var \Laravel\Ripple\Virtual\Virtual */
public readonly Virtual $virtual;

/*** @var \Ripple\Channel\Channel */
public readonly Channel $channel;

/*** @var \Ripple\File\Lock */
public readonly Lock $lock;

/*** @var \Laravel\Ripple\Virtual\Virtual */
public Virtual $virtual;

/*** @var bool */
public bool $owner = false;

Expand Down Expand Up @@ -96,8 +98,10 @@ public function start(bool $daemon = false): void
$this->owner = true;
$this->lock->exclusion(false);
$this->virtual = new Virtual(__DIR__ . '/Virtual/server.bin.php');
$this->virtual->launch();
$this->virtual->session->onMessage = static fn (string $content) => Output::writeln($content);
$this->virtual->launch([
'RIP_WATCH' => Config::get('ripple.RIP_WATCH')
]);
$this->virtual->session->onMessage = static fn (string $content) => fwrite(STDOUT, $content);
$this->virtual->session->onErrorMessage = static fn (string $content) => Output::error($content);

$this->channel = channel(base_path(), true);
Expand All @@ -107,37 +111,22 @@ public function start(bool $daemon = false): void
$command = $this->channel->receive();
switch ($command) {
case 'stop':
$this->virtual->channel->send('stop');
try {
\Co\sleep(0.1);
if ($this->virtual->session->getStatus('running')) {
\Co\sleep(1);
$this->virtual->session->inputSignal(SIGINT);
}
} catch (Throwable) {
}
$this->virtual->stop();
exit(0);
break;

case 'reload':
$oldVirtual = $this->virtual;
fwrite(STDOUT, "\033c");

$oldVirtual = $this->virtual;
$virtual = new Virtual(__DIR__ . '/Virtual/server.bin.php');
$virtual->launch();
$virtual->launch([
'RIP_WATCH' => Config::get('ripple.RIP_WATCH')
]);
$this->virtual = $virtual;
$this->virtual->session->onMessage = static fn (string $content) => Output::writeln($content);
$this->virtual->session->onMessage = static fn (string $content) => fwrite(STDOUT, $content);
$this->virtual->session->onErrorMessage = static fn (string $content) => Output::error($content);
$oldVirtual->channel->send('stop');

try {
\Co\sleep(0.1);
if ($oldVirtual->session->getStatus('running')) {
\Co\sleep(1);
$oldVirtual->session->inputSignal(SIGINT);
}
} catch (Throwable) {
}

$oldVirtual->stop();
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ripple.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
return [
'HTTP_LISTEN' => Env::get('RIP_HTTP_LISTEN', 'http://127.0.0.1:8008'),
'HTTP_WORKERS' => Env::get('RIP_HTTP_WORKERS', 1),
'HTTP_RELOAD' => Env::get('RIP_HTTP_RELOAD', 1)
'HTTP_WATCH' => Env::get('RIP_WATCH', 0)
];
57 changes: 31 additions & 26 deletions src/Octane/Bin/ripple-server.bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use Illuminate\Contracts\Container\BindingResolutionException;
use Laravel\Octane\ApplicationFactory;
use Laravel\Octane\RequestContext;
use Laravel\Octane\Worker;
use Laravel\Ripple\Factory;
use Laravel\Ripple\HttpWorker;
use Laravel\Ripple\Octane\RippleClient;
use Laravel\Ripple\Util;
use Revolt\EventLoop\UnsupportedFeatureException;
use Ripple\Http\Server\Request;
use Ripple\Utils\Output;
Expand All @@ -34,12 +36,12 @@
\define("RIP_HOST", \getenv('RIP_HOST'));
\define("RIP_PORT", \getenv('RIP_PORT'));
\define("RIP_WORKERS", \getenv('RIP_WORKERS'));
\define("RIP_RELOAD", \getenv('RIP_RELOAD'));
\define("RIP_WATCH", \getenv('RIP_WATCH'));

require RIP_PROJECT_PATH . '/vendor/autoload.php';

/*** Octane part */
$octaneWorker = new \Laravel\Octane\Worker(
$octaneWorker = new Worker(
new ApplicationFactory(RIP_PROJECT_PATH),
$octaneClient = new RippleClient()
);
Expand All @@ -53,7 +55,7 @@
$application,
'http://' . RIP_HOST . ':' . RIP_PORT,
\intval(RIP_WORKERS),
\boolval(RIP_RELOAD)
\boolval(RIP_WATCH)
);
$application->singleton(Manager::class, static fn () => $manager);
$application->singleton(HttpWorker::class, static fn () => $httpWorker);
Expand All @@ -67,60 +69,63 @@
...$octaneClient->marshalRequest(new RequestContext(['rippleRequest' => $rippleRequest]))
);
});
$manager->addWorker($httpWorker);
$manager->add($httpWorker);

/*** Hot reload part */
$projectChannel = channel(RIP_PROJECT_PATH);
$includedFiles = \get_included_files();
$hotReload = static function (string $file) use ($manager, $includedFiles, $projectChannel) {
if (\in_array($file, $includedFiles, true)) {
\fwrite(\STDOUT, "\033c");
$projectChannel->send('reload');
} else {
$manager->reload();
$date = \date('Y-m-d H:i:s');
\is_file($file) && Output::writeln("[{$date}] {$file} has been modified");
\is_file($file)
&& ($file = Util::getRelativePath($file, RIP_PROJECT_PATH))
&& Output::writeln("[{$date}] {$file} has been modified");
}
};

$hotReloadWatch = Factory::createMonitor();
$hotReloadWatch->onModify = $hotReload;
$hotReloadWatch->onTouch = $hotReload;
$hotReloadWatch->onRemove = $hotReload;
if (\boolval(RIP_RELOAD)) {
if (\boolval(RIP_WATCH)) {
$hotReloadWatch->run();
}

/*** Guardian part*/
async(static function () use ($manager) {
$channel = channel(RIP_VIRTUAL_ID, true);
try {
onSignal(\SIGINT, function () use ($manager) {
$manager->stop();
exit(0);
});

onSignal(\SIGTERM, function () use ($manager) {
$manager->stop();
exit(0);
});

onSignal(\SIGQUIT, function () use ($manager) {
$manager->stop();
exit(0);
});
} catch (UnsupportedFeatureException) {
Output::warning('Failed to register signal handler');
}

while (1) {
$control = $channel->receive();
if ($control === 'stop') {
$manager->stop();
$manager->terminate();
exit(0);
}
}
});

try {
onSignal(\SIGINT, function () use ($manager) {
$manager->terminate();
exit(0);
});

onSignal(\SIGTERM, function () use ($manager) {
$manager->terminate();
exit(0);
});

onSignal(\SIGQUIT, function () use ($manager) {
$manager->terminate();
exit(0);
});
} catch (UnsupportedFeatureException) {
Output::warning('Failed to register signal handler');
}

/*** start */
Output::info("[laravel-ripple]", 'started');
$manager->run();
Expand Down
16 changes: 5 additions & 11 deletions src/Octane/Bin/ripple-ware.bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,23 @@

$virtual = new Virtual($imagePath);
$virtual->launch();
$virtual->session->onMessage = static fn (string $content) => Output::writeln($content);
$virtual->session->onMessage = static fn (string $content) => \fwrite(\STDOUT, $content);
$virtual->session->onErrorMessage = static fn (string $content) => Output::error($content);

$virtualStop = static function () use (&$virtual) {
$virtual->channel->send('stop');
try {
\Co\sleep(0.1);
if ($virtual->session->getStatus('running')) {
\Co\sleep(1);
$virtual->session->inputSignal(\SIGINT);
}
} catch (Throwable) {
}
$virtual->stop();
exit(0);
};

$virtualReboot = static function () use (&$virtual, $imagePath) {
\fwrite(\STDOUT, "\033c");

$oldVirtual = $virtual;
$_virtual = new Virtual($imagePath);
$_virtual->launch();
$oldVirtual->channel->send('stop');
$virtual = $_virtual;
$virtual->session->onMessage = static fn (string $content) => Output::writeln($content);
$virtual->session->onMessage = static fn (string $content) => \fwrite(\STDOUT, $content);
$virtual->session->onErrorMessage = static fn (string $content) => Output::error($content);
};

Expand Down
6 changes: 3 additions & 3 deletions src/Octane/Commands/RippleStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public function handle(RippleServerProcessInspector $inspector): int
'RIP_PROJECT_PATH' => base_path(),
'RIP_BIN_WORKING_PATH' => $binPath,
'APP_BASE_PATH' => base_path(),
'RIP_HOST' => $this->option('host') ?? '127.0.0.1',
'RIP_PORT' => $this->option('port') ?? 8000,
'RIP_HOST' => $this->getHost(),
'RIP_PORT' => $this->getPort(),
'RIP_WORKERS' => $workers,
'RIP_RELOAD' => $watch ?? 0
'RIP_WATCH' => $watch ?? 0
]
);
$process->start();
Expand Down
52 changes: 52 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/**
* Copyright © 2024 cclilshy
* Email: jingnigg@gmail.com
*
* This software is licensed under the MIT License.
* For full license details, please visit: https://opensource.org/licenses/MIT
*
* By using this software, you agree to the terms of the license.
* Contributions, suggestions, and feedback are always welcome!
*/

namespace Laravel\Ripple;

use Throwable;

use function realpath;
use function rtrim;
use function str_starts_with;
use function strlen;
use function substr;

use const DIRECTORY_SEPARATOR;

class Util
{
/**
* @param $path1
* @param $path2
*
* @return string
*/
public static function getRelativePath($path1, $path2): string
{
try {
$path1 = rtrim(realpath($path1), DIRECTORY_SEPARATOR);
$path2 = rtrim(realpath($path2), DIRECTORY_SEPARATOR);

if (!$path1 || !$path2) {
return '';
}

if (str_starts_with($path1, $path2)) {
return substr($path1, strlen($path2) + 1);
}
} catch (Throwable $e) {
return '';
}

return '';
}
}
Loading

0 comments on commit ffbacee

Please sign in to comment.