1
|
|
|
<?php |
2
|
|
|
namespace Tavii\SQSJobQueueBundle\Storage; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Tavii\SQSJobQueue\Queue\QueueName; |
8
|
|
|
use Tavii\SQSJobQueue\Storage\EntityInterface; |
9
|
|
|
use Tavii\SQSJobQueue\Storage\StorageInterface; |
10
|
|
|
use Tavii\SQSJobQueueBundle\Entity\SqsWorker; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class DoctrineStorage implements StorageInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var EntityManager |
17
|
|
|
*/ |
18
|
|
|
private $entityManager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EntityRepository |
22
|
|
|
*/ |
23
|
|
|
private $repository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param EntityManager $em |
27
|
|
|
* @param EntityRepository $repository |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
EntityManager $em, |
|
|
|
|
31
|
|
|
EntityRepository $repository |
32
|
|
|
) |
33
|
|
|
{ |
34
|
|
|
$this->entityManager = $em; |
35
|
|
|
$this->repository = $repository; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function all() |
42
|
|
|
{ |
43
|
|
|
return $this->repository->findAll(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function find(QueueName $queueName, $server = null, $procId = null) |
50
|
|
|
{ |
51
|
|
|
return $this->repository->findBy($this->createCriteria($queueName, $server, $procId)); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function set(QueueName $queueName, $server, $procId) |
58
|
|
|
{ |
59
|
|
|
$entity = new SqsWorker(); |
60
|
|
|
$entity->setQueueName($queueName) |
61
|
|
|
->setServer($server) |
62
|
|
|
->setProcId($procId) |
63
|
|
|
; |
64
|
|
|
$this->entityManager->persist($entity); |
65
|
|
|
$this->entityManager->flush(); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function get(QueueName $queueName, $server, $procId) |
73
|
|
|
{ |
74
|
|
|
return $this->repository->findOneBy($this->createCriteria($queueName, $server, $procId)); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function remove(QueueName $queueName, $server, $procId) |
81
|
|
|
{ |
82
|
|
|
$entity = $this->get($queueName, $server, $procId); |
83
|
|
|
if ($entity instanceof EntityInterface) { |
84
|
|
|
$this->entityManager->remove($entity); |
85
|
|
|
$this->entityManager->flush(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function removeForce(QueueName $queueName, $server) |
93
|
|
|
{ |
94
|
|
|
/** @var SqsWorker $entity */ |
95
|
|
|
foreach ($this->find($queueName, $server) as $entity) { |
96
|
|
|
$this->entityManager->remove($entity); |
97
|
|
|
$this->entityManager->flush(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param QueueName $queueName |
104
|
|
|
* @param string|null $server |
105
|
|
|
* @param string|null $procId |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function createCriteria(QueueName $queueName, $server = null, $procId = null) |
109
|
|
|
{ |
110
|
|
|
$criteria = array( |
111
|
|
|
'queue' => $queueName->getName(), |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
if (!empty($queueName->getPrefix())) { |
115
|
|
|
$criteria['prefix'] = $queueName->getPrefix(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($server !== null) { |
119
|
|
|
$criteria['server'] = $server; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($procId !== null) { |
123
|
|
|
$criteria['procId'] = $procId; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $criteria; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.