Skip to content

Commit 27f765f

Browse files
versh23samdark
authored andcommitted
Fixes #260: Added STOMP driver
1 parent 6c36788 commit 27f765f

File tree

18 files changed

+537
-8
lines changed

18 files changed

+537
-8
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ php:
1010
matrix:
1111
include:
1212
- php: 5.5
13-
env: EXCLUDE_AMQP_INTEROP=true
13+
env:
14+
- EXCLUDE_AMQP_INTEROP=true
15+
- EXCLUDE_STOMP=true
1416
fast_finish: true
1517

1618

@@ -19,6 +21,7 @@ services:
1921
- postgresql
2022
- redis-server
2123
- rabbitmq
24+
- docker
2225

2326
# cache vendor dirs
2427
cache:
@@ -30,12 +33,15 @@ before_install:
3033
- sudo apt-get install -qq beanstalkd
3134
- sudo beanstalkd -v
3235
- sudo service beanstalkd start
36+
- docker pull webcenter/activemq
37+
- docker run -d -p 61613:61613 webcenter/activemq
3338
- if [[ ${TRAVIS_PHP_VERSION:0:1} == "5" ]]; then pecl install igbinary-2.0.8; else pecl install igbinary; fi
3439

3540
install:
3641
- travis_retry composer self-update && composer --version
3742
- export PATH="$HOME/.composer/vendor/bin:$PATH"
3843
- if [ "$EXCLUDE_AMQP_INTEROP" = true ]; then travis_retry composer remove "enqueue/amqp-lib" "enqueue/amqp-tools" --dev --no-interaction --no-update; fi
44+
- if [ "$EXCLUDE_STOMP" = true ]; then travis_retry composer remove "enqueue/stomp" --dev --no-interaction --no-update; fi
3945
- travis_retry composer install --prefer-dist --no-interaction
4046

4147
before_script:

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Yii2 Queue Extension Change Log
22
===============================
33

4-
2.2.2 under development
4+
2.3.0 under development
55
-----------------------
66

7-
- no changes in this release.
7+
- Enh #260: Added STOMP driver (versh23)
88

99

1010
2.2.1 May 21, 2019

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
An extension for running tasks asynchronously via queues.
1010

11-
It supports queues based on **DB**, **Redis**, **RabbitMQ**, **AMQP**, **Beanstalk** and **Gearman**.
11+
It supports queues based on **DB**, **Redis**, **RabbitMQ**, **AMQP**, **Beanstalk**, **ActiveMQ** and **Gearman**.
1212

