Skip to content

Commit 5d8976b

Browse files
Merge pull request #632 from kafkiansky/thesis-amqp-tutorial
Added tutorial for thesis/amqp
2 parents 6696c1b + bf3a835 commit 5d8976b

20 files changed

+636
-0
lines changed

php-thesis/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

php-thesis/Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
up:
2+
docker compose up -d
3+
4+
down:
5+
docker compose down
6+
7+
php:
8+
docker compose exec php bash
9+
10+
run-send:
11+
docker compose exec php php hello-world/send.php
12+
13+
run-receive:
14+
docker compose exec php php hello-world/receive.php
15+
16+
run-new-task:
17+
docker compose exec php php work-queue/new_task.php "A very hard task which takes two seconds.."
18+
19+
run-worker:
20+
docker compose exec php php work-queue/worker.php
21+
22+
run-emit-log:
23+
docker compose exec php php pub-sub/emit_log.php
24+
25+
run-receive-logs:
26+
docker compose exec php php pub-sub/receive_logs.php
27+
28+
run-emit-log-direct:
29+
docker compose exec php php routing/emit_log_direct.php warning "Something went wrong"
30+
31+
run-receive-logs-direct:
32+
docker compose exec php php routing/receive_logs_direct.php info warning error
33+
34+
run-emit-log-topic:
35+
docker compose exec php php topics/emit_log_topic.php "kern.critical" "A critical kernel error"
36+
37+
run-receive-logs-topic:
38+
docker compose exec php php topics/receive_logs_topic.php "#"
39+
40+
run-rpc-server:
41+
docker compose exec php php rpc/rpc_server.php
42+
43+
run-rpc-client:
44+
docker compose exec php php rpc/rpc_client.php
45+
46+
run-confirms-message:
47+
docker compose exec php php publisher-confirms/message.php
48+
49+
run-confirms-batch:
50+
docker compose exec php php publisher-confirms/batch.php

