-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
ScheduleManagement.php
149 lines (125 loc) · 4.04 KB
/
ScheduleManagement.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace EthanYehuda\CronjobManager\Model;
use EthanYehuda\CronjobManager\Api\ScheduleManagementInterface;
use EthanYehuda\CronjobManager\Helper\Processor;
use EthanYehuda\CronjobManager\Api\ScheduleRepositoryInterface;
use Magento\Cron\Model\ConfigInterface;
use EthanYehuda\CronjobManager\Api\JobManagementInterface;
use Magento\Cron\Model\ScheduleFactory;
use Magento\Cron\Model\Schedule;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime\DateTime;
class ScheduleManagement implements ScheduleManagementInterface
{
/**
* @var Processor
*/
private $processor;
/**
* @var ScheduleRepositoryInterface
*/
private $scheduleRepository;
/**
* @var ConfigInterface
*/
private $config;
/**
* @var DateTime
*/
private $dateTime;
/**
* @var ScheduleFactory
*/
private $scheduleFactory;
public function __construct(
Processor $processor,
ScheduleRepositoryInterface $scheduleRepository,
ConfigInterface $config,
DateTime $dateTime,
ScheduleFactory $scheduleFactory
) {
$this->processor = $processor;
$this->scheduleRepository = $scheduleRepository;
$this->config = $config;
$this->dateTime = $dateTime;
$this->scheduleFactory = $scheduleFactory;
}
public function execute(int $scheduleId): bool
{
$groups = $this->listJobs();
$schedule = $this->scheduleRepository->get($scheduleId);
$jobCode = $schedule->getJobCode();
$groupId = $this->getGroupId($jobCode, $groups);
$jobConfig = $groups[$groupId][$jobCode];
$this->processor->runScheduledJob($jobConfig, $schedule);
$this->scheduleRepository->save($schedule);
return true;
}
public function listJobs(): array
{
return $this->config->getJobs();
}
public function createSchedule(string $jobCode, $time = null): Schedule
{
$time = strftime(ScheduleManagementInterface::TIME_FORMAT, $time ?? $this->dateTime->gmtTimestamp());
$schedule = $this->scheduleFactory->create()
->setJobCode($jobCode)
->setStatus(Schedule::STATUS_PENDING)
->setCreatedAt(
strftime(ScheduleManagementInterface::TIME_FORMAT, $this->dateTime->gmtTimestamp())
)->setScheduledAt($time);
$this->scheduleRepository->save($schedule);
return $schedule;
}
public function scheduleNow(string $jobCode): Schedule
{
return $this->createSchedule($jobCode);
}
public function schedule(string $jobCode, int $time): Schedule
{
return $this->createSchedule($jobCode, $time);
}
public function getGroupId(string $jobCode, $groups = null): string
{
if (is_null($groups)) {
$groups = $this->listJobs();
}
foreach ($groups as $groupId => $crons) {
if (isset($crons[$jobCode])) {
return $groupId;
}
}
throw new LocalizedException(__(
'No such job: %jobCode',
['jobCode' => $jobCode]
));
}
public function flush(): bool
{
$jobGroups = $this->listJobs();
foreach ($jobGroups as $groupId => $crons) {
$this->processor->cleanupJobs($groupId);
}
return true;
}
/**
* @param int $jobId
* @param int $timestamp
* @return bool
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function kill(int $jobId, int $timestamp): bool
{
$schedule = $this->scheduleRepository->get($jobId);
if ($schedule->getStatus() !== Schedule::STATUS_RUNNING) {
return false;
}
$schedule->setData(
'kill_request',
strftime(ScheduleManagementInterface::TIME_FORMAT, $this->dateTime->gmtTimestamp($timestamp))
);
$this->scheduleRepository->save($schedule);
return true;
}
}