1313
Documentation is at [docs/guide/README.md](docs/guide/README.md).
1414

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"yiisoft/yii2-debug": "*",
3030
"yiisoft/yii2-gii": "*",
3131
"phpunit/phpunit": "~4.4",
32-
"aws/aws-sdk-php": ">=2.4"
32+
"aws/aws-sdk-php": ">=2.4",
33+
"enqueue/stomp": "^0.8.39"
3334
},
3435
"suggest": {
3536
"ext-pcntl": "Need for process signals.",
@@ -38,7 +39,8 @@
3839
"php-amqplib/php-amqplib": "Need for AMQP queue.",
3940
"enqueue/amqp-lib": "Need for AMQP interop queue.",
4041
"ext-gearman": "Need for Gearman queue.",
41-
"aws/aws-sdk-php": "Need for aws SQS."
42+
"aws/aws-sdk-php": "Need for aws SQS.",
43+
"enqueue/stomp": "Need for Stomp queue."
4244
},
4345
"autoload": {
4446
"psr-4": {
@@ -51,7 +53,8 @@
5153
"yii\\queue\\gearman\\": "src/drivers/gearman",
5254
"yii\\queue\\redis\\": "src/drivers/redis",
5355
"yii\\queue\\sync\\": "src/drivers/sync",
54-
"yii\\queue\\sqs\\": "src/drivers/sqs"
56+
"yii\\queue\\sqs\\": "src/drivers/sqs",
57+
"yii\\queue\\stomp\\": "src/drivers/stomp"
5558
}
5659
},
5760
"autoload-dev": {

docs/guide-ru/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [Beanstalk драйвер](driver-beanstalk.md)
2323
* [Gearman драйвер](driver-gearman.md)
2424
* [AWS SQS драйвер](driver-sqs.md)
25+
* [Stomp драйвер](driver-stomp.md)
2526

2627
Инструменты разработчика
2728
------------------------

docs/guide-ru/driver-stomp.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Stomp драйвер
2+
================
3+
4+
Драйвер работает с очередью на базе ActiveMQ.
5+
6+
В приложении должно быть установлено расширение `enqueue/stomp`.
7+
8+
Пример настройки:
9+
10+
```php
11+
return [
12+
'bootstrap' => [
13+
'queue', // Компонент регистрирует свои консольные команды
14+
],
15+
'components' => [
16+
'queue' => [
17+
'class' => \yii\queue\stomp\Queue::class,
18+
'host' => 'localhost',
19+
'port' => 61613,
20+
'queueName' => 'queue',
21+
],
22+
],
23+
];
24+
```
25+
26+
Консоль
27+
-------
28+
29+
Для обработки очереди используются консольные команды.
30+
31+
```sh
32+
yii queue/listen
33+
```
34+
35+
Команда `listen` запускает обработку очереди в режиме демона. Очередь опрашивается непрерывно.
36+
Если добавляются новые задания, то они сразу же извлекаются и выполняются. Способ наиболее эфективен
37+
если запускать команду через [supervisor](worker.md#supervisor) или [systemd](worker.md#systemd).
38+
39+
Для команды `listen` доступны следующие опции:
40+
41+
- `--verbose`, `-v`: состояние обработки заданий выводится в консоль.
42+
- `--isolate`: каждое задание выполняется в отдельном дочернем процессе.
43+
- `--color`: подсветка вывода в режиме `--verbose`.

docs/guide/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Queue Drivers
2222
* [Beanstalk](driver-beanstalk.md)
2323
* [Gearman](driver-gearman.md)
2424
* [AWS SQS](driver-sqs.md)
25+
* [Stomp](driver-stomp.md)
2526

2627
Developer tools
2728
---------------

docs/guide/driver-stomp.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Stomp Driver
2+
===============
3+
4+
5+
This driver works with ActiveMQ queues.
6+
7+
It requires the `enqueue/stomp` package.
8+
9+
Configuration example:
10+
11+
```php
12+
return [
13+
'bootstrap' => [
14+
'queue', // The component registers its own console commands
15+
],
16+
'components' => [
17+
'queue' => [
18+
'class' => \yii\queue\stomp\Queue::class,
19+
'host' => 'localhost',
20+
'port' => 61613,
21+
'queueName' => 'queue',
22+
],
23+
],
24+
];
25+
```
26+
27+
Console
28+
-------
29+
30+
A console command is used to execute queued jobs.
31+
32+
```sh
33+
yii queue/listen [timeout]
34+
```
35+
36+
The `listen` command launches a daemon which infinitely queries the queue. If there are new tasks
37+
they're immediately obtained and executed. The `timeout` parameter specifies the number of seconds to sleep between
38+
querying the queue. This method is most efficient when the command is properly daemonized via
39+
[supervisor](worker.md#supervisor) or [systemd](worker.md#systemd).

src/drivers/stomp/Command.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* @link http://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license http://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yii\queue\stomp;
9+
10+
use yii\console\Exception;
11+
use yii\queue\cli\Command as CliCommand;
12+
13+
/**
14+
* Manages application stomp-queue.
15+
*
16+
* @author Sergey Vershinin <[email protected]>
17+
* @since 2.3.0
18+
*/
19+
class Command extends CliCommand
20+
{
21+
/**
22+
* @var Queue
23+
*/
24+
public $queue;
25+
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function isWorkerAction($actionID)
31+
{
32+
return in_array($actionID, ['run', 'listen']);
33+
}
34+
35+
36+
/**
37+
* Runs all jobs from stomp-queue.
38+
* It can be used as cron job.
39+
*
40+
* @return null|int exit code.
41+
*/
42+
public function actionRun()
43+
{
44+
return $this->queue->run(false);
45+
}
46+
47+
/**
48+
* Listens stomp-queue and runs new jobs.
49+
* It can be used as daemon process.
50+
*
51+
* @param int $timeout number of seconds to wait a job.
52+
* @throws Exception when params are invalid.
53+
* @return null|int exit code.
54+
*/
55+
public function actionListen($timeout = 3)
56+
{
57+
if (!is_numeric($timeout)) {
58+
throw new Exception('Timeout must be numeric.');
59+
}
60+
if ($timeout < 1) {
61+
throw new Exception('Timeout must be greater that zero.');
62+
}
63+
64+
return $this->queue->run(true, $timeout);
65+
}
66+
}

0 commit comments

Comments
 (0)