Skip to content

Commit 9010c31

Browse files
authored
Merge pull request php-enqueue#1081 from balabis/master
Added: ability to choose different entity manager
2 parents d10414d + 51ffcea commit 9010c31

File tree

14 files changed

+35
-101
lines changed

14 files changed

+35
-101
lines changed

docs/bundle/config_reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enqueue:
6767
queue_name: ~
6868
job:
6969
enabled: false
70+
default_mapping: true
7071
async_events:
7172
enabled: false
7273
extensions:

docs/bundle/job_queue.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ enqueue:
6060
# plus basic bundle configuration
6161

6262
job: true
63+
64+
# adds bundle's default Job entity mapping to application's entity manager.
65+
# set it to false when using your own mapped entities for jobs.
66+
default_mapping: true
6367

6468
doctrine:
6569
# plus basic bundle configuration

pkg/enqueue-bundle/DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ private function getJobConfiguration(): ArrayNodeDefinition
9494
}
9595

9696
return (new ArrayNodeDefinition('job'))
97+
->children()
98+
->booleanNode('default_mapping')
99+
->defaultTrue()
100+
->info('Adds bundle\'s default Job entity mapping to application\'s entity manager')
101+
->end()
102+
->end()
97103
->addDefaultsIfNotSet()
98104
->canBeEnabled()
99105
;

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ private function registerJobQueueDoctrineEntityMapping(ContainerBuilder $contain
171171
return;
172172
}
173173

174+
$config = $container->getExtensionConfig('enqueue');
175+
176+
if (!empty($config)) {
177+
$processedConfig = $this->processConfiguration(new Configuration(false), $config);
178+
179+
foreach ($processedConfig as $name => $modules) {
180+
if (isset($modules['job']) && false === $modules['job']['default_mapping']) {
181+
return;
182+
}
183+
}
184+
}
185+
174186
foreach ($container->getExtensionConfig('doctrine') as $config) {
175187
// do not register mappings if dbal not configured.
176188
if (!empty($config['dbal'])) {

pkg/enqueue-bundle/Tests/Functional/App/AppKernel.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public function getLogDir()
3737
return sys_get_temp_dir().'/EnqueueBundle/cache/logs';
3838
}
3939

40-
/**
41-
* @param \Symfony\Component\Config\Loader\LoaderInterface $loader
42-
*/
4340
public function registerContainerConfiguration(LoaderInterface $loader)
4441
{
4542
$loader->load(__DIR__.'/config/config.yml');

pkg/job-queue/CalculateRootJobStatusService.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ class CalculateRootJobStatusService
1212
*/
1313
private $jobStorage;
1414

15-
/**
16-
* @param JobStorage $jobStorage
17-
*/
1815
public function __construct(JobStorage $jobStorage)
1916
{
2017
$this->jobStorage = $jobStorage;
2118
}
2219

2320
/**
24-
* @param Job $job
25-
*
2621
* @return bool true if root job was stopped
2722
*/
2823
public function calculate(Job $job)
@@ -91,11 +86,7 @@ protected function calculateRootJobStatus(array $jobs)
9186
$success++;
9287
break;
9388
default:
94-
throw new \LogicException(sprintf(
95-
'Got unsupported job status: id: "%s" status: "%s"',
96-
$job->getId(),
97-
$job->getStatus()
98-
));
89+
throw new \LogicException(sprintf('Got unsupported job status: id: "%s" status: "%s"', $job->getId(), $job->getStatus()));
9990
}
10091
}
10192

pkg/job-queue/DependentJobContext.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ class DependentJobContext
1414
*/
1515
private $dependentJobs;
1616

