1. Go to this page and download the library: Download mmucklo/queue-bundle 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/ */
mmucklo / queue-bundle example snippets
namespace App\Worker; // for symfony 2/3, the namespace would typically be AppBundle\Worker
class Fibonacci
extends \Dtc\QueueBundle\Model\Worker
{
private $filename;
public function __construct() {
$this->filename = '/tmp/fib-result.txt';
}
public function fibonacciFile($n) {
$fib = $this->fibonacci($n);
file_put_contents($this->filename, "{$n}: {$fib}");
}
public function fibonacci($n)
{
if($n == 0)
return 0; //F0
elseif ($n == 1)
return 1; //F1
else
return $this->fibonacci($n - 1) + $this->fibonacci($n - 2);
}
public function getName() {
return 'fibonacci';
}
public function getFilename()
{
return $this->filename;
}
}
// Dependency inject the worker or fetch it from the container
$fibonacci = $container->get('App\Worker\Fibonacci');
// For Symfony 3.3, 3.4
// $fibonacci = $container->get('AppBundle\Worker\Fibonacci');
//
// For Symfony 2, 3.0, 3.1, 3.2:
// $fibonacci = $container->get('app.worker.fibonacci');
// Basic Examples
$fibonacci->later()->fibonacci(20);
$fibonacci->later()->fibonacciFile(20);
// Batch Example
$fibonacci->batchLater()->fibonacci(20); // Batch up runs into a single run
// Timed Example
$fibonacci->later(90)->fibonacci(20); // Run 90 seconds later
// Priority
// Note: whether 1 == High or Low priority is configurable, but by default it is High
$fibonacci->later(0, 1); // As soon as possible, High priority
$fibonacci->later(0, 125); // Medium priority
$fibonacci->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;
$fibonacci->later()->setExpiresAt(new \DateTime("@$expireTime"))->fibonacci(20); // Must be run within the hour or not at all
namespace App\Entity; // Or whatever
use Dtc\QueueBundle\Entity\Job as BaseJob;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'job_some_other_name')]
#[ORM\Index(columns: ['crc_hash', 'status'], name: 'job_crc_hash_idx')]
#[ORM\Index(columns: ['priority', 'whenAt'], name: 'job_priority_idx')]
#[ORM\Index(columns: ['whenAt'], name: 'job_when_idx')]
#[ORM\Index(columns: ['status', 'whenAt'], name: 'job_status_idx')]
class Job extends BaseJob {
}
// ... similarly for Entity\JobArchive if necessary
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Dtc\QueueBundle\Document\Job as BaseJob;
#[ODM\Document(db: 'my_db', collection: 'my_job_collection')]
class Job extends BaseJob
{
}
// ... similarly for Document\JobArchive if necessary
use Dtc\QueueBundle\EventDispatcher\Event;
use Dtc\QueueBundle\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ClearManagerSubscriber
implements EventSubscriberInterface
{
private $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function onPostJob(Event $event)
{
$managerIds = [
'doctrine.odm.mongodb.document_manager',
'doctrine.orm.default_entity_manager',
'doctrine.orm.content_entity_manager'
];
foreach ($managerIds as $id) {
$manager = $this->container->get($id);
$manager->clear();
}
}
public static function getSubscribedEvents()
{
return array(
Event::POST_JOB => 'onPostJob',
);
}
}