Use php-fpm as a simple built-in async queue

Overview

PHP-FPM Async Queue

Use php-fpm as a simple built-in async queue. Based on interoperable queue interfaces Queue Interop.

Usage

composer makasim/php-fpm-queue:0.1.x-dev queue-interop/queue-interop:0.7.x-dev enqueue/dsn:0.9.x-dev

A sender script:

<?php
# sender.php

use Makasim\PhpFpm\PhpFpmConnectionFactory;

require_once __DIR__.'/vendor/autoload.php';

$context = (new PhpFpmConnectionFactory('tcp://localhost:9000'))->createContext();

$queue = $context->createQueue('/app/worker.php');
$message = $context->createMessage('aBody');

$context->createProducer()->send($queue, $message);

A worker script:

<?php
# worker.php

use Makasim\PhpFpm\PhpFpmConnectionFactory;

require_once __DIR__.'/vendor/autoload.php';

$context = (new PhpFpmConnectionFactory('tcp://localhost:9000'))->createContext();
// or
//$context = (new PhpFpmConnectionFactory('unix:///var/run/php/php7.1-fpm.sock'))->createContext(); 

$queue = $context->createQueue(__FILE__);

$consumer = $context->createConsumer($queue);

if ($message = $consumer->receiveNoWait()) {
    // process message

    $consumer->acknowledge($message);
}

Start PHP-FPM:

docker run -v `pwd`:/app -p 9000:9000 php:7.2-fpm

Send a message:

php sender.php

Credits

Inspired by Benjamin post Using php-fpm as a simple built-in async queue

License

MIT License

You might also like...
Magento 2 Message Queue OS AMQP Broker Implementation

Magento 2 Message Queue AMQP Backend AMQP message queue backend implementation for Rcason_Mq. Installation Require the module via Composer $ composer

Queue Management Systems for LPG vendor agencies of Sri Lanka, for the LPG shortages in 2022

gas-queue-mgt Queue Management Systems for LPG vendor agencies of Sri Lanka, for the LPG shortages in 2022 Installation Requirements PHP 7.4 or later

A lightweight queue based on Redis Stream for webman plugin.
A lightweight queue based on Redis Stream for webman plugin.

workbunny/webman-rqueue 🐇 A lightweight queue based on Redis Stream for webman plugin. 🐇 A lightweight queue based on Redis Stream for webman plugin

Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker.
Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker.

symfony-api Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker. ⚙️ Depl

Promoting the interoperability of message queue objects.

Queue Interoperability About queue-interop tries to identify and standardize a common way for PHP programs to create, send, receive and read MQ messag

Tiny, fast and simple PHP boilerplate built on top of FlightPHP

BlessPHP Tiny, fast and simple PHP boilerplate built on top of FlightPHP. Good enough to use as skeleton for prototypes and some pet-projects. The nam

A Simple Linode SDK built for Laravel with @JustSteveKing laravel-transporter package

linode client A Simple Linode client built for Laravel with @JustSteveKing laravel-transporter package Installation You can install the package via co

A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax.

Slack for PHP A simple PHP package for sending messages to Slack with incoming webhooks, focussed on ease-of-use and elegant syntax. Note: this packag

LunarBB is a simple to use, lightweight and accessible Bulletin Board System developed in PHP

LunarBB - Simple Bulletin Board System LunarBB is a simple to use, lightweight and accessible Bulletin Board System developed in PHP. This Webapplicat

