A modern PHP client library for Celery - Distributed Task Queue.
- PHP 8.0+
Install celery-for-php via Composer.
composer require smuuf/celery-for-phpIf 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.
<?phpusePredis\ClientasPredisClient;
useSmuuf\CeleryForPhp\Celery;
useSmuuf\CeleryForPhp\TaskSignature;
useSmuuf\CeleryForPhp\Brokers\RedisBroker;
useSmuuf\CeleryForPhp\Drivers\PredisRedisDriver;
useSmuuf\CeleryForPhp\Backends\RedisBackend;
$predis = newPredisClient(['host' => '127.0.0.1']);
$redisDriver = newPredisRedisDriver($predis);
$celery = newCelery(
newRedisBroker($redisDriver),
newRedisBackend($redisDriver),
// Optionally explicit config object.// config: new \Smuuf\CeleryForPhp\Config(...)
);
$task = newTaskSignature(
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 === 9You 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.
<?phpusePredis\ClientasPredisClient;
useSmuuf\CeleryForPhp\Celery;
useSmuuf\CeleryForPhp\TaskSignature;
useSmuuf\CeleryForPhp\Brokers\AmqpBroker;
useSmuuf\CeleryForPhp\Drivers\PredisRedisDriver;
useSmuuf\CeleryForPhp\Drivers\PhpAmqpLibAmqpDriver;
usePhpAmqpLib\Connection\AMQPSSLConnection;
useSmuuf\CeleryForPhp\Backends\RedisBackend;
//$amqpConn = new AMQPConnection(['127.0.0.1', '5672', '', '', '/']);$amqpConn = newAMQPSSLConnection(['127.0.0.1', '5672', '', '', '/', ['verify_peer'=>false]]);
$amqpDriver = newPhpAmqpLibAmqpDriver($amqpConn);
$predis = newPredisClient(['host' => '127.0.0.1']);
$redisDriver = newPredisRedisDriver($predis);
$celery = newCelery(
newAmqpBroker($amqpDriver),
newRedisBackend($redisDriver),
// Optionally explicit config object.// config: new \Smuuf\CeleryForPhp\Config(...)
);
$task = newTaskSignature(
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