/
SchedulerService.php
129 lines (110 loc) · 3.38 KB
/
SchedulerService.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
<?php
/**
* @package Newscoop
* @copyright 2014 Sourcefabric z.ú.
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/
namespace Newscoop\Services;
use Doctrine\ORM\EntityManager;
use Newscoop\SchedulerServiceInterface;
use Newscoop\Entity\CronJob;
use Jobby\Jobby;
use Cron\CronExpression;
/**
* Scheduler Service to handle Jobby which is a PHP cron job manager
*/
class SchedulerService implements SchedulerServiceInterface
{
protected $em;
protected $config;
protected $jobby;
/**
* Construct
*/
public function __construct(EntityManager $em, $config = array())
{
$this->em = $em;
$this->config = $config;
$this->jobby = new Jobby();
}
/**
* {@inheritDoc}
*/
public function registerJob($jobName, array $config)
{
try {
foreach (array("command", "schedule") as $field) {
if (empty($config[$field])) {
throw new \Exception("'$field' is required for '$jobName' job");
}
}
$jobByCommand = $this->em->getRepository('Newscoop\Entity\CronJob')->findOneByCommand($config['command']);
if (!$jobByCommand) {
$cronJob = new CronJob();
foreach ($config as $key => $value) {
$setter = "set" . ucfirst($key);
$cronJob->{$setter}($value);
}
$cronJob->setName($jobName);
$this->em->persist($cronJob);
$this->em->flush($cronJob);
}
} catch (\Exception $e) {
throw new \Exception("Could not register job: '$jobName'");
}
}
/**
* {@inheritDoc}
*/
public function removeJob($jobName, array $config)
{
foreach (array("command", "schedule") as $field) {
if (empty($config[$field])) {
throw new \Exception("'$field' is required for '$jobName' job");
}
}
$job = $this->em->getRepository('Newscoop\Entity\CronJob')->findOneBy(array(
'command' => $config['command'],
'schedule' => $config['schedule'],
'name' => $jobName,
));
if ($job) {
$this->em->remove($job);
$this->em->flush();
}
}
/**
* {@inheritDoc}
*/
public function addSchedulerJob($jobName, array $config)
{
if (array_key_exists('environment', $this->config) && !empty($this->config['environment'])) {
$config['environment'] = $this->config['environment'];
}
$this->jobby->add($jobName, $config);
}
/**
* {@inheritDoc}
*/
public function run()
{
$this->jobby->run();
}
/**
* {@inheritDoc}
*/
public function getNextRunDate($schedule, $currentTime = 'now', $nth = 0, $allowCurrentDate = false)
{
$cron = CronExpression::factory($schedule);
return $cron->getNextRunDate($currentTime, $nth, $allowCurrentDate)->format('Y-m-d H:i:s');
}
/**
* {@inheritDoc}
*/
public function getPreviousRunDate($schedule, $currentTime = 'now', $nth = 0, $allowCurrentDate = false)
{
$cron = CronExpression::factory($schedule);
return $cron->getPreviousRunDate($currentTime, $nth, $allowCurrentDate)->format('Y-m-d H:i:s');
}
}