17-
/**
18-
* @param Job $job
19-
*/
2017
public function __construct(Job $job)
2118
{
2219
$this->job = $job;

pkg/job-queue/DependentJobProcessor.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ class DependentJobProcessor implements Processor, TopicSubscriberInterface
3030
*/
3131
private $logger;
3232

33-
/**
34-
* @param JobStorage $jobStorage
35-
* @param ProducerInterface $producer
36-
* @param LoggerInterface $logger
37-
*/
3833
public function __construct(JobStorage $jobStorage, ProducerInterface $producer, LoggerInterface $logger)
3934
{
4035
$this->jobStorage = $jobStorage;

pkg/job-queue/DependentJobService.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,17 @@ public function __construct(JobStorage $jobStorage)
2020
}
2121

2222
/**
23-
* @param Job $job
24-
*
2523
* @return DependentJobContext
2624
*/
2725
public function createDependentJobContext(Job $job)
2826
{
2927
return new DependentJobContext($job);
3028
}
3129

32-
/**
33-
* @param DependentJobContext $context
34-
*/
3530
public function saveDependentJob(DependentJobContext $context)
3631
{
3732
if (!$context->getJob()->isRoot()) {
38-
throw new \LogicException(sprintf(
39-
'Only root jobs allowed but got child. jobId: "%s"',
40-
$context->getJob()->getId()
41-
));
33+
throw new \LogicException(sprintf('Only root jobs allowed but got child. jobId: "%s"', $context->getJob()->getId()));
4234
}
4335

4436
$this->jobStorage->saveJob($context->getJob(), function (Job $job) use ($context) {

pkg/job-queue/Job.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ public function getCreatedAt()
237237
* Do not call from the outside.
238238
*
239239
* @internal
240-
*
241-
* @param \DateTime $createdAt
242240
*/
243241
public function setCreatedAt(\DateTime $createdAt)
244242
{
@@ -258,8 +256,6 @@ public function getStartedAt()
258256
* Do not call from the outside.
259257
*
260258
* @internal
261-
*
262-
* @param \DateTime $startedAt
263259
*/
264260
public function setStartedAt(\DateTime $startedAt)
265261
{
@@ -279,8 +275,6 @@ public function getStoppedAt()
279275
* Do not call from the outside.
280276
*
281277
* @internal
282-
*
283-
* @param \DateTime $stoppedAt
284278
*/
285279
public function setStoppedAt(\DateTime $stoppedAt)
286280
{
@@ -324,9 +318,6 @@ public function getData()
324318
return $this->data;
325319
}
326320

327-
/**
328-
* @param array $data
329-
*/
330321
public function setData(array $data)
331322
{
332323
$this->data = $data;

pkg/job-queue/JobProcessor.php

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ class JobProcessor
1717
*/
1818
private $producer;
1919

20-
/**
21-
* @param JobStorage $jobStorage
22-
* @param ProducerInterface $producer
23-
*/
2420
public function __construct(JobStorage $jobStorage, ProducerInterface $producer)
2521
{
2622
$this->jobStorage = $jobStorage;
@@ -74,7 +70,6 @@ public function findOrCreateRootJob($ownerId, $jobName, $unique = false)
7470

7571
/**
7672
* @param string $jobName
77-
* @param Job $rootJob
7873
*
7974
* @return Job
8075
*/
@@ -104,9 +99,6 @@ public function findOrCreateChildJob($jobName, Job $rootJob)
10499
return $job;
105100
}
106101

107-
/**
108-
* @param Job $job
109-
*/
110102
public function startChildJob(Job $job)
111103
{
112104
if ($job->isRoot()) {
@@ -116,11 +108,7 @@ public function startChildJob(Job $job)
116108
$job = $this->jobStorage->findJobById($job->getId());
117109

118110
if (Job::STATUS_NEW !== $job->getStatus()) {
119-
throw new \LogicException(sprintf(
120-
'Can start only new jobs: id: "%s", status: "%s"',
121-
$job->getId(),
122-
$job->getStatus()
123-
));
111+
throw new \LogicException(sprintf('Can start only new jobs: id: "%s", status: "%s"', $job->getId(), $job->getStatus()));
124112
}
125113

126114
$job->setStatus(Job::STATUS_RUNNING);
@@ -131,9 +119,6 @@ public function startChildJob(Job $job)
131119
$this->sendCalculateRootJobStatusEvent($job);
132120
}
133121

134-
/**
135-
* @param Job $job
136-
*/
137122
public function successChildJob(Job $job)
138123
{
139124
if ($job->isRoot()) {
@@ -143,11 +128,7 @@ public function successChildJob(Job $job)
143128
$job = $this->jobStorage->findJobById($job->getId());
144129

145130
if (Job::STATUS_RUNNING !== $job->getStatus()) {
146-
throw new \LogicException(sprintf(
147-
'Can success only running jobs. id: "%s", status: "%s"',
148-
$job->getId(),
149-
$job->getStatus()
150-
));
131+
throw new \LogicException(sprintf('Can success only running jobs. id: "%s", status: "%s"', $job->getId(), $job->getStatus()));
151132
}
152133

153134
$job->setStatus(Job::STATUS_SUCCESS);
@@ -158,9 +139,6 @@ public function successChildJob(Job $job)
158139
$this->sendCalculateRootJobStatusEvent($job);
159140
}
160141

161-
/**
162-
* @param Job $job
163-
*/
164142
public function failChildJob(Job $job)
165143
{
166144
if ($job->isRoot()) {
@@ -170,11 +148,7 @@ public function failChildJob(Job $job)
170148
$job = $this->jobStorage->findJobById($job->getId());
171149

172150
if (Job::STATUS_RUNNING !== $job->getStatus()) {
173-
throw new \LogicException(sprintf(
174-
'Can fail only running jobs. id: "%s", status: "%s"',
175-
$job->getId(),
176-
$job->getStatus()
177-
));
151+
throw new \LogicException(sprintf('Can fail only running jobs. id: "%s", status: "%s"', $job->getId(), $job->getStatus()));
178152
}
179153

180154
$job->setStatus(Job::STATUS_FAILED);
@@ -185,9 +159,6 @@ public function failChildJob(Job $job)
185159
$this->sendCalculateRootJobStatusEvent($job);
186160
}
187161

188-
/**
189-
* @param Job $job
190-
*/
191162
public function cancelChildJob(Job $job)
192163
{
193164
if ($job->isRoot()) {
@@ -197,11 +168,7 @@ public function cancelChildJob(Job $job)
197168
$job = $this->jobStorage->findJobById($job->getId());
198169

199170
if (!in_array($job->getStatus(), [Job::STATUS_NEW, Job::STATUS_RUNNING], true)) {
200-
throw new \LogicException(sprintf(
201-
'Can cancel only new or running jobs. id: "%s", status: "%s"',
202-
$job->getId(),
203-
$job->getStatus()
204-
));
171+
throw new \LogicException(sprintf('Can cancel only new or running jobs. id: "%s", status: "%s"', $job->getId(), $job->getStatus()));
205172
}
206173

207174
$job->setStatus(Job::STATUS_CANCELLED);
@@ -217,7 +184,6 @@ public function cancelChildJob(Job $job)
217184
}
218185

219186
/**
220-
* @param Job $job
221187
* @param bool $force
222188
*/
223189
public function interruptRootJob(Job $job, $force = false)
@@ -245,8 +211,6 @@ public function interruptRootJob(Job $job, $force = false)
245211

246212
/**
247213
* @see https://github.com/php-enqueue/enqueue-dev/pull/222#issuecomment-336102749 See for rationale
248-
*
249-
* @param Job $job
250214
*/
251215
protected function saveJob(Job $job)
252216
{
@@ -255,8 +219,6 @@ protected function saveJob(Job $job)
255219

256220
/**
257221
* @see https://github.com/php-enqueue/enqueue-dev/pull/222#issuecomment-336102749 See for rationale
258-
*
259-
* @param Job $job
260222
*/
261223
protected function sendCalculateRootJobStatusEvent(Job $job)
262224
{

pkg/job-queue/JobRunner.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class JobRunner
1515
private $rootJob;
1616

1717
/**
18-
* @param JobProcessor $jobProcessor
19-
* @param Job $rootJob
18+
* @param Job $rootJob
2019
*/
2120
public function __construct(JobProcessor $jobProcessor, Job $rootJob = null)
2221
{
@@ -25,9 +24,8 @@ public function __construct(JobProcessor $jobProcessor, Job $rootJob = null)
2524
}
2625

2726
/**
28-
* @param string $ownerId
29-
* @param string $name
30-
* @param callable $runCallback
27+
* @param string $ownerId
28+
* @param string $name
3129
*
3230
* @throws \Throwable|\Exception if $runCallback triggers an exception
3331
*
@@ -54,11 +52,7 @@ public function runUnique($ownerId, $name, callable $runCallback)
5452
try {
5553
$this->jobProcessor->failChildJob($childJob);
5654
} catch (\Throwable $t) {
57-
throw new OrphanJobException(sprintf(
58-
'Job cleanup failed. ID: "%s" Name: "%s"',
59-
$childJob->getId(),
60-
$childJob->getName()
61-
), 0, $e);
55+
throw new OrphanJobException(sprintf('Job cleanup failed. ID: "%s" Name: "%s"', $childJob->getId(), $childJob->getName()), 0, $e);
6256
}
6357

6458
throw $e;
@@ -74,8 +68,7 @@ public function runUnique($ownerId, $name, callable $runCallback)
7468
}
7569

7670
/**
77-
* @param string $name
78-
* @param callable $startCallback
71+
* @param string $name
7972
*
8073
* @return mixed
8174
*/
@@ -89,8 +82,7 @@ public function createDelayed($name, callable $startCallback)
8982
}
9083

9184
/**
92-
* @param string $jobId
93-
* @param callable $runCallback
85+
* @param string $jobId
9486
*
9587
* @return mixed
9688
*/

pkg/job-queue/Test/DbalPersistedConnection.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ protected function persistTransactionNestingLevel($level)
102102
static::$persistedTransactionNestingLevels[$this->getConnectionId()] = $level;
103103
}
104104

105-
/**
106-
* @param DriverConnection $connection
107-
*/
108105
protected function persistConnection(DriverConnection $connection)
109106
{
110107
static::$persistedConnections[$this->getConnectionId()] = $connection;

pkg/job-queue/Tests/Functional/app/AppKernel.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public function getLogDir()
3636
return sys_get_temp_dir().'/EnqueueJobQueue/cache/logs';
3737
}
3838

39-
/**
40-
* @param \Symfony\Component\Config\Loader\LoaderInterface $loader
41-
*/
4239
public function registerContainerConfiguration(LoaderInterface $loader)
4340
{
4441
$loader->load(__DIR__.'/config/config.yml');

0 commit comments

Comments
 (0)