diff --git a/.castor/console.php b/.castor/console.php new file mode 100644 index 0000000..53f5c8e --- /dev/null +++ b/.castor/console.php @@ -0,0 +1,39 @@ + + * Created at: 30/10/2023 + */ + +declare(strict_types=1); + +namespace php; + +require_once __DIR__ . '/../vendor/autoload.php'; + +use Castor\Attribute\AsTask; +use PhpAmqpLib\Connection\AMQPStreamConnection; +use PhpAmqpLib\Message\AMQPMessage; + +use function Castor\notify; +use function Castor\run; + +#[AsTask(description: 'Execute console command')] +function console( + string $consoleCommand +): void { + run( + command: 'docker-compose -f docker-compose.yml exec demo php -d max_execution_time=0 console.php ' . $consoleCommand, + 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 new file mode 100644 index 0000000..a3ffc7a --- /dev/null +++ b/.castor/docker.php @@ -0,0 +1,49 @@ + + * Created at: 08/11/2023 + */ + +declare(strict_types=1); + +namespace docker; + +use Castor\Attribute\AsTask; + +use function Castor\notify; +use function Castor\run; + +require_once __DIR__ . '/../vendor/autoload.php'; + +#[AsTask(description: 'Start docker containers')] +function up(): void { + docker_compose(command: 'up', timeout: 0); +} + +#[AsTask(description: 'Stop and clean docker containers')] +function down(): void { + docker_compose('down -v'); +} + +#[AsTask(description: 'Initialize rabbitmq configuration')] +function init(): void { + docker_compose_exec('rabbitmq', 'rabbitmqctl import_definitions /scripts/definitions.json'); + notify('RabbitMQ Initialized'); +} + +function docker_compose( + string $command, + ?int $timeout = null +): void { + run( + 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); +} 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-commands/console.php b/castor-commands/console.php deleted file mode 100644 index 3152bd6..0000000 --- a/castor-commands/console.php +++ /dev/null @@ -1,27 +0,0 @@ - - * Created at: 30/10/2023 - */ - -declare(strict_types=1); - -namespace php; - -require_once __DIR__ . '/../vendor/autoload.php'; - -use Castor\Attribute\AsTask; -use PhpAmqpLib\Connection\AMQPStreamConnection; -use PhpAmqpLib\Message\AMQPMessage; - -use function Castor\run; - -#[AsTask(description: 'Execute console command')] -function console( - string $consoleCommand -): void { - run( - 'docker-compose -f docker-compose.yml exec demo php -d max_execution_time=120 console.php ' . $consoleCommand - ); -} 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..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; - 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