|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jarobe\TaskRunner\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Jarobe\TaskRunner\Entity\TaskEvent; |
|
7
|
|
|
use Jarobe\TaskRunner\Hydrator\Reflector; |
|
8
|
|
|
use Jarobe\TaskRunner\Model\TaskResult; |
|
9
|
|
|
use Jarobe\TaskRunner\TaskType\TaskTypeInterface; |
|
10
|
|
|
|
|
11
|
|
|
class TaskEventManager |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var EntityManager |
|
15
|
|
|
*/ |
|
16
|
|
|
private $entityManager; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Reflector |
|
20
|
|
|
*/ |
|
21
|
|
|
private $reflector; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(EntityManager $entityManager, Reflector $reflector) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$this->entityManager = $entityManager; |
|
26
|
|
|
$this->reflector = $reflector; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param TaskTypeInterface $task |
|
31
|
|
|
* @return TaskEvent |
|
32
|
|
|
*/ |
|
33
|
|
|
public function createTaskEvent(TaskTypeInterface $task) |
|
34
|
|
|
{ |
|
35
|
|
|
$taskName = $this->reflector->getNameForClass($task); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$taskEvent = new TaskEvent(); |
|
38
|
|
|
$taskEvent->setTaskName($taskName) |
|
39
|
|
|
->setTargetTime($task->getTargetTime()) |
|
|
|
|
|
|
40
|
|
|
->setPayload($task->getPayload()) |
|
41
|
|
|
; |
|
42
|
|
|
|
|
43
|
|
|
$this->entityManager->persist($taskEvent); |
|
44
|
|
|
$this->entityManager->flush($taskEvent); |
|
45
|
|
|
return $taskEvent; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param TaskEvent $taskEvent |
|
50
|
|
|
* @return TaskEvent |
|
51
|
|
|
*/ |
|
52
|
|
|
public function initiateTaskEvent(TaskEvent $taskEvent) |
|
53
|
|
|
{ |
|
54
|
|
|
$now = new \DateTime('now'); |
|
55
|
|
|
$taskEvent->setInitiatedAt($now); |
|
56
|
|
|
|
|
57
|
|
|
$this->entityManager->flush($taskEvent); |
|
58
|
|
|
|
|
59
|
|
|
return $taskEvent; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param TaskEvent $taskEvent |
|
64
|
|
|
* @param TaskResult $result |
|
65
|
|
|
* @return TaskEvent |
|
66
|
|
|
*/ |
|
67
|
|
|
public function updateTaskEventWithResult(TaskEvent $taskEvent, TaskResult $result) |
|
68
|
|
|
{ |
|
69
|
|
|
$now = new \DateTime('now'); |
|
70
|
|
|
if ($result->isSuccess()) { |
|
71
|
|
|
$taskEvent->setCompletedAt($now) |
|
72
|
|
|
->setErrors(null) |
|
73
|
|
|
; |
|
74
|
|
|
} else { |
|
75
|
|
|
$taskEvent->setFailedAt($now) |
|
76
|
|
|
->setErrors($result->getErrors()) |
|
77
|
|
|
; |
|
78
|
|
|
} |
|
79
|
|
|
$this->entityManager->flush($taskEvent); |
|
80
|
|
|
return $taskEvent; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
The
EntityManagermight 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
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring 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.