Skip to content

Commit 56e5c97

Browse files
authored
Merge pull request #430 from turboboy88/mongodb_transport
Mongodb transport
2 parents bf9f57a + dab69f9 commit 56e5c97

File tree

57 files changed

+3301
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3301
-11
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ cache:
4444
directories:
4545
- $HOME/.composer/cache
4646

47+
before_install:
48+
- echo "extension = mongodb.so" >> $HOME/.phpenv/versions/$(phpenv version-name)/etc/php.ini
49+
- php -m
50+
- php -i | grep -C 15 mongo
51+
4752
install:
4853
- rm $HOME/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini;
4954
- echo "memory_limit=2048M" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Features:
6161
[![Build Status](https://travis-ci.org/php-enqueue/fs.png?branch=master)](https://travis-ci.org/php-enqueue/fs)
6262
[![Total Downloads](https://poser.pugx.org/enqueue/fs/d/total.png)](https://packagist.org/packages/enqueue/fs)
6363
[![Latest Stable Version](https://poser.pugx.org/enqueue/fs/version.png)](https://packagist.org/packages/enqueue/fs)
64+
* [Mongodb](docs/transport/mongodb.md)
65+
[![Build Status](https://travis-ci.org/php-enqueue/mongodb.png?branch=master)](https://travis-ci.org/php-enqueue/mongodb)
66+
[![Total Downloads](https://poser.pugx.org/enqueue/mongodb/d/total.png)](https://packagist.org/packages/enqueue/mongodb)
67+
[![Latest Stable Version](https://poser.pugx.org/enqueue/mongodb/version.png)](https://packagist.org/packages/enqueue/mongodb)
6468
* [Null](docs/transport/null.md).
6569
[![Build Status](https://travis-ci.org/php-enqueue/null.png?branch=master)](https://travis-ci.org/php-enqueue/null)
6670
[![Total Downloads](https://poser.pugx.org/enqueue/null/d/total.png)](https://packagist.org/packages/enqueue/null)

bin/run-fun-test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
set -x
44
set -e
55

6-
COMPOSE_PROJECT_NAME=mqdev docker-compose run --workdir="/mqdev" --rm dev ./bin/test "$@"
6+
docker-compose run --workdir="/mqdev" --rm dev ./bin/test "$@"

bin/test

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ waitForService redis 6379 50
3636
waitForService beanstalkd 11300 50
3737
waitForService gearmand 4730 50
3838
waitForService kafka 9092 50
39+
waitForService mongo 27017 50
3940

4041
php pkg/job-queue/Tests/Functional/app/console doctrine:database:create --if-not-exists
4142
php pkg/job-queue/Tests/Functional/app/console doctrine:schema:update --force

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"enqueue/fs": "*@dev",
1717
"enqueue/null": "*@dev",
1818
"enqueue/dbal": "*@dev",
19+
"enqueue/mongodb": "*@dev",
1920
"enqueue/sqs": "*@dev",
2021
"enqueue/pheanstalk": "*@dev",
2122
"enqueue/gearman": "*@dev",
@@ -60,7 +61,8 @@
6061
"platform": {
6162
"ext-amqp": "1.9.3",
6263
"ext-gearman": "1.1",
63-
"ext-rdkafka": "3.3"
64+
"ext-rdkafka": "3.3",
65+
"ext-mongodb": "1.3"
6466
}
6567
},
6668
"repositories": [
@@ -143,6 +145,10 @@
143145
{
144146
"type": "path",
145147
"url": "pkg/async-event-dispatcher"
148+
},
149+
{
150+
"type": "path",
151+
"url": "pkg/mongodb"
146152
}
147153
]
148154
}

docker-compose.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ services:
1313
- zookeeper
1414
- google-pubsub
1515
- rabbitmqssl
16+
- mongo
1617
volumes:
1718
- './:/mqdev'
1819
environment:
@@ -24,7 +25,7 @@ services:
2425
- RABBITMQ_PASSWORD=guest
2526
- RABBITMQ_VHOST=mqdev
2627
- RABBITMQ_AMQP__PORT=5672
27-
- RABBITMQ_STOMP_PORT=61613
28+
- RABBITMQ_STOMP_PORT=61613
2829
- DOCTRINE_DRIVER=pdo_mysql
2930
- DOCTRINE_HOST=mysql
3031
- DOCTRINE_PORT=3306
@@ -44,6 +45,7 @@ services:
4445
- RDKAFKA_PORT=9092
4546
- PUBSUB_EMULATOR_HOST=http://google-pubsub:8085
4647
- GCLOUD_PROJECT=mqdev
48+
- MONGO_DSN=mongodb://mongo
4749

4850
rabbitmq:
4951
image: 'enqueue/rabbitmq:latest'
@@ -102,6 +104,11 @@ services:
102104
image: 'google/cloud-sdk:latest'
103105
entrypoint: 'gcloud beta emulators pubsub start --host-port=0.0.0.0:8085'
104106

107+
mongo:
108+
image: mongo:3.7
109+
ports:
110+
- "27017:27017"
111+
105112
volumes:
106113
mysql-data:
107114
driver: local

docker/Dockerfile

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ RUN set -x && \
1313
git clone https://github.com/pdezwart/php-amqp.git . && git checkout v1.9.3 && \
1414
phpize --clean && phpize && ./configure && make install
1515

16-
## confis
17-
18-
# RUN rm -f /etc/php/7.0/cli/conf.d/*xdebug.ini
19-
2016
## librdkafka
2117
RUN set -x && \
2218
apt-get update && \
@@ -27,10 +23,10 @@ RUN set -x && \
2723
git checkout v0.11.1 && \
2824
./configure && make && make install && \
2925
pecl install rdkafka && \
30-
echo "extension=rdkafka.so" > /etc/php/7.1/cli/conf.d/10-rdkafka.ini && \
31-
echo "extension=rdkafka.so" > /etc/php/7.1/fpm/conf.d/10-rdkafka.ini
26+
echo "extension=rdkafka.so" > /etc/php/7.2/cli/conf.d/10-rdkafka.ini && \
27+
echo "extension=rdkafka.so" > /etc/php/7.2/fpm/conf.d/10-rdkafka.ini
3228

33-
COPY ./php/cli.ini /etc/php/7.1/cli/conf.d/1-dev_cli.ini
29+
COPY ./php/cli.ini /etc/php/7.2/cli/conf.d/1-dev_cli.ini
3430
COPY ./bin/dev_entrypoiny.sh /usr/local/bin/entrypoint.sh
3531
RUN chmod u+x /usr/local/bin/entrypoint.sh
3632

docs/transport/mongodb.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Mongodb transport
2+
3+
Allows to use [MongoDB](https://www.mongodb.com/) as a message queue broker.
4+
5+
* [Installation](#installation)
6+
* [Create context](#create-context)
7+
* [Send message to topic](#send-message-to-topic)
8+
* [Send message to queue](#send-message-to-queue)
9+
* [Send priority message](#send-priority-message)
10+
* [Send expiration message](#send-expiration-message)
11+
* [Send delayed message](#send-delayed-message)
12+
* [Consume message](#consume-message)
13+
14+
## Installation
15+
16+
```bash
17+
$ composer require enqueue/mongodb
18+
```
19+
20+
## Create context
21+
22+
```php
23+
<?php
24+
use Enqueue\Mongodb\MongodbConnectionFactory;
25+
26+
// connects to localhost
27+
$connectionFactory = new MongodbConnectionFactory();
28+
29+
// same as above
30+
$factory = new MongodbConnectionFactory('mongodb:');
31+
32+
// same as above
33+
$factory = new MongodbConnectionFactory([]);
34+
35+
$factory = new MongodbConnectionFactory([
36+
'uri' => 'mongodb://localhost:27017/db_name',
37+
'dbname' => 'enqueue',
38+
'collection_name' => 'enqueue',
39+
'polling_interval' => '1000',
40+
]);
41+
42+
$psrContext = $factory->createContext();
43+
44+
// if you have enqueue/enqueue library installed you can use a function from there to create the context
45+
$psrContext = \Enqueue\dsn_to_context('mongodb:');
46+
```
47+
48+
## Send message to topic
49+
50+
```php
51+
<?php
52+
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
53+
/** @var \Enqueue\Mongodb\MongodbDestination $fooTopic */
54+
55+
$message = $psrContext->createMessage('Hello world!');
56+
57+
$psrContext->createProducer()->send($fooTopic, $message);
58+
```
59+
60+
## Send message to queue
61+
62+
```php
63+
<?php
64+
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
65+
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
66+
67+
$message = $psrContext->createMessage('Hello world!');
68+
69+
$psrContext->createProducer()->send($fooQueue, $message);
70+
```
71+
72+
## Send priority message
73+
74+
```php
75+
<?php
76+
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
77+
78+
$fooQueue = $psrContext->createQueue('foo');
79+
80+
$message = $psrContext->createMessage('Hello world!');
81+
82+
$psrContext->createProducer()
83+
->setPriority(5) // the higher priority the sooner a message gets to a consumer
84+
//
85+
->send($fooQueue, $message)
86+
;
87+
```
88+
89+
## Send expiration message
90+
91+
```php
92+
<?php
93+
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
94+
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
95+
96+
$message = $psrContext->createMessage('Hello world!');
97+
98+
$psrContext->createProducer()
99+
->setTimeToLive(60000) // 60 sec
100+
//
101+
->send($fooQueue, $message)
102+
;
103+
```
104+
105+
## Send delayed message
106+
107+
```php
108+
<?php
109+
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
110+
111+
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
112+
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
113+
114+
// make sure you run "composer require enqueue/amqp-tools".
115+
116+
$message = $psrContext->createMessage('Hello world!');
117+
118+
$psrContext->createProducer()
119+
->setDeliveryDelay(5000) // 5 sec
120+
121+
->send($fooQueue, $message)
122+
;
123+
````
124+
125+
## Consume message:
126+
127+
```php
128+
<?php
129+
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
130+
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
131+
132+
$consumer = $psrContext->createConsumer($fooQueue);
133+
134+
$message = $consumer->receive();
135+
136+
// process a message
137+
138+
$consumer->acknowledge($message);
139+
// $consumer->reject($message);
140+
```
141+
142+
[back to index](../index.md)

phpunit.xml.dist

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
<directory>pkg/gps/Tests</directory>
7878
</testsuite>
7979

80+
<testsuite name="Mongodb transport">
81+
<directory>pkg/mongodb/Tests</directory>
82+
</testsuite>
83+
8084
<testsuite name="enqueue-bundle">
8185
<directory>pkg/enqueue-bundle/Tests</directory>
8286
</testsuite>

pkg/enqueue-bundle/EnqueueBundle.php

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Enqueue\Fs\Symfony\FsTransportFactory;
2222
use Enqueue\Gps\GpsConnectionFactory;
2323
use Enqueue\Gps\Symfony\GpsTransportFactory;
24+
use Enqueue\Mongodb\Symfony\MongodbTransportFactory;
2425
use Enqueue\RdKafka\RdKafkaConnectionFactory;
2526
use Enqueue\RdKafka\Symfony\RdKafkaTransportFactory;
2627
use Enqueue\Redis\RedisConnectionFactory;
@@ -112,6 +113,12 @@ class_exists(AmqpLibConnectionFactory::class)
112113
$extension->setTransportFactory(new MissingTransportFactory('rdkafka', ['enqueue/rdkafka']));
113114
}
114115

116+
if (class_exists(MongodbTransportFactory::class)) {
117+
$extension->setTransportFactory(new MongodbTransportFactory('mongodb'));
118+
} else {
119+
$extension->setTransportFactory(new MissingTransportFactory('mongodb', ['enqueue/mongodb']));
120+
}
121+
115122
$container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
116123
$container->addCompilerPass(new AsyncTransformersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
117124
}

pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ public function provideEnqueueConfigs()
206206
]];
207207
}
208208

209+
yield 'mongodb_dsn' => [[
210+
'transport' => [
211+
'default' => 'mongodb',
212+
'mongodb' => getenv('MONGO_DSN'),
213+
],
214+
]];
215+
209216
// yield 'gps' => [[
210217
// 'transport' => [
211218
// 'default' => 'gps',

pkg/enqueue/Symfony/DefaultTransportFactory.php

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Enqueue\Fs\Symfony\FsTransportFactory;
99
use Enqueue\Gps\GpsConnectionFactory;
1010
use Enqueue\Gps\Symfony\GpsTransportFactory;
11+
use Enqueue\Mongodb\MongodbConnectionFactory;
12+
use Enqueue\Mongodb\Symfony\MongodbTransportFactory;
1113
use Enqueue\Null\NullConnectionFactory;
1214
use Enqueue\Null\Symfony\NullTransportFactory;
1315
use Enqueue\RdKafka\RdKafkaConnectionFactory;
@@ -215,6 +217,10 @@ private function findFactory($dsn)
215217
return new RdKafkaTransportFactory('default_kafka');
216218
}
217219

220+
if ($factory instanceof MongodbConnectionFactory) {
221+
return new MongodbTransportFactory('default_mongodb');
222+
}
223+
218224
throw new \LogicException(sprintf(
219225
'There is no supported transport factory for the connection factory "%s" created from DSN "%s"',
220226
get_class($factory),

pkg/enqueue/Tests/Functions/DsnToConnectionFactoryFunctionTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Enqueue\Fs\FsConnectionFactory;
1010
use Enqueue\Gearman\GearmanConnectionFactory;
1111
use Enqueue\Gps\GpsConnectionFactory;
12+
use Enqueue\Mongodb\MongodbConnectionFactory;
1213
use Enqueue\Null\NullConnectionFactory;
1314
use Enqueue\Pheanstalk\PheanstalkConnectionFactory;
1415
use Enqueue\RdKafka\RdKafkaConnectionFactory;
@@ -97,5 +98,7 @@ public static function provideDSNs()
9798
yield ['sqs:', SqsConnectionFactory::class];
9899

99100
yield ['gps:', GpsConnectionFactory::class];
101+
102+
yield ['mongodb:', MongodbConnectionFactory::class];
100103
}
101104
}

pkg/enqueue/Tests/Symfony/DefaultTransportFactoryTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,7 @@ public static function provideDSNs()
291291
yield ['stomp:', 'default_stomp'];
292292

293293
yield ['kafka:', 'default_kafka'];
294+
295+
yield ['mongodb:', 'default_mongodb'];
294296
}
295297
}

pkg/enqueue/functions.php

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Enqueue\Fs\FsConnectionFactory;
1111
use Enqueue\Gearman\GearmanConnectionFactory;
1212
use Enqueue\Gps\GpsConnectionFactory;
13+
use Enqueue\Mongodb\MongodbConnectionFactory;
1314
use Enqueue\Null\NullConnectionFactory;
1415
use Enqueue\Pheanstalk\PheanstalkConnectionFactory;
1516
use Enqueue\RdKafka\RdKafkaConnectionFactory;
@@ -108,6 +109,10 @@ function dsn_to_connection_factory($dsn)
108109
$map['gps'] = GpsConnectionFactory::class;
109110
}
110111

112+
if (class_exists(MongodbConnectionFactory::class)) {
113+
$map['mongodb'] = MongodbConnectionFactory::class;
114+
}
115+
111116
list($scheme) = explode(':', $dsn, 2);
112117
if (false == $scheme || false === strpos($dsn, ':')) {
113118
throw new \LogicException(sprintf('The scheme could not be parsed from DSN "%s"', $dsn));

pkg/mongodb/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*~
2+
/composer.lock
3+
/composer.phar
4+
/phpunit.xml
5+
/vendor/
6+
/.idea/
7+
/examples/

0 commit comments

Comments
 (0)