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

Skip to content

Commit 9524bc4

Browse files
committed
Fixed doctrine provider
1 parent 4f93f9c commit 9524bc4

7 files changed

Lines changed: 49 additions & 38 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ class Configuration implements ConfigurationInterface
1818
public function getConfigTreeBuilder()
1919
{
2020
$treeBuilder = new TreeBuilder();
21-
$rootNode = $treeBuilder->root('xaav_queue_bundle');
21+
$rootNode = $treeBuilder->root('xaav_queue');
2222

23-
// Here you should define the parameters that are allowed to
24-
// configure your bundle. See the documentation linked above for
25-
// more information on that topic.
23+
$rootNode
24+
->children()
25+
->scalarNode('driver')
26+
->cannotBeOverwritten()
27+
->isRequired()
28+
->cannotBeEmpty()
29+
->end()
30+
->end();
2631

2732
return $treeBuilder;
2833
}

DependencyInjection/XaavQueueExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class XaavQueueExtension extends Extension
1111
{
1212
public function load(array $configs, ContainerBuilder $container)
1313
{
14-
$configuration = new Configuration();
15-
$config = $this->processConfiguration($configuration, $configs);
14+
//$configuration = new Configuration();
15+
//$config = $this->processConfiguration($configuration, $configs);
1616

1717
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
1818
$loader->load('services.yml');

Entity/JobQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class JobQueue extends DoctrineJobQueue implements JobQueueInterface
2424
protected $name;
2525

2626
/**
27-
* @ORM\OneToMany(targetEntity="SerializedJob", mappedBy="jobQueue")
27+
* @ORM\OneToMany(targetEntity="SerializedJob", mappedBy="jobQueue",cascade={"persist", "all"})
2828
*/
2929
protected $serializedJobs;
3030

Entity/SerializedJob.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ class SerializedJob
2222
protected $data;
2323

2424
/**
25-
* ORM\ManyToOne(targetEntity="jobQueue", mappedBy="serializedJobs")
25+
* @ORM\ManyToOne(targetEntity="JobQueue")
2626
*/
2727
protected $jobQueue;
2828

29+
public function getJobQueue()
30+
{
31+
return $this->jobQueue;
32+
}
33+
34+
public function setJobQueue(JobQueue $jobQueue)
35+
{
36+
$this->jobQueue = $jobQueue;
37+
}
38+
2939
public function setData($data)
3040
{
3141
$this->data = $data;

JobQueue/JobQueue/DoctrineJobQueue.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Xaav\QueueBundle\JobQueue\JobQueue;
44

5+
use Doctrine\ORM\EntityManager;
56
use Xaav\QueueBundle\Entity\SerializedJob;
67
use Xaav\QueueBundle\JobQueue\Job\JobInterface;
78
use Doctrine\Common\Collections\ArrayCollection;
@@ -13,18 +14,31 @@ class DoctrineJobQueue implements JobQueueInterface
1314
*/
1415
protected $serializedJobs;
1516

17+
/**
18+
* @var EntityManager
19+
*/
20+
protected $entityManager;
21+
22+
public function setEntityManager(EntityManager $entityManager)
23+
{
24+
$this->entityManager = $entityManager;
25+
}
26+
1627
public function getJobFromQueue()
1728
{
1829
$serializedJob = $this->serializedJobs->last();
19-
$this->serializedJobs->remove($this->serializedJobs->key());
30+
$this->entityManager->remove($serializedJob);
2031

21-
return unserialize($serializedJob->getData());
32+
if ($serializedJob) {
33+
return unserialize($serializedJob->getData());
34+
}
2235
}
2336

2437
public function addJobToQueue(JobInterface $job)
2538
{
2639
$serializedJob = new SerializedJob();
2740
$serializedJob->setData(serialize($job));
41+
$serializedJob->setJobQueue($this);
2842

2943
$this->serializedJobs->add($serializedJob);
3044
}

JobQueue/Provider/DoctrineProvider.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,34 @@
22

33
namespace Xaav\QueueBundle\JobQueue\Provider;
44

5+
use Doctrine\ORM\EntityManager;
56
use Xaav\QueueBundle\Entity\JobQueue;
67

78
class DoctrineProvider implements JobQueueProviderInterface
89
{
910
protected $entityManager;
1011

11-
public function __construct($entityManager)
12+
public function __construct(EntityManager $entityManager)
1213
{
1314
$this->entityManager = $entityManager;
1415
}
1516

1617
public function getJobQueueByName($name)
1718
{
18-
$result = $this->entityManager
19+
$queue = $this->entityManager
1920
->getRepository('XaavQueueBundle:JobQueue')
2021
->findOneByName($name);
2122

22-
if($result) {
23-
24-
return $result;
25-
}
26-
else {
23+
if(!$queue) {
2724

2825
$queue = new JobQueue();
2926
$queue->setName($name);
3027

3128
$this->entityManager->persist($queue);
32-
33-
return $queue;
3429
}
35-
}
3630

37-
public function __destruct()
38-
{
39-
try {
40-
$this->entityManager->flush();
41-
}
42-
catch (\Exception $ex)
43-
{
44-
//Throwing an exception will cause bad things!
45-
}
31+
$queue->setEntityManager($this->entityManager);
32+
33+
return $queue;
4634
}
4735
}

Resources/config/services.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
services:
2-
xaav.jobqueue.provider.amqp:
3-
class: Xaav\QueueBundle\JobQueue\Provider\AMQPProvider
4-
arguments: []
5-
xaav.jobqueue.provider.doctrine:
6-
class: Xaav\QueueBundle\JobQueue\Provider\DoctrineProvider
7-
arguments: [@doctrine.orm.entity_manager]
8-
xaav.jobqueue.provider.flatfile:
9-
class: Xaav\QueueBundle\JobQueue\Provider\FlatFileProvider
10-
arguments: [%kernel.cache_dir%]
2+
xaav.jobqueue.processor:
3+
class: Xaav\QueueBundle\JobQueue\JobQueueProcessor
4+
arguments: [ @xaav.jobqueue.provider ]

0 commit comments

Comments
 (0)