Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e54d60

Browse files
committed
Updated structure.
1 parent 3874288 commit 4e54d60

11 files changed

Lines changed: 145 additions & 188 deletions

File tree

Command/ProcessQueueCommand.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ protected function configure()
2020

2121
protected function execute(InputInterface $input, OutputInterface $output)
2222
{
23-
$processor = $this->getContainer()
24-
->get('xaav.queue.processor');
23+
$manager = $this->getContainer()
24+
->get('xaav.queue.manager');
2525

26-
$queueName = $input->getArgument('name');
26+
$queue = $manager->get($input->getArgument('name'));
2727

2828
$output->writeln('Processing Queue, press Ctrl+C to stop');
2929

30-
while (true) {
31-
$output->writeln('Checking for jobs...');
32-
while (!$processor->process($queueName)) {
33-
$output->writeln('Processed Job');
30+
while ($job = $queue->get()) {
31+
if ($job) {
32+
$job->process();
3433
}
35-
$output->writeln('Sleeping for three seconds...');
3634
sleep(3);
3735
}
3836
}

Entity/Queue.php

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace Xaav\QueueBundle\Entity;
44

5-
use Doctrine\ORM\EntityManager;
65
use Xaav\QueueBundle\Queue\Job\JobInterface;
7-
use Xaav\QueueBundle\Queue\QueueInterface;
86
use Doctrine\ORM\Mapping as ORM;
97

108
/**
119
* @ORM\Entity()
1210
*/
13-
class Queue implements QueueInterface
11+
class Queue
1412
{
1513
/**
1614
* @ORM\Id
@@ -29,11 +27,6 @@ class Queue implements QueueInterface
2927
*/
3028
protected $serializedJobs;
3129

32-
/**
33-
* @var EntityManager
34-
*/
35-
protected $entityManager;
36-
3730
/**
3831
* Constructor
3932
*/
@@ -69,41 +62,18 @@ public function getId()
6962
}
7063

7164
/**
72-
* Set Entity Manager
73-
*/
74-
public function setEntityManager(EntityManager $entityManager)
65+
* Add serializedJobs
66+
*/
67+
public function addSerializedJobs(SerializedJob $serializedJobs)
7568
{
76-
$this->entityManager = $entityManager;
69+
$this->serializedJobs[] = $serializedJobs;
7770
}
7871

7972
/**
80-
* {@InheritDoc}
73+
* Get serializedJobs
8174
*/
82-
public function get()
75+
public function getSerializedJobs()
8376
{
84-
$serializedJob = $this->serializedJobs->last();
85-
if ($serializedJob) {
86-
$this->serializedJobs->removeElement($serializedJob);
87-
$this->entityManager->remove($serializedJob);
88-
89-
$this->entityManager->flush();
90-
91-
return unserialize($serializedJob->getData());
92-
}
93-
}
94-
95-
/**
96-
* {@InheritDoc}
97-
*/
98-
public function add(JobInterface $job)
99-
{
100-
$serializedJob = new SerializedJob();
101-
$serializedJob->setData(serialize($job));
102-
$serializedJob->setQueue($this);
103-
104-
$this->serializedJobs->add($serializedJob);
105-
$this->entityManager->persist($serializedJob);
106-
107-
$this->entityManager->flush();
77+
return $this->serializedJobs;
10878
}
10979
}

Entity/SerializedJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getQueue()
3737
/**
3838
* Set Job Queue
3939
*/
40-
public function setQueue(Queue $queue)
40+
public function setQueue(Queue $queue = null)
4141
{
4242
$this->queue = $queue;
4343
}

FlatFile/Queue.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

Queue/Adapter/DoctrineAdapter.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Xaav\QueueBundle\Queue\Adapter;
44

5+
use Xaav\QueueBundle\Entity\SerializedJob;
6+
57
use Xaav\QueueBundle\Entity\Queue;
68

79
class DoctrineAdapter implements QueueAdapterInterface
@@ -14,23 +16,53 @@ public function __construct(array $options)
1416
$this->entityManager = $container->get('doctrine')->getEntityManager();
1517
}
1618

