Giter Club home page Giter Club logo

celery-for-php's Introduction

celery-for-php ๐ŸŒฑ

PHP tests

A modern PHP client library for Celery - Distributed Task Queue.

Requirements

  • PHP 8.0+

Installation

Install celery-for-php via Composer.

composer require smuuf/celery-for-php

Redis (Predis)

If you want to use Redis as a broker and/or result backend, celery-for-php contains a Redis driver backed by Predis.

The Predis Client object then needs to be wrapped in our Smuuf\CeleryForPhp\Drivers\PredisRedisDriver driver object, which provides the necessary interface for celery-for-php's actual communication with Redis.

Example usage

<?php

use Predis\Client as PredisClient;

use Smuuf\CeleryForPhp\Celery;
use Smuuf\CeleryForPhp\TaskSignature;
use Smuuf\CeleryForPhp\Brokers\RedisBroker;
use Smuuf\CeleryForPhp\Drivers\PredisRedisDriver;
use Smuuf\CeleryForPhp\Backends\RedisBackend;

$predis = new PredisClient(['host' => '127.0.0.1']);
$redisDriver = new PredisRedisDriver($predis);

$celery = new Celery(
	new RedisBroker($redisDriver),
	new RedisBackend($redisDriver),
	// Optionally explicit config object.
	// config: new \Smuuf\CeleryForPhp\Config(...)
);

$task = new TaskSignature(
	taskName: 'my_celery_app.add_numbers',
	queue: 'my_queue', // Optional, 'celery' by default.
	args: [1, 3, 5],
	// kwargs: ['arg_a' => 123, 'arg_b' => 'something'],
	// eta: 'now +10 minutes',
	// ... or more optional arguments.
);

// Send the task into Celery.
$asyncResult = $celery->sendTask($task);

// Wait for the result (up to 10 seconds by default) and return it.
// Alternatively a \Smuuf\CeleryForPhp\Exc\CeleryTimeoutException exception will
// be thrown if the task won't finish in time.
$result = $asyncResult->get();
// $result === 9

AMQP/RabbitMQ (PhpAmqpLib)

You can use AMQP/RabbitMQ as the broker instead, with Redis as the backend. celery-for-php contains a AMQP driver backed by PhpAmqpLib.

The PhpAmqpLib AMQPConnection or AMQPSSLConnection object needs to be wrapped in our Smuuf\CeleryForPhp\Drivers\PhpAmqpLibAmqpDriver driver object, which provides the necessary interface for celery-for-php's actual communication via AMQP.

Example usage

<?php

use Predis\Client as PredisClient;

use Smuuf\CeleryForPhp\Celery;
use Smuuf\CeleryForPhp\TaskSignature;
use Smuuf\CeleryForPhp\Brokers\AmqpBroker;
use Smuuf\CeleryForPhp\Drivers\PredisRedisDriver;
use Smuuf\CeleryForPhp\Drivers\PhpAmqpLibAmqpDriver;
use PhpAmqpLib\Connection\AMQPSSLConnection;
use Smuuf\CeleryForPhp\Backends\RedisBackend;

//$amqpConn = new AMQPConnection(['127.0.0.1', '5672', '', '', '/']);
$amqpConn = new AMQPSSLConnection(['127.0.0.1', '5672', '', '', '/', ['verify_peer'=>false]]);
$amqpDriver = new PhpAmqpLibAmqpDriver($amqpConn);

$predis = new PredisClient(['host' => '127.0.0.1']);
$redisDriver = new PredisRedisDriver($predis);

$celery = new Celery(
	new AmqpBroker($amqpDriver),
	new RedisBackend($redisDriver),
	// Optionally explicit config object.
	// config: new \Smuuf\CeleryForPhp\Config(...)
);

$task = new TaskSignature(
	taskName: 'my_celery_app.add_numbers',
	queue: 'my_queue', // Optional, 'celery' by default.
	args: [1, 3, 5],
	// kwargs: ['arg_a' => 123, 'arg_b' => 'something'],
	// eta: 'now +10 minutes',
	// ... or more optional arguments.
);

// Send the task into Celery.
$asyncResult = $celery->sendTask($task);

// Wait for the result (up to 10 seconds by default) and return it.
// Alternatively a \Smuuf\CeleryForPhp\Exc\CeleryTimeoutException exception will
// be thrown if the task won't finish in time.
$result = $asyncResult->get();
// $result === 9

celery-for-php's People

Contributors

smuuf avatar usererrorllc avatar

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.