-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
CleanRunningJobs.php
78 lines (67 loc) · 2.12 KB
/
CleanRunningJobs.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
<?php
declare(strict_types=1);
namespace EthanYehuda\CronjobManager\Model;
use EthanYehuda\CronjobManager\Api\Data\ScheduleInterface;
use EthanYehuda\CronjobManager\Api\ScheduleRepositoryAdapterInterface;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory;
use Magento\Cron\Model\Schedule;
use Magento\Framework\Stdlib\DateTime\DateTime;
/**
* Update jobs with dead processes from running to error
*/
class CleanRunningJobs
{
/**
* @var ProcessManagement
*/
private $processManagement;
/**
* @var ScheduleRepositoryAdapterInterface
*/
private $scheduleRepository;
/**
* @var Clock
*/
private $clock;
/**
* @var DateTime
*/
private $dateTime;
public function __construct(
ScheduleRepositoryAdapterInterface $scheduleRepository,
ProcessManagement $processManagement,
DateTime $dateTime,
Clock $clock
) {
$this->processManagement = $processManagement;
$this->scheduleRepository = $scheduleRepository;
$this->dateTime = $dateTime;
$this->clock = $clock;
}
/**
* Find all jobs in status "running" (according to db),
* and check if the process is alive. If not, set status to error, with the message
* "Process went away"
*/
public function execute()
{
$runningJobs = $this->scheduleRepository->getByStatus(ScheduleInterface::STATUS_RUNNING);
foreach ($runningJobs as $schedule) {
if ($schedule->getHostname() !== \gethostname()) {
continue;
}
if ($this->processManagement->isPidAlive($schedule->getPid())) {
continue;
}
$messages = [];
if ($schedule->getMessages()) {
$messages[] = $schedule->getMessages();
}
$messages[] = __('Process went away at %1', $this->dateTime->gmtDate(null, $this->clock->now()));
$schedule
->setStatus(Schedule::STATUS_ERROR)
->setMessages(implode("\n", $messages));
$this->scheduleRepository->save($schedule);
}
}
}