smuuf/celery-for-php

适用于现代PHP的现代Celery客户端。

0.2 2023-11-09 23:32 UTC

This package is auto-updated.

Last update: 2024-09-14 16:55:43 UTC


README

PHP tests

一个适用于Celery - 分布式任务队列的现代PHP客户端库。

要求

  • PHP 8.0+

安装

通过Composer安装celery-for-php

composer require smuuf/celery-for-php

Redis (Predis)

如果您想使用Redis作为代理和/或结果后端,celery-for-php包含一个由Predis支持的Redis驱动程序。

然后需要将Predis Client对象包装在我们的Smuuf\CeleryForPhp\Drivers\PredisRedisDriver驱动程序对象中,该对象为celery-for-php与Redis的实际通信提供必要的接口。

示例用法

<?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)

您还可以使用AMQP/RabbitMQ作为代理,使用Redis作为后端。celery-for-php包含一个由PhpAmqpLib支持的AMQP驱动程序。

需要将PhpAmqpLib AMQPConnectionAMQPSSLConnection对象包装在我们的Smuuf\CeleryForPhp\Drivers\PhpAmqpLibAmqpDriver驱动程序对象中,该对象为celery-for-php通过AMQP的通信提供必要的接口。

示例用法

<?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