Comments
  • error with PhpFpmConnectionFactory

    error with PhpFpmConnectionFactory

    Thanks for the library Im trying to test it but I got this error below. I tried to use the socket but I got same error Here is my code

    <?php
    # sender.php
    
    use Makasim\PhpFpm\PhpFpmConnectionFactory;
    
    require_once __DIR__ . '/vendor/autoload.php';
    
    $context = (new PhpFpmConnectionFactory('tcp://127.0.0.1:9000'))->createContext();
    //$context = (new PhpFpmConnectionFactory('unix://run/php-fpm/www.sock'))->createContext(); 
    ///run/php-fpm/www.sock
    echo 'here'.__DIR__;
    $queue = $context->createQueue(__DIR__ .'/testworker.php');
    $message = $context->createMessage('aBody');
    
    $context->createProducer()->send($queue, $message);
    

    bash-4.2# php testsender.php

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function Enqueue\Dsn\Dsn::__construct(), 1 passed in /var/www/vendor/makasim/php-fpm-queue/src/PhpFpmConnectionFactory.php on line 25 and exactly 10 expected in /var/www/vendor/enqueue/dsn/Dsn.php:59 Stack trace: #0 /var/www/vendor/makasim/php-fpm-queue/src/PhpFpmConnectionFactory.php(25): Enqueue\Dsn\Dsn->__construct('unix://run/php-...') #1 /var/www/testsender.php(9): Makasim\PhpFpm\PhpFpmConnectionFactory->createContext() #2 {main} thrown in /var/www/vendor/enqueue/dsn/Dsn.php on line 59 bash-4.2# php testsender.php

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function Enqueue\Dsn\Dsn::__construct(), 1 passed in /var/www/vendor/makasim/php-fpm-queue/src/PhpFpmConnectionFactory.php on line 25 and exactly 10 expected in /var/www/vendor/enqueue/dsn/Dsn.php:59 Stack trace: #0 /var/www/vendor/makasim/php-fpm-queue/src/PhpFpmConnectionFactory.php(25): Enqueue\Dsn\Dsn->__construct('tcp://127.0.0.1...') #1 /var/www/testsender.php(8): Makasim\PhpFpm\PhpFpmConnectionFactory->createContext() #2 {main} thrown in /var/www/vendor/enqueue/dsn/Dsn.php on line 59 bash-4.2# php -v PHP 7.2.4 (cli) (built: Apr 16 2018 22:22:48) ( NTS )

    opened by usefksa 0
  • fpm didn't emit worker.php

    fpm didn't emit worker.php

    env: docker php7.3:fpm code: all copy from README fpm log: "POST " 200

    I think path is correct, (or not ... path is empty) volume: /home/ubuntu/labs/queue:/app but I don't know why worker.php not working.

    php-fpm.conf -> log_level: debug

    10.0.17.4 -  09/Apr/2019:10:11:54 +0000 "GET /sender.php" 200
    [09-Apr-2019 10:11:54.389192] DEBUG: pid 1, fpm_event_loop(), line 417: event module triggered 1 events
    [09-Apr-2019 10:11:54.391732] DEBUG: pid 1, fpm_event_loop(), line 417: event module triggered 1 events
    10.0.17.4 -  09/Apr/2019:10:11:54 +0000 "POST " 200
    [09-Apr-2019 10:11:55.003241] DEBUG: pid 1, fpm_pctl_perform_idle_server_maintenance(), line 377: [pool www] currently 0 active children, 2 spare children, 2 running children. Spawning rate 1
    
    opened by gai00 0
Releases(0.1.2)
Owner
Max Kotliar
func(){}()
Max Kotliar
Docker-magento2 - 🐳 Docker containers with all required Magento 2 dependencies installed available as FPM through Nginx and CLI

Magento 2 Docker A collection of Docker images for running Magento 2 through nginx and on the command line. Quick Start cp composer.env.sample compose

Meanbee 454 Dec 27, 2022
Beanstalk is a simple, fast work queue.

beanstalkd Simple and fast general purpose work queue. https://beanstalkd.github.io/ See doc/protocol.txt for details of the network protocol. Please

Beanstalkd 6.3k Jan 6, 2023
Venture allows you to create and manage complex, async workflows in your Laravel apps.

Venture is a package to help you build and manage complex workflows of interdependent jobs using Laravel's queueing system. Installation Note: Venture

Kai Sassnowski 680 Dec 14, 2022
An async process dispatcher for Amp.

process This package provides an asynchronous process dispatcher that works on all major platforms (including Windows). As Windows pipes are file hand

AMPHP 204 Jan 8, 2023
This component, based on the Symfony serializer and async-aws, is a human-readable and quick abstraction to easily store serialized objects in DynamoDB 🚀.

DynamoDB Storable This component, based on the Symfony serializer and async-aws, is a human-readable and quick abstraction to easily store serialized

Matthieu W. 2 Jun 19, 2022
Stupid async implementation using await-generator

libAsync Stupid async implementation using await-generator Usage libAsync::doAsync(Closure $executor); // <-- Returns a promise Example Fetch data fro

null 5 Jan 2, 2023
Integrate reCAPTCHA using async HTTP/2, making your app fast with a few lines.

ReCaptcha Integrate reCAPTCHA using async HTTP/2, making your app fast with a few lines. use Illuminate\Support\Facades\Route; Route::post('login', f

Laragear 14 Dec 6, 2022
PHP library for Disque, an in-memory, distributed job queue

disque-php A PHP library for the very promising disque distributed job queue. Features: Support for both PHP (5.5+) and HHVM No dependencies: Fast con

Mariano Iglesias 132 Oct 19, 2022
PHP Web User Queue designed to run on shared hosting

WebUserQueue Introduction This is a web user queueing system written in PHP with a MySQL backend designed to run on shared hosting. It was designed to

Chris 0 Aug 13, 2022
Magento 2 Message Queue Open Source Module

Magento 2 Message Queue Module Lightweight implementation of message queue for Magento 2 Community Edition. System requirements This extension support

Renato 36 Sep 30, 2021