|
| 1 | +# PHP-FPM Async Queue |
| 2 | + |
| 3 | +Use php-fpm as a simple built-in async queue. Based on interoperable queue interfaces [Queue Interop](https://github.com/queue-interop/queue-interop). |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```bash |
| 8 | +composer makasim/php-fpm-queue:0.1.x-dev queue-interop/queue-interop:0.7.x-dev enqueue/dsn:0.9.x-dev |
| 9 | +``` |
| 10 | + |
| 11 | +A sender script: |
| 12 | + |
| 13 | +```php |
| 14 | +<?php |
| 15 | +# sender.php |
| 16 | + |
| 17 | +use Makasim\PhpFpm\PhpFpmConnectionFactory; |
| 18 | + |
| 19 | +require_once __DIR__.'/vendor/autoload.php'; |
| 20 | + |
| 21 | +$context = (new PhpFpmConnectionFactory('tcp://localhost:9000'))->createContext(); |
| 22 | + |
| 23 | +$queue = $context->createQueue('/app/worker.php'); |
| 24 | +$message = $context->createMessage('aBody'); |
| 25 | + |
| 26 | +$context->createProducer()->send($queue, $message); |
| 27 | +``` |
| 28 | + |
| 29 | +A worker script: |
| 30 | + |
| 31 | + |
| 32 | +```php |
| 33 | +<?php |
| 34 | +# worker.php |
| 35 | + |
| 36 | +use Makasim\PhpFpm\PhpFpmConnectionFactory; |
| 37 | + |
| 38 | +require_once __DIR__.'/vendor/autoload.php'; |
| 39 | + |
| 40 | +$context = (new PhpFpmConnectionFactory('tcp://localhost:9000'))->createContext(); |
| 41 | + |
| 42 | +$queue = $context->createQueue(__FILE__); |
| 43 | + |
| 44 | +$consumer = $context->createConsumer($queue); |
| 45 | + |
| 46 | +if ($message = $consumer->receiveNoWait()) { |
| 47 | + // process message |
| 48 | + |
| 49 | + $consumer->acknowledge($message); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Start PHP-FPM: |
| 54 | + |
| 55 | +```bash |
| 56 | +docker run -v `pwd`:/app -p 9000:9000 php:7.2-fpm |
| 57 | +``` |
| 58 | + |
| 59 | +Send a message: |
| 60 | + |
| 61 | +```bash |
| 62 | +php sender.php |
| 63 | +``` |
| 64 | + |
| 65 | +## Credits |
| 66 | + |
| 67 | +Inspired by Benjamin post [Using php-fpm as a simple built-in async queue](https://tideways.com/profiler/blog/using-php-fpm-as-a-simple-built-in-async-queue) |
| 68 | + |
0 commit comments