From 6671e6026b9725c36df9956084a01046bcc0e349 Mon Sep 17 00:00:00 2001 From: Julien Mercier-Rojas Date: Wed, 8 Nov 2023 11:34:47 +0100 Subject: [PATCH 1/3] Replace current Makefile recipes with Castor tasks --- {castor-commands => .castor}/console.php | 3 +- .castor/docker.php | 41 ++++++++++++++++++++++++ Makefile | 14 -------- castor.php | 2 +- phpstan.neon.dist | 2 +- src/Console/LoadMessages.php | 2 +- 6 files changed, 46 insertions(+), 18 deletions(-) rename {castor-commands => .castor}/console.php (75%) create mode 100644 .castor/docker.php delete mode 100644 Makefile diff --git a/castor-commands/console.php b/.castor/console.php similarity index 75% rename from castor-commands/console.php rename to .castor/console.php index 3152bd6..2577c8f 100644 --- a/castor-commands/console.php +++ b/.castor/console.php @@ -22,6 +22,7 @@ function console( string $consoleCommand ): void { run( - 'docker-compose -f docker-compose.yml exec demo php -d max_execution_time=120 console.php ' . $consoleCommand + command: 'docker-compose -f docker-compose.yml exec demo php -d max_execution_time=0 console.php ' . $consoleCommand, + timeout: 0 ); } diff --git a/.castor/docker.php b/.castor/docker.php new file mode 100644 index 0000000..6df0adb --- /dev/null +++ b/.castor/docker.php @@ -0,0 +1,41 @@ + + * Created at: 08/11/2023 + */ + +declare(strict_types=1); + +namespace docker; + +use Castor\Attribute\AsTask; +use function Castor\run; +use function php\console; + +require_once __DIR__ . '/../vendor/autoload.php'; + +#[AsTask(description: 'Start docker containers')] +function up(): void { + run( + command: 'docker-compose -f docker-compose.yml up', + timeout: 0 + ); +} + +#[AsTask(description: 'Stop and clean docker containers')] +function down(): void { + run( + command: 'docker-compose -f docker-compose.yml down -v' + ); +} + +#[AsTask(description: 'Initialize rabbitmq configuration')] +function init(): void { + run( + command: 'docker-compose -f docker-compose.yml up -d rabbitmq' + ); + run( + command: 'docker-compose -f docker-compose.yml exec rabbitmq rabbitmqctl import_definitions /scripts/definitions.json' + ); +} diff --git a/Makefile b/Makefile deleted file mode 100644 index 483bb6e..0000000 --- a/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -.PHONY: down up install - -up: - @docker-compose up - -down: - @docker-compose down -v - -install: - @docker-compose up -d rabbitmq - @docker-compose exec rabbitmq rabbitmqctl import_definitions /scripts/definitions.json - -load-messages: - @docker-compose exec demo php pub/load-messages.php diff --git a/castor.php b/castor.php index 55e5ed6..d27c055 100644 --- a/castor.php +++ b/castor.php @@ -2,4 +2,4 @@ use function Castor\import; -import(__DIR__ . '/castor-commands'); +import(__DIR__ . '/.castor'); diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 1531801..f61b238 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -4,7 +4,7 @@ parameters: - src/ excludePaths: - castor.php - - castor-commands/* + - .castor/* includes: - vendor/phpstan/phpstan/conf/bleedingEdge.neon diff --git a/src/Console/LoadMessages.php b/src/Console/LoadMessages.php index 1810513..446f4cc 100644 --- a/src/Console/LoadMessages.php +++ b/src/Console/LoadMessages.php @@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $exchange = (string) getenv('RABBITMQ_EXCHANGE'); $batch = 100; - $max = 1000 * 1000; + $max = 1000 * 1000 * 10; for ($i = 0; $i < $max; $i++) { $message = $this->getMessage(); $channel->batch_basic_publish($message, $exchange, $message->getRoutingKey()); From 9ab3df1fe183c5ee464c0b211eabfbc61e6c03f7 Mon Sep 17 00:00:00 2001 From: Julien Mercier-Rojas Date: Wed, 8 Nov 2023 11:43:28 +0100 Subject: [PATCH 2/3] Refactor castor commands --- .castor/docker.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/.castor/docker.php b/.castor/docker.php index 6df0adb..f62d265 100644 --- a/.castor/docker.php +++ b/.castor/docker.php @@ -11,31 +11,36 @@ use Castor\Attribute\AsTask; use function Castor\run; -use function php\console; require_once __DIR__ . '/../vendor/autoload.php'; #[AsTask(description: 'Start docker containers')] function up(): void { - run( - command: 'docker-compose -f docker-compose.yml up', - timeout: 0 - ); + docker_compose(command: 'up', timeout: 0); } #[AsTask(description: 'Stop and clean docker containers')] function down(): void { - run( - command: 'docker-compose -f docker-compose.yml down -v' - ); + docker_compose('down -v'); } #[AsTask(description: 'Initialize rabbitmq configuration')] function init(): void { + docker_compose_exec('rabbitmq', 'rabbitmqctl import_definitions /scripts/definitions.json'); +} + +function docker_compose( + string $command, + ?int $timeout = null +): void { run( - command: 'docker-compose -f docker-compose.yml up -d rabbitmq' - ); - run( - command: 'docker-compose -f docker-compose.yml exec rabbitmq rabbitmqctl import_definitions /scripts/definitions.json' + command: 'docker-compose -f docker-compose.yml ' . $command, + timeout: $timeout ); } + +function docker_compose_exec(string $container, string $command, ?int $timeout = null): void +{ + docker_compose("up -d $container"); + docker_compose("exec $container $command", $timeout); +} From f179f59c5eee363bea13a5ea1f0b206e40070267 Mon Sep 17 00:00:00 2001 From: Julien Mercier-Rojas Date: Wed, 8 Nov 2023 12:11:51 +0100 Subject: [PATCH 3/3] Add arguments to define number of messages to load --- .castor/console.php | 11 +++++++++++ .castor/docker.php | 3 +++ src/Console/LoadMessages.php | 25 +++++++++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/.castor/console.php b/.castor/console.php index 2577c8f..53f5c8e 100644 --- a/.castor/console.php +++ b/.castor/console.php @@ -15,6 +15,7 @@ use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; +use function Castor\notify; use function Castor\run; #[AsTask(description: 'Execute console command')] @@ -26,3 +27,13 @@ function console( timeout: 0 ); } + +#[AsTask(description: 'Load messaged in RabbitMQ', name: 'load-messages')] +function loadMessages(int $nbMessages): void +{ + run( + command: 'docker-compose -f docker-compose.yml exec demo php -d max_execution_time=0 console.php demo:load-messages ' . $nbMessages, + timeout: 0 + ); + notify(sprintf('%d messages loaded', $nbMessages)); +} diff --git a/.castor/docker.php b/.castor/docker.php index f62d265..a3ffc7a 100644 --- a/.castor/docker.php +++ b/.castor/docker.php @@ -10,6 +10,8 @@ namespace docker; use Castor\Attribute\AsTask; + +use function Castor\notify; use function Castor\run; require_once __DIR__ . '/../vendor/autoload.php'; @@ -27,6 +29,7 @@ function down(): void { #[AsTask(description: 'Initialize rabbitmq configuration')] function init(): void { docker_compose_exec('rabbitmq', 'rabbitmqctl import_definitions /scripts/definitions.json'); + notify('RabbitMQ Initialized'); } function docker_compose( diff --git a/src/Console/LoadMessages.php b/src/Console/LoadMessages.php index 446f4cc..55ba26e 100644 --- a/src/Console/LoadMessages.php +++ b/src/Console/LoadMessages.php @@ -10,12 +10,14 @@ namespace JeckelLab\IpcSharedMemoryDemo\Console; use Exception; +use InvalidArgumentException; use JeckelLab\IpcSharedMemoryDemo\Message\Message; use PhpAmqpLib\Channel\AMQPChannel; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Exception\AMQPConnectionBlockedException; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -24,19 +26,25 @@ class LoadMessages extends Command { private ?AMQPStreamConnection $connection = null; + protected function configure(): void + { + $this->addArgument('nb-messages', InputArgument::REQUIRED, 'Number of messages to load'); + } + + /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @throws Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { - $channel = $this->getChannel(); + $nbMessages = $this->getNbMessagesToLoad($input); + $channel = $this->getChannel(); $exchange = (string) getenv('RABBITMQ_EXCHANGE'); $batch = 100; - $max = 1000 * 1000 * 10; - for ($i = 0; $i < $max; $i++) { + for ($i = 0; $i < $nbMessages; $i++) { $message = $this->getMessage(); $channel->batch_basic_publish($message, $exchange, $message->getRoutingKey()); @@ -54,10 +62,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $channel->publish_batch(); - $output->writeln(sprintf('Published %d messages', $max)); + $output->writeln(sprintf('Published %d messages', $nbMessages)); return Command::SUCCESS; } + public function getNbMessagesToLoad(InputInterface $input): int + { + $arg = $input->getArgument('nb-messages'); + if (is_numeric($arg)) { + return (int) $arg; + } + throw new InvalidArgumentException('Argument must be a number'); + } + /** * @return AMQPChannel * @throws Exception