RhubarbΒΆ

Examples of Celery Worker Execution From PHP

Send AsyncResult and Wait For Result

use Rhubarb\Rhubarb;
use Rhubarb\Task\Args\Python as PythonTask;

$config = include('configuration/predis.php');
$rhubarb = new Rhubarb($config);
$argsPython = new PythonTask(1, 2);

try {
    $result = $rhubarb->task('app.add')
        ->delay($argsPython, array())
        ->get();
} catch (\Rhubarb\Exception\TimeoutException $e) {
    /*
     * If the task result is not received within '10' seconds (default) a
     * `\Rhubarb\Exception\TimeoutException` is thrown. 
     */
    echo $e->getMessage(), PHP_EOL;
}

Send task with kwargs

use Rhubarb\Rhubarb;
use Rhubarb\Task\Args\Python as PythonArgs;
use Rhubarb\Task\Args\Python\Kwargs;

$config = include('configuration/predis.php');
$rhubarb = new Rhubarb($config);

$kwargs = new Kwargs(array('arg3' => 'this is kwarg three'));
$kwargs['arg_1'] = 'my first arg';
$kwargs->arg2 = 'the second arg';

$args = new PythonArgs($kwargs);

$result = $rhubarb->task('app.add')
    ->delay($args)
    ->get();

Send task using an invokable signature

use Rhubarb\Rhubarb;
use Rhubarb\Task\Args\Python as PythonArgs;

$config = include('configuration/amqp.php');
$rhubarb = new Rhubarb($config);

$args = new PythonArgs(1, 2);

$signature = $rhubarb->task('app.add');
$result = $signature($args)->get();

Send task with a 60 second countdown header

use Rhubarb\Rhubarb;
use Rhubarb\Task\Args\Python as PythonArgs;

$config = include('configuration/predis.php');
$rhubarb = new Rhubarb($config);
$args = new PythonArgs(1, 2);

$rhubarb->task('app.add')
    ->delay($args, array(), array('countdown' => 60)); /* Task will execute in 60 seconds */

Send task using ETA header

use Rhubarb\Rhubarb;
use Rhubarb\Task\Args\Python as PythonArgs;

$config = include('configuration/predis.php');
$rhubarb = new Rhubarb($config);
$args = new PythonArgs(1, 2);

$runAt = new \DateTime();
/* One Hour: 3600S */
$runAt->add(new DateInterval('PT3600S'));

$rhubarb->task('app.add')
    ->delay($args, array(), array('eta' => $runAt->format(\DateTime::ISO8601)));

Send task using Expires header

use Rhubarb\Rhubarb;
use Rhubarb\Task\Args\Python as PythonArgs;

$config = include('configuration/predis.php');
$rhubarb = new Rhubarb($config);
$args = new PythonArgs(1, 2);
$expiresAt = new \DateTime();
/* One Hour: 3600S */
$expiresAt->add(new DateInterval('PT3600S'));
$rhubarb->task('app.add')
    ->delay($args, array(), array('expires' => $expiresAt->format(\DateTime::ISO8601)));

Previous topic

backends

Next topic

Tasks