// Dependency inject the worker or fetch it from the container
$fibonacciWorker = $container->get('App\Worker\FibonacciWorker');
// For Symfony 3.3, 3.4
// $fibonacciWorker = $container->get('AppBundle\Worker\FibonacciWorker');
//
// For Symfony 2, 3.0, 3.1, 3.2:
// $fibonacciWorker = $container->get('app.worker.fibonacci');
// Basic Examples
$fibonacciWorker->later()->fibonacci(20);
$fibonacciWorker->later()->fibonacciFile(20);
// Batch Example
$fibonacciWorker->batchLater()->fibonacci(20); // Batch up runs into a single run
// Timed Example
$fibonacciWorker->later(90)->fibonacci(20); // Run 90 seconds later
// Priority
// Note: whether 1 == High or Low priority is configurable, but by default it is High
$fibonacciWorker->later(0, 1); // As soon as possible, High priority
$fibonacciWorker->later(0, 125); // Medium priority
$fibonacciWorker->later(0, 255); // Low priority
// Advanced Usage Example:
// (If the job is not processed by $expireTime, then don't execute it ever...)
$expireTime = time() + 3600;
$fibonacciWorker->later()->setExpiresAt(new \DateTime("@$expireTime"))->fibonacci(20); // Must be run within the hour or not at allExample:
bin/console dtc:queue:create fibonacci fibonacci 20It's recommended to stick with primitives (string, int, float, bool, null) or arrays of primitives. Objects are not supported as arguments. This not only promotes loose coupling between the enqueuer and the job, it allows manually enqueuing via cli. In a real world scenario, things can go wrong in such a way you may need to manually enqueue a job.
Considering a more complicated job:
class Hello extends \Dtc\QueueBundle\Model\Worker
{
public function run(string $name = null, int $times = 1, bool $askHowTheyAre = false): int
{
if (null === $name) {
$name = '';
}
for ($i = 0; $i < $times; $i++) {
$message = sprintf(
'Hello %s',
$name
);
if ($askHowTheyAre) {
$message .= ', How are you?';
}
print($message.PHP_EOL);
}
return self::RESULT_SUCCESS;
}
public function getName() {
return 'hello';
}
}A cli command could enqueue the above job:
$ bin/console dtc:queue:create_job -j hello run '[ null, 3, true ]'
Hello, How are you?
Hello, How are you?
Hello, How are you?
$ bin/console dtc:queue:create_job -j hello run '[ "Matthew" ]'
Hello Matthew$ bin/console dtc:queue:create_job -p hello run 'a:3:{i:0;N;i:1;i:3;i:2;b:1;}'
Hello, How are you?
Hello, How are you?
Hello, How are you?
$ bin/console dtc:queue:create_job -p hello run 'a:1:{i:0;s:7:"Matthew";}'
Hello Matthew$ bin/console dtc:queue:create_job --interpret-args hello run null 3 true
Hello, How are you?
Hello, How are you?
Hello, How are you?
$ bin/console dtc:queue:create_job --interpret-args hello run Matthew
Hello MatthewPlease note, the last bash argument is a json encoded string of arguments to be passed to the php method:
[
null,
3,
true
][
"Matthew"
]in php this would be represented as:
$job = new Hello();
$job->run(null, 3, true);
$job->run("Matthew");