Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Console/RunServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Drift\Console\OutputPrinter;
use Drift\Server\Adapter\KernelAdapter;
use Drift\Server\Application;
use Drift\Server\ConsoleServerMessage;
use Drift\Server\ConsoleMasterServerMessage;
use Drift\Server\Context\ServerContext;
use Drift\Server\Mime\MimeTypeChecker;
use React\EventLoop\LoopInterface;
Expand Down Expand Up @@ -68,14 +68,15 @@ protected function executeServerCommand(
$filesystem
)
->then(function (KernelAdapter $kernelAdapter) use ($application, $outputPrinter, $serverContext, $filesystem, &$forceShutdownReference) {
(new ConsoleServerMessage('Kernel built.', '~', true))->print($outputPrinter);
(new ConsoleServerMessage('Kernel preloaded.', '~', true))->print($outputPrinter);
(new ConsoleServerMessage('Kernel ready to accept requests.', '~', true))->print($outputPrinter);
(new ConsoleMasterServerMessage('Kernel built.', true))->print($outputPrinter);
(new ConsoleMasterServerMessage('Kernel preloaded.', true))->print($outputPrinter);
(new ConsoleMasterServerMessage('Kernel ready to accept requests.', true))->print($outputPrinter);

if ($serverContext->hasExchanges()) {
(new ConsoleServerMessage('Kernel connected to exchanges.', '~', true))->print($outputPrinter);
(new ConsoleMasterServerMessage('Kernel connected to exchanges.', true))->print($outputPrinter);
}

(new ConsoleMasterServerMessage('...', true))->print($outputPrinter);
$applicationCallback = function () use ($application, $kernelAdapter, $filesystem, &$forceShutdownReference) {
$application->runServer(
$kernelAdapter,
Expand All @@ -90,9 +91,10 @@ protected function executeServerCommand(
$this->forkApplication($serverContext->getWorkers(), $applicationCallback, $outputPrinter);
}
}, function (Throwable $e) use ($outputPrinter) {
(new ConsoleServerMessage($e->getMessage(), '~', false))->print($outputPrinter);
(new ConsoleServerMessage('The server will shut down.', '~', false))->print($outputPrinter);
(new ConsoleServerMessage('Bye!', '~', false))->print($outputPrinter);
(new ConsoleMasterServerMessage('...', false))->print($outputPrinter);
(new ConsoleMasterServerMessage($e->getMessage(), false))->print($outputPrinter);
(new ConsoleMasterServerMessage('The server will shut down.', false))->print($outputPrinter);
(new ConsoleMasterServerMessage('Bye!', false))->print($outputPrinter);
});
}

Expand All @@ -117,7 +119,7 @@ private function forkApplication(

case 0:
$GLOBALS['number_of_process'] = str_pad(\strval($numberOfFork), 2, '0', STR_PAD_LEFT);
(new ConsoleServerMessage("Worker #$numberOfFork starting", '~', true))->print($outputPrinter);
(new ConsoleMasterServerMessage("Worker #$numberOfFork starting", true))->print($outputPrinter);
$callable();
break;

Expand Down
21 changes: 12 additions & 9 deletions src/Console/ServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
use Drift\EventLoop\EventLoopUtils;
use Drift\Server\Console\Style\Muted;
use Drift\Server\Console\Style\Purple;
use Drift\Server\ConsoleServerMessage;
use Drift\Server\ConsoleMasterServerMessage;
use Drift\Server\Context\ServerContext;
use Drift\Server\ServerHeaderPrinter;
use Exception;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -134,21 +135,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
$forceShutdownReference
);

(new ConsoleServerMessage('EventLoop is running.', '~', true))->print($outputPrinter);
(new ConsoleMasterServerMessage('EventLoop is running.', true))->print($outputPrinter);
EventLoopUtils::runLoop($loop, (\intval($input->getOption('allowed-loop-stops')) + 1), function (int $timesMissing) use ($outputPrinter, &$forceShutdownReference) {
if ($forceShutdownReference) {
(new ConsoleServerMessage(
sprintf('Loop forced to stop.'), '~', false)
(new ConsoleMasterServerMessage(
sprintf('Loop forced to stop.'), false)
)->print($outputPrinter);
} else {
(new ConsoleServerMessage(
sprintf('Rerunning EventLoop. %d retries missing', $timesMissing), '~', false)
(new ConsoleMasterServerMessage(
sprintf('Rerunning EventLoop. %d retries missing', $timesMissing), false)
)->print($outputPrinter);
}
}, $forceShutdownReference);
(new ConsoleServerMessage('EventLoop stopped.', '~', false))->print($outputPrinter);
(new ConsoleServerMessage('Closing the server.', '~', false))->print($outputPrinter);
(new ConsoleServerMessage('Bye bye!.', '~', false))->print($outputPrinter);
(new ConsoleMasterServerMessage('...', false))->print($outputPrinter);
(new ConsoleMasterServerMessage('EventLoop stopped.', false))->print($outputPrinter);
(new ConsoleMasterServerMessage('Closing the server.', false))->print($outputPrinter);
(new ConsoleMasterServerMessage('Bye bye!.', false))->print($outputPrinter);

return 0;
}
Expand Down Expand Up @@ -186,6 +188,7 @@ protected function createOutputPrinter(
$outputFormatter = $output->getFormatter();
$outputFormatter->setStyle('muted', new Muted());
$outputFormatter->setStyle('purple', new Purple());
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));

return new OutputPrinter($output, $isQuiet, $isAlmostQuiet);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Console/Style/Performance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Drift Server
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Drift\Server\Console\Style;

/**
* Class Purple.
*/
final class Performance extends CustomStyle
{
public function apply(string $text)
{
return sprintf("\033[01;95m%s\e[0m", $text);
}
}
64 changes: 64 additions & 0 deletions src/ConsoleMasterServerMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Drift Server
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Drift\Server;

use Drift\Console\OutputPrinter;

/**
* Class ConsoleMasterServerMessage.
*/
final class ConsoleMasterServerMessage implements Printable
{
private string $message;
private bool $ok;

/**
* @param string $message
* @param bool $ok
*/
public function __construct(
string $message,
bool $ok
) {
$this->message = $message;
$this->ok = $ok;
}

/**
* Print.
*
* @param OutputPrinter $outputPrinter
*/
public function print(OutputPrinter $outputPrinter)
{
if (!$outputPrinter->shouldPrintImportantOutput()) {
return;
}

$color = 'red';
if ($this->ok) {
$color = 'green';
}

$forkNumber = isset($GLOBALS['number_of_process'])
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
: '';
$outputPrinter->print("$forkNumber<fg=$color;options=bold>SRV</>");
$outputPrinter->print(' ');
$outputPrinter->print('<muted>'.$this->message.'</muted>');
$outputPrinter->printLine();
}
}
46 changes: 46 additions & 0 deletions src/ConsoleMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Drift Server
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Drift\Server;

abstract class ConsoleMessage implements Printable
{
/**
* @param string $elapsedType
*
* @return string
*/
protected function styledPerformance(string $elapsedType): string
{
$info = [
$this->toLength($elapsedType, str_contains($elapsedType, 'μ') ? 8 : 7),
$this->toLength((string) (int) (memory_get_usage() / 1048576), 4),
$this->toLength((string) (int) (memory_get_usage(true) / 1048576), 4),
];

return '<performance>['.implode('|', $info).']</performance>';
}

/**
* @param string $string
* @param int $length
*
* @return string
*/
protected function toLength(string $string, int $length): string
{
return str_pad($string, $length, ' ', STR_PAD_LEFT);
}
}
8 changes: 4 additions & 4 deletions src/ConsoleRequestMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Class ConsoleRequestMessage.
*/
final class ConsoleRequestMessage implements Printable
final class ConsoleRequestMessage extends ConsoleMessage
{
private string $url;
private string $method;
Expand Down Expand Up @@ -58,7 +58,7 @@ public function __construct(
*/
public function print(OutputPrinter $outputPrinter)
{
$hasError = \intval($this->code) >= 400;
$hasError = $this->code >= 400;
if (
!$outputPrinter->shouldPrintRegularOutput() &&
(!$outputPrinter->shouldPrintImportantOutput() || !$hasError)
Expand All @@ -74,12 +74,12 @@ public function print(OutputPrinter $outputPrinter)
$color = 'red';
}

$performance = $this->styledPerformance($this->elapsedTime);
$forkNumber = isset($GLOBALS['number_of_process'])
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
: '';
$outputPrinter->print("$forkNumber<fg=$color;options=bold>{$this->code}</>");
$outputPrinter->print(" $method $this->url ");
$outputPrinter->print('(<muted>'.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB</muted>)');
$outputPrinter->print(" $performance $method $this->url ");
if ($this->code >= 400) {
$outputPrinter->print(' - <muted>'.$this->messageInMessage($this->message).'</muted>');
}
Expand Down
6 changes: 3 additions & 3 deletions src/ConsoleServerMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Class ConsoleServerMessage.
*/
final class ConsoleServerMessage implements Printable
final class ConsoleServerMessage extends ConsoleMessage
{
private string $message;
private string $elapsedTime;
Expand Down Expand Up @@ -59,13 +59,13 @@ public function print(OutputPrinter $outputPrinter)
$color = 'green';
}

$performance = $this->styledPerformance($this->elapsedTime);
$forkNumber = isset($GLOBALS['number_of_process'])
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
: '';
$outputPrinter->print("$forkNumber<fg=$color;options=bold>SRV</>");
$outputPrinter->print(' ');
$outputPrinter->print('(<muted>'.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB</muted>)');
$outputPrinter->print(' - <muted>'.$this->message.'</muted>');
$outputPrinter->print(" $performance {$this->message}</muted>");
$outputPrinter->printLine();
}
}
6 changes: 3 additions & 3 deletions src/ConsoleStaticMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Class ConsoleStaticMessage.
*/
final class ConsoleStaticMessage implements Printable
final class ConsoleStaticMessage extends ConsoleMessage
{
private string $url;
private string $elapsedTime;
Expand Down Expand Up @@ -52,12 +52,12 @@ public function print(OutputPrinter $outputPrinter)

$method = str_pad('GET', 6, ' ');

$performance = $this->styledPerformance($this->elapsedTime);
$forkNumber = isset($GLOBALS['number_of_process'])
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
: '';
$outputPrinter->print("$forkNumber<purple>200</purple>");
$outputPrinter->print(" $method $this->url ");
$outputPrinter->print('(<muted>'.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB</muted>');
$outputPrinter->print(" $performance $method $this->url ");
$outputPrinter->printLine();
}
}
7 changes: 6 additions & 1 deletion tests/ApplicationStaticFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public function testRegular()
$this->waitForChange($process, $initialOutput);

$this->assertStringContainsString(
'200 GET',
'200',
$process->getOutput()
);

$this->assertStringContainsString(
' GET ',
$process->getOutput()
);

Expand Down
Loading