PHP code example of roadrunner / jobs

1. Go to this page and download the library: Download roadrunner/jobs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

roadrunner / jobs example snippets




use Spiral\RoadRunner\Jobs\Jobs;
use Spiral\Goridge\RPC\RPC;

));

// Select "local" pipeline from jobs
$queue = $jobs->connect('local');

// Create task prototype with default headers
$task = $queue->create('ping', '{"site": "https://example.com"}') // Create task with "ping" name
    ->withHeader('attempts', '4') // Number of attempts to execute the task
    ->withHeader('retry-delay', '10'); // Delay between attempts

// Push "ping" task to the queue
$task = $queue->dispatch($task);

var_dump($task->getId() . ' has been queued');



use Spiral\RoadRunner\Jobs\Consumer;

 $consumer->waitTask()) {
    try {
        $name = $task->getName(); // "ping"
        $queue = $task->getQueue(); // "local"
        $driver = $task->getDriver(); // Driver::Memory
        $payload = $task->getPayload(); // {"site": "https://example.com"}

        // Process task

        $task->ack();
    } catch (\Throwable $e) {
        $task->requeue($e);
    }
}

$task->nack(message: $reason, redelivery: true);

$task->withHeader('attempts', (string) ($attempts + 1))->requeue($exception);

$task->withDelay(10)->requeue($exception);
yaml
rpc:
  listen: tcp://127.0.0.1:6001

server:
  command: php consumer.php
  relay: pipes

jobs:
  consume: [ "local" ]
  pipelines:
    local:
      driver: memory
      config:
        priority: 10
        prefetch: 10000