php-thesis/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Non-blocking php code for RabbitMQ tutorials based on thesis/amqp
2+
3+
Run rabbitmq server and php container with `make up`.
4+
5+
## Code
6+
7+
[Tutorial one: "Hello World!"](https://www.rabbitmq.com/tutorials/tutorial-one-php):
8+
```shell
9+
make run-send
10+
make run-receive
11+
```
12+
13+
[Tutorial two: Work Queues](https://www.rabbitmq.com/tutorials/tutorial-two-php):
14+
```shell
15+
make run-new-task
16+
make run-worker
17+
```
18+
19+
[Tutorial three: Publish/Subscribe](https://www.rabbitmq.com/tutorials/tutorial-three-php)
20+
```shell
21+
make run-emit-log
22+
make run-receive-logs
23+
```
24+
25+
[Tutorial four: Routing](https://www.rabbitmq.com/tutorials/tutorial-four-php):
26+
```shell
27+
make run-emit-log-direct
28+
make run-receive-logs-direct
29+
```
30+
31+
[Tutorial five: Topics](https://www.rabbitmq.com/tutorials/tutorial-five-php):
32+
```shell
33+
make run-emit-log-topic
34+
make run-receive-logs-topic
35+
```
36+
37+
[Tutorial six: RPC](https://www.rabbitmq.com/tutorials/tutorial-six-php):
38+
```shell
39+
make run-rpc-server
40+
make run-rpc-client
41+
```
42+
43+
[Tutorial seven: Publisher Confirms](https://www.rabbitmq.com/tutorials/tutorial-seven-php):
44+
```shell
45+
make run-rpc-server
46+
make run-rpc-client
47+
```

php-thesis/composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"require": {
3+
"thesis/amqp": "^1.0"
4+
},
5+
"require-dev": {
6+
"symfony/var-dumper": "^7.3"
7+
}
8+
}

php-thesis/docker-compose.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
php:
3+
build:
4+
dockerfile: docker/php/Dockerfile
5+
volumes:
6+
- .:/app:cached
7+
command: sh -c 'trap "exit 0" TERM; tail -f /dev/null & wait'
8+
9+
rabbitmq:
10+
image: rabbitmq:4.0-management-alpine
11+
restart: always
12+
ports:
13+
- "127.0.0.1:5672:5672"
14+
- "127.0.0.1:15672:15672"

php-thesis/docker/php/Dockerfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM composer:2.8 AS composer
2+
FROM mlocati/php-extension-installer:2.7 AS php-extension-installer
3+
FROM php:8.3-cli-bookworm AS php-dev
4+
5+
COPY --from=composer /usr/bin/composer /usr/bin/
6+
COPY --from=php-extension-installer /usr/bin/install-php-extensions /usr/bin/
7+
8+
ARG UID=10001
9+
ARG GID=10001
10+
11+
RUN <<EOF
12+
set -e
13+
groupmod --gid=${GID} www-data
14+
usermod --uid=${UID} --gid=${GID} www-data
15+
apt-get update
16+
apt-get install --no-install-recommends --no-install-suggests -q -y unzip
17+
EOF
18+
19+
RUN <<EOF
20+
set -e
21+
install-php-extensions opcache pcntl sockets pgsql bcmath intl uv
22+
apt-get remove -q -y ${PHPIZE_DEPS} ${BUILD_DEPENDS}
23+
EOF
24+
25+
RUN <<EOF
26+
set -e
27+
mkdir /var/.composer
28+
chown www-data:www-data /var/.composer
29+
EOF
30+
31+
ENV COMPOSER_CACHE_DIR=/var/.composer
32+
33+
USER www-data
34+
35+
WORKDIR /app

php-thesis/hello-world/receive.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Thesis\Amqp\Client;
6+
use Thesis\Amqp\Config;
7+
use Thesis\Amqp\DeliveryMessage;
8+
use function Amp\trapSignal;
9+
10+
require_once __DIR__.'/../vendor/autoload.php';
11+
12+
$client = new Client(
13+
new Config(
14+
urls: ['rabbitmq:5672'],
15+
),
16+
);
17+
18+
$channel = $client->channel();
19+
$channel->queueDeclare('hello');
20+
21+
echo " [*] Waiting for messages. To exit press CTRL+C\n";
22+
23+
$channel->consume(
24+
static function (DeliveryMessage $delivery): void {
25+
echo " [x] Received {$delivery->message->body} \n";
26+
},
27+
queue: 'hello',
28+
noAck: true,
29+
);
30+
31+
trapSignal([\SIGTERM, \SIGINT]);
32+
33+
$client->disconnect();

php-thesis/hello-world/send.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Thesis\Amqp\Client;
6+
use Thesis\Amqp\Config;
7+
use Thesis\Amqp\Message;
8+
9+
require_once __DIR__.'/../vendor/autoload.php';
10+
11+
$client = new Client(
12+
new Config(
13+
urls: ['rabbitmq:5672'],
14+
),
15+
);
16+
17+
$channel = $client->channel();
18+
19+
$channel->publish(new Message('Hello World!'), routingKey: 'hello');
20+
21+
$client->disconnect();

php-thesis/pub-sub/emit_log.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Thesis\Amqp\Client;
6+
use Thesis\Amqp\Config;
7+
use Thesis\Amqp\Message;
8+
9+
require_once __DIR__.'/../vendor/autoload.php';
10+
11+
$client = new Client(
12+
new Config(
13+
urls: ['rabbitmq:5672'],
14+
),
15+
);
16+
17+
$channel = $client->channel();
18+
$channel->exchangeDeclare(exchange: 'logs', exchangeType: 'fanout');
19+
20+
$channel->publish(
21+
new Message(
22+
body: $data = implode(' ', array_slice($argv, 1)) ?: 'info: Hello World!',
23+
),
24+
exchange: 'logs',
25+
);
26+
27+
$client->disconnect();

php-thesis/pub-sub/receive_logs.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Thesis\Amqp\Client;
6+
use Thesis\Amqp\Config;
7+
use Thesis\Amqp\DeliveryMessage;
8+
use function Amp\trapSignal;
9+
10+
require_once __DIR__.'/../vendor/autoload.php';
11+
12+
$client = new Client(
13+
new Config(
14+
urls: ['rabbitmq:5672'],
15+
),
16+
);
17+
18+
$channel = $client->channel();
19+
20+
$channel->exchangeDeclare(exchange: 'logs', exchangeType: 'fanout');
21+
$queue = $channel->queueDeclare(exclusive: true);
22+
$channel->queueBind(queue: $queue->name, exchange: 'logs');
23+
24+
echo " [*] Waiting for logs. To exit press CTRL+C\n";
25+
26+
$channel->consume(
27+
callback: static function (DeliveryMessage $delivery): void {
28+
echo " [x] {$delivery->message->body} \n";
29+
},
30+
queue: $queue->name,
31+
noAck: true,
32+
);
33+
34+
trapSignal([\SIGTERM, \SIGINT]);
35+
36+
$client->disconnect();

0 commit comments

Comments
 (0)