Skip to content

Commit

Permalink
Merge pull request #6 from Jeckel-Lab/feature/replace_makefile_with_c…
Browse files Browse the repository at this point in the history
…astor

Replace current Makefile recipes with Castor tasks
  • Loading branch information
jeckel authored Nov 8, 2023
2 parents 0a45d1c + f179f59 commit 0d73e2d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 47 deletions.
39 changes: 39 additions & 0 deletions .castor/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* 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));
}
49 changes: 49 additions & 0 deletions .castor/docker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* 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);
}
14 changes: 0 additions & 14 deletions Makefile

This file was deleted.

27 changes: 0 additions & 27 deletions castor-commands/console.php

This file was deleted.

2 changes: 1 addition & 1 deletion castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

use function Castor\import;

import(__DIR__ . '/castor-commands');
import(__DIR__ . '/.castor');
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parameters:
- src/
excludePaths:
- castor.php
- castor-commands/*
- .castor/*

includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
25 changes: 21 additions & 4 deletions src/Console/LoadMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());

Expand All @@ -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
Expand Down

0 comments on commit 0d73e2d

Please sign in to comment.