Git Product home page Git Product logo

queueplatform's Introduction

Queue Platform

Build Status

PHP Queue Platform provides an easy way to build queue system in PHP.

Easy way

The easiest path to use queue platform is to use QueuePlatformExample repository.

QueuePlatformExample provides a complete working example with sample workers. To run and test it, it comes with configured vagrant setup. To use it just boot up vagrant with vagrant up.

Vagrant setup will set up a complete environment. It will start 5 working processes, setup queue manager URL and server testing GUI.

URL to testing GUI: http://192.168.29.6/index.html

How to modify it, see docs in QueuePlatformExample repository.

Hard way

1. require package

Require QueuePlatform package with composer: composer require bostjanob/queue-platform.

2. create workers

Workers must extend BostjanOb\QueuePlatform\Worker interface.

The only method to implement is run($params = null).

3. create QueueManager class and register workers

Create a new BostjanOb\QueuePlatform\QueueManager object and register your workers with it.

For constructor, you must provide storage object (object that implements \BostjanOb\QueuePlatform\Storage\Storage)

<?php

// queuemanager.php
$storage = new \BostjanOb\QueuePlatform\Storage\SqlLiteStorage('db.sqlite3');
$qm = new \BostjanOb\QueuePlatform\QueueManager($storage);

$qm->registerWorker('task-name', new Worker());

4. make queue manager JSON-RPC public

To push task, get task status and to make communication with working processes QueueManager uses JSON-RPC. So it must be accessible by a web server (over URL).

To listen for json-rpc request run listen() on QueueManager.

<?php

// queue.php - server over web server
require 'queuemanager.php';
echo $qm->listen();

5. start working processes

To start long running working process call work() on QueueManager. Run it from CLI and pass URL to QueueManager (in step 4) as a parameter.

<?php

// process.php
require 'queuemanager.php';
$qm->work();

Run from CLI:

php process.php http://example.com/queue.php

QueueManager JSON-RPC SERVER

QueueManager JSON-RPC provides two methods to interact with.

- queueTask

Queue task to be then later pulled by the processor. It accepts one or two parameters.

the first parameter is the name of a worker registered with a queue manager

the second parameter is a parameter for the worker. It accepts a single value. If you want to pass multiple values use an array.

returned result is a task object as a JSON-RPC result

- getTask

getTask method return task object. The method requires id of a task as a first parameter.

QueueManager CLI working process

To run long running process run PHP script from CLI (step 5.).

Command to start process:

php file.php [OPTIONS] URL_TO_QUEUEMANAGER
Options

Available options:

  • workers - List workers to work with. Default to all registered in QueueManage. (split by comma)
  • sleep - How many seconds to wait if there is no task. The process will check if there is some task and if there is none it will wait before checking again.

Example with options:

php file.php --workers=foo,bar --sleep=3 http://example.com/queue.php

queueplatform's People

Contributors

bostjanob avatar

Watchers

 avatar  avatar

queueplatform's Issues

Handle failed jobs

Handle failed jobs.

If job fails. Send failed request to QueueManager and log it. Give options to retry failed jobs.

Connect to DB when needed

Make connection to DB only when needed and not in __constructor.

This way, workers can be run without DB.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.