/
ProcessUpdater.php
112 lines (98 loc) · 3.5 KB
/
ProcessUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Concrete\Core\Command\Process;
use Concrete\Core\Application\Application;
use Concrete\Core\Command\Process\Event\ProcessEvent;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Entity\Command\Batch as BatchEntity;
use Concrete\Core\Entity\Command\Process;
use Concrete\Core\Entity\Command\TaskProcess;
use Concrete\Core\Events\EventDispatcher;
use Concrete\Core\Localization\Service\Date;
use Concrete\Core\Notification\Events\MercureService;
use Concrete\Core\Notification\Events\ServerEvent\ProcessClosedEvent;
use Concrete\Core\Notification\Mercure\Update\BatchUpdated;
use Concrete\Core\Notification\Events\ServerEvent\ProcessClosed;
use Doctrine\ORM\EntityManager;
class ProcessUpdater
{
/**
* @var Date
*/
protected $dateService;
/**
* @var Repository
*/
protected $config;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var MercureService
*/
protected $mercureService;
/**
* @var EventDispatcher
*/
protected $eventDispatcher;
public function __construct(
EntityManager $entityManager,
EventDispatcher $eventDispatcher,
Date $dateService,
Repository $config,
MercureService $mercureService
) {
$this->eventDispatcher = $eventDispatcher;
$this->entityManager = $entityManager;
$this->dateService = $dateService;
$this->config = $config;
$this->mercureService = $mercureService;
}
/**
* @param string|Process $process
*/
public function closeProcess($process, int $exitCode, string $exitMessage = null)
{
if (is_string($process)) {
$process = $this->entityManager->find(Process::class, $process);
}
$dateCompleted = $this->dateService->toDateTime()->getTimestamp();
$process->setDateCompleted($dateCompleted);
$process->setExitCode($exitCode);
$process->setExitMessage($exitMessage);
$this->entityManager->persist($process);
$this->entityManager->flush();
if ($this->mercureService->isEnabled()) {
usleep(500000); // some fast-running tasks cause race conditions.
$event = new ProcessClosedEvent($process->jsonSerialize(), $exitCode);
$hub = $this->mercureService->getHub();
$hub->publish($event->getUpdate());
}
$this->clearOldProcesses();
if ($process instanceof TaskProcess) {
$task = $process->getTask();
$task->setDateLastCompleted($dateCompleted);
$this->entityManager->persist($process);
$this->entityManager->flush();
}
$this->eventDispatcher->dispatch('on_close_process', new ProcessEvent($process));
}
protected function clearOldProcesses()
{
$threshold = (int) $this->config->get('concrete.processes.delete_threshold');
$now = new \DateTime();
$now->sub(new \DateInterval('P' . $threshold . 'D'));
$minTimestamp = $now->getTimestamp();
$query = $this->entityManager->createQuery(
"select p from \Concrete\Core\Entity\Command\Process p where p.dateCompleted < :minTimestamp and p.dateCompleted is not null"
);
$query->setParameter('minTimestamp', $minTimestamp);
$processes = $query->getResult();
if ($processes) {
foreach ($processes as $process) {
$this->entityManager->remove($process);
}
}
$this->entityManager->flush();
}
}