19+
/**
20+
* @return Queue
21+
*/
22+
protected function getQueue($name)
23+
{
24+
$queue = $this->entityManager
25+
->getRepository('XaavQueueBundle:Queue')
26+
->findOneByName($name);
27+
28+
if (!$queue) {
29+
30+
$queue = new Queue();
31+
$queue->setName($name);
32+
33+
$this->entityManager->persist($queue);
34+
}
35+
36+
if ($queue->getSerializedJobs()->count() == 0) {
37+
$this->entityManager->refresh($queue);
38+
}
39+
40+
return $queue;
41+
}
42+
1743
public function get($name)
1844
{
19-
$queue = $this->entityManager
20-
->getRepository('XaavQueueBundle:Queue')
21-
->findOneByName($name);
45+
$queue = $this->getQueue($name);
46+
47+
$job = $queue->getSerializedJobs()->last();
48+
$job->setQueue();
2249

23-
if(!$queue) {
50+
$this->entityManager->remove($job);
51+
$this->entityManager->flush();
2452

25-
$queue = new Queue();
26-
$queue->setName($name);
53+
return $job->getData();
54+
}
55+
56+
public function add($name, $job)
57+
{
58+
$queue = $this->getQueue($name);
2759

28-
$this->entityManager->persist($queue);
29-
}
60+
$job = new SerializedJob();
61+
$job->setData($job);
3062

31-
$this->entityManager->refresh($queue);
32-
$queue->setEntityManager($this->entityManager);
63+
$queue->addSerializedJobs($job);
64+
$this->entityManager->persist($job);
3365

34-
return $queue;
66+
$this->entityManager->flush();
3567
}
3668
}

Queue/Adapter/QueueAdapterInterface.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ interface QueueAdapterInterface
77
public function __construct(array $options);
88

99
/**
10-
* Gets the queue with the specified name from this provider.
10+
* Gets a Job from the specified queue
1111
*
12-
* @param string $name A unique identifier of the queue
12+
* @param string $queue The queue to get the job from
13+
*
14+
* @return JobInterface A job that needs to be processed
15+
*/
16+
public function get($queue);
17+
18+
/**
19+
* Add the job to the specified queue
20+
*
21+
* @param string $queue The queue to add the job to
22+
* @param string $job The job to add
1323
*/
14-
public function get($name);
24+
public function add($queue, $job);
1525
}

Queue/Queue.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Xaav\QueueBundle\Queue;
4+
5+
use Xaav\QueueBundle\Queue\Job\JobInterface;
6+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use Xaav\QueueBundle\Queue\Adapter\QueueAdapterInterface;
9+
10+
class Queue implements QueueInterface
11+
{
12+
/**
13+
* @var QueueAdapterInterface
14+
*/
15+
protected $adapter;
16+
17+
/**
18+
* @var ContainerInterface
19+
*/
20+
protected $container;
21+
protected $name;
22+
23+
public function __construct(QueueAdapterInterface $adapter, ContainerInterface $container, $name)
24+
{
25+
$this->adapter = $adapter;
26+
$this->container = $container;
27+
$this->name = $name;
28+
}
29+
30+
public function get()
31+
{
32+
$job = unserialize($this->adapter->get($this->name));
33+
if ($job instanceof ContainerAwareInterface) {
34+
$job->setContainer($this->container);
35+
}
36+
37+
return $job;
38+
}
39+
40+
public function add(JobInterface $job)
41+
{
42+
$this->adapter->add($this->name, serialize($job));
43+
}
44+
}

Queue/QueueManager.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Xaav\QueueBundle\Queue;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use Xaav\QueueBundle\Queue\Adapter\QueueAdapterInterface;
7+
8+
class QueueManager
9+
{
10+
protected $adapter;
11+
protected $container;
12+
13+
public function __construct(QueueAdapterInterface $adapter, ContainerInterface $container)
14+
{
15+
$this->adapter = $adapter;
16+
$this->container = $container;
17+
}
18+
19+
/**
20+
* Return a queue with the specified name
21+
*
22+
* @param string $name The name of the queue to return
23+
*/
24+
public function get($queue)
25+
{
26+
return new Queue($this->adapter, $this->container, $name);
27+
}
28+
}

Queue/QueueProcessor.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)