Skip to content

Commit

Permalink
Fixes #260: Added STOMP driver
Browse files Browse the repository at this point in the history
  • Loading branch information
versh23 authored and samdark committed Jun 4, 2019
1 parent 6c36788 commit 27f765f
Show file tree
Hide file tree
Showing 18 changed files with 537 additions and 8 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ php:
matrix:
include:
- php: 5.5
env: EXCLUDE_AMQP_INTEROP=true
env:
- EXCLUDE_AMQP_INTEROP=true
- EXCLUDE_STOMP=true
fast_finish: true


Expand All @@ -19,6 +21,7 @@ services:
- postgresql
- redis-server
- rabbitmq
- docker

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

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

before_script:
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Yii2 Queue Extension Change Log
===============================

2.2.2 under development
2.3.0 under development
-----------------------

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


2.2.1 May 21, 2019
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

An extension for running tasks asynchronously via queues.

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

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

Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"yiisoft/yii2-debug": "*",
"yiisoft/yii2-gii": "*",
"phpunit/phpunit": "~4.4",
"aws/aws-sdk-php": ">=2.4"
"aws/aws-sdk-php": ">=2.4",
"enqueue/stomp": "^0.8.39"
},
"suggest": {
"ext-pcntl": "Need for process signals.",
Expand All @@ -38,7 +39,8 @@
"php-amqplib/php-amqplib": "Need for AMQP queue.",
"enqueue/amqp-lib": "Need for AMQP interop queue.",
"ext-gearman": "Need for Gearman queue.",
"aws/aws-sdk-php": "Need for aws SQS."
"aws/aws-sdk-php": "Need for aws SQS.",
"enqueue/stomp": "Need for Stomp queue."
},
"autoload": {
"psr-4": {
Expand All @@ -51,7 +53,8 @@
"yii\\queue\\gearman\\": "src/drivers/gearman",
"yii\\queue\\redis\\": "src/drivers/redis",
"yii\\queue\\sync\\": "src/drivers/sync",
"yii\\queue\\sqs\\": "src/drivers/sqs"
"yii\\queue\\sqs\\": "src/drivers/sqs",
"yii\\queue\\stomp\\": "src/drivers/stomp"
}
},
"autoload-dev": {
Expand Down
1 change: 1 addition & 0 deletions docs/guide-ru/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* [Beanstalk драйвер](driver-beanstalk.md)
* [Gearman драйвер](driver-gearman.md)
* [AWS SQS драйвер](driver-sqs.md)
* [Stomp драйвер](driver-stomp.md)

Инструменты разработчика
------------------------
Expand Down
43 changes: 43 additions & 0 deletions docs/guide-ru/driver-stomp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Stomp драйвер
================

Драйвер работает с очередью на базе ActiveMQ.

В приложении должно быть установлено расширение `enqueue/stomp`.

Пример настройки:

```php
return [
'bootstrap' => [
'queue', // Компонент регистрирует свои консольные команды
],
'components' => [
'queue' => [
'class' => \yii\queue\stomp\Queue::class,
'host' => 'localhost',
'port' => 61613,
'queueName' => 'queue',
],
],
];
```

Консоль
-------

Для обработки очереди используются консольные команды.

```sh
yii queue/listen
```

Команда `listen` запускает обработку очереди в режиме демона. Очередь опрашивается непрерывно.
Если добавляются новые задания, то они сразу же извлекаются и выполняются. Способ наиболее эфективен
если запускать команду через [supervisor](worker.md#supervisor) или [systemd](worker.md#systemd).

Для команды `listen` доступны следующие опции:

- `--verbose`, `-v`: состояние обработки заданий выводится в консоль.
- `--isolate`: каждое задание выполняется в отдельном дочернем процессе.
- `--color`: подсветка вывода в режиме `--verbose`.
1 change: 1 addition & 0 deletions docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Queue Drivers
* [Beanstalk](driver-beanstalk.md)
* [Gearman](driver-gearman.md)
* [AWS SQS](driver-sqs.md)
* [Stomp](driver-stomp.md)

Developer tools
---------------
Expand Down
39 changes: 39 additions & 0 deletions docs/guide/driver-stomp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Stomp Driver
===============


This driver works with ActiveMQ queues.

It requires the `enqueue/stomp` package.

Configuration example:

```php
return [
'bootstrap' => [
'queue', // The component registers its own console commands
],
'components' => [
'queue' => [
'class' => \yii\queue\stomp\Queue::class,
'host' => 'localhost',
'port' => 61613,
'queueName' => 'queue',
],
],
];
```

Console
-------

A console command is used to execute queued jobs.

```sh
yii queue/listen [timeout]
```

The `listen` command launches a daemon which infinitely queries the queue. If there are new tasks
they're immediately obtained and executed. The `timeout` parameter specifies the number of seconds to sleep between
querying the queue. This method is most efficient when the command is properly daemonized via
[supervisor](worker.md#supervisor) or [systemd](worker.md#systemd).
66 changes: 66 additions & 0 deletions src/drivers/stomp/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\queue\stomp;

use yii\console\Exception;
use yii\queue\cli\Command as CliCommand;

/**
* Manages application stomp-queue.
*
* @author Sergey Vershinin <[email protected]>
* @since 2.3.0
*/
class Command extends CliCommand
{
/**
* @var Queue
*/
public $queue;


/**
* @inheritdoc
*/
protected function isWorkerAction($actionID)
{
return in_array($actionID, ['run', 'listen']);
}


/**
* Runs all jobs from stomp-queue.
* It can be used as cron job.
*
* @return null|int exit code.
*/
public function actionRun()
{
return $this->queue->run(false);
}

/**
* Listens stomp-queue and runs new jobs.
* It can be used as daemon process.
*
* @param int $timeout number of seconds to wait a job.
* @throws Exception when params are invalid.
* @return null|int exit code.
*/
public function actionListen($timeout = 3)
{
if (!is_numeric($timeout)) {
throw new Exception('Timeout must be numeric.');
}
if ($timeout < 1) {
throw new Exception('Timeout must be greater that zero.');
}

return $this->queue->run(true, $timeout);
}
}
Loading

0 comments on commit 27f765f

Please sign in to comment.