Skip to content

Instantly share code, notes, and snippets.

@erdemkeren
Last active March 18, 2020 11:05
Show Gist options
  • Save erdemkeren/bc56f40b290bca443345ce2aeb00e969 to your computer and use it in GitHub Desktop.
Save erdemkeren/bc56f40b290bca443345ce2aeb00e969 to your computer and use it in GitHub Desktop.
Using queue

Creating a Job for the Queue Worker:

<?php

use Common\Queue\Interfaces\JobInterface;

class ExampleJob implements JobInterface
{
    private $id = 0;
    private $string = 'hello';
    private $array = ['key' => 'value'];

    public function handle(array $attributes): void
    {
        echo ('Done.');
    }
}
use TestJob;
use Common\Queue\Dispatcher;

/** @var Dispatcher $dispatcher */
$dispatcher = $this->di->get('queue.dispatcher');
$exampleJob = new ExampleJob();

$dispatcher->dispatch($exampleJob, ['id' => 1, 'message' => 'foo bar']);

The ExampleJob instance will be serialized using serialize function and then deserialized when consuming the message.

Running the Job:

To run the queue worker, execute the following command:

./bin/robo queue:work

To run the worker on a specific queue, specify it using the queue option:

./bin/robo queue:work --queue=default

To prevent the output, use the silent option:

./bin/robo queue:work --silent

If you want to handle the next job only, use once:

./bin/robo queue:work --once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment