Download the PHP package misterion/ko-worker without Composer
On this page you can find all versions of the php package misterion/ko-worker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package ko-worker
ko-worker
About
Ko-worker project is a PHP library that aims to abstract queue and RPC messaging patterns that can be implemented over RabbitMQ. The other goal is simplify message dispatching and RPC server development over RabbitMQ.
Each time you starting to use something like queues you should decide how to process your messages. Yes, it's sounds not complicated:
- write some console application or daemon
- grab message from queue and execute them in master or child process
- restart child process if it fail
- log crashes or so on
- ...
And Ko-Worker allows to simplify this to just 2 steps
- create config file
- run ko application
Why Ko-worker?
- Used in real life highload GameNet project
- Close to 100% code coverage
- Simple - less then 5 minuted before you start.
Requirements
PHP >= 5.4
pcntl extension installed
posix extension installed
amqp extension installed
Installation
Composer
The recommended way to install library is composer. You can see package information on Packagist.
Do not use composer?
Just clone the repository and take care about autoload for namespace Ko
.
Examples
Look at examples
folder
UNDONE
Usage
First of all your should create your RabbitMq configuration file. For example config.yaml:
Here we configure the exchange producer and queue consumer that our application will have. In this example Ko\AmqpBroker will contain the producer social_activity
and consumer named social_activity
.
You should specify a connection for the client.
If you need to add optional queue or exchange arguments, then your options can be something like this:
another example with message TTL of 20 seconds:
The argument value must be a list of datatype and value. Valid datatypes are:
S
- StringI
- IntegerD
- DecimalT
- TimestampsF
- TableA
- Array
Adapt the arguments
according to your needs.
If you want to bind queue with specific routing keys you can declare it in producer or consumer config:
Producers, Consumers, AqmBroker?
In a messaging application, the process sending messages to the broker is called producer while the process receiving those messages is called consumer. In your application you will have several of them that you can list under their respective entries in the configuration.
Producer
A producer will be used to send messages to the server. In the AMQP Model, messages are sent to an exchange, this means that in the configuration for a producer you will have to specify the connection options along with the exchange options, which usually will be the name of the exchange and the type of it.
Now let's say that you want to process some social network activity in the background. After you add friend, you will publish a message to server with the following information:
Besides the message itself, the Ko\RabbitMq\Producer#publish()
method also accepts an optional routing key parameter and an optional array of additional properties. This way, for example, you can change the application headers.
The next piece of the puzzle is to have a consumer that will take the message out of the queue and process it accordingly.
Consumers
A consumer will connect to the server and start a loop waiting for incoming messages to process. Depending on the specified class for such consumer will be the behavior it will have. Let's review the consumer configuration from above:
As we see there, the class option has a reference to an \MyProject\TestAction class. It should implements Ko\Worker\ActionInterface.
When the consumer gets a message from the server it will create and execute such class. If for testing or debugging purposes you need to specify a different class, then you can change it there.
Apart from the callback we also specify the connection to use, the same way as we do with a producer. The remaining options are the the queue_options. In the queue_options we will provide a queue name and binding.
Why?
As we said, messages in AMQP are published to an exchange. This doesn't mean the message has reached a queue. For this to happen, first we need to create such queue and then bind it to the exchange.
The cool thing about this is that you can bind several queues to one exchange, in that way one message can arrive to several destinations.
The advantage of this approach is the decoupling from the producer and the consumer. The producer does not care about how many consumers will process his messages.
All it needs is that his message arrives to the server. In this way we can expand the actions we perform every time a friend added without the need to change code in our controller.
Now, how to run a consumer? There's a command for it that can be executed like this:
What does this mean?
We are executing the social_activity consumer telling it should use 10 child process to consuming.
Every time the consumer receives a message from the server, it will execute the configured callback passing the AMQP message as an instance of the AMQPEnvelope
class. The message body can be obtained by calling $msg->getBody()
.
By default the consumer will process messages in an endless loop for some definition of endless.
UNDONE
Production
Ko-worker came with nice ko-package
utility which allow you to create executable phar from your worker project.
Credits
Ko-worker written as a part of GameNet project by Nikolay Bondarenko (misterionkell at gmail.com) and Vadim Sabirov (pr0head at gmail.com). We use some interesting ideas from Alvaro Videla Thumper and part of documentation from RabbitMqBundle.
License
Released under the MIT license.
Links
All versions of ko-worker with dependencies
misterion/ko-process Version ~0.4
symfony/yaml Version ^3.0
ulrichsg/getopt-php Version ~3.1.0