-
Notifications
You must be signed in to change notification settings - Fork 33
/
CronJobsController.php
108 lines (95 loc) · 3.41 KB
/
CronJobsController.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
<?php
namespace Controllers\Cron;
use Controllers\BaseController;
use Utils\Lock\Lock;
use okapi\Facade;
class CronJobsController extends BaseController
{
private $jobToRun;
private $jobs = [];
public function __construct($jobToRun = null)
{
parent::__construct();
if ($jobToRun !== null && !$this->ocConfig->getCronjobSchedule($jobToRun)) {
die("unknown job: ".$jobToRun."\n");
}
$this->jobToRun = $jobToRun;
}
public function isCallableFromRouter($actionName)
{
// this controller is used by cron only - router shouldn't call it!
return false;
}
public function index()
{
$lockHandle = Lock::tryLock($this, Lock::EXCLUSIVE | Lock::NONBLOCKING);
if (!$lockHandle) {
$lastLockedRun = Facade::cache_get('ocpl/lastLockedCronRun');
$minutesSinceLastRun = (time() - strtotime($lastLockedRun)) / 60;
// We allow one run to take a maximum of 14 minutes, so that admins
// are not spammed with error messages if something is slow.
if ($minutesSinceLastRun > 14) {
die("Another instance of CronJobsController is running for ".$minutesSinceLastRun.
" minutes, or problem with lock file.\n"
);
}
} else {
Facade::cache_set('ocpl/lastLockedCronRun', date('Y-m-d H:i:s'), 7200);
}
Facade::disable_error_handling();
$this->prepareJobs();
// First run non-reentrant jobs in 'locked' mode, then reentrant jobs.
// See Jobs::isReentrant() for explanation of reentrant cronjobs.
if ($lockHandle) {
$this->runJobs(false);
Lock::unlock($lockHandle);
}
$this->runJobs(true);
}
private function prepareJobs()
{
foreach ($this->ocConfig->getCronjobSchedule() as $jobName => $schedule) {
if (!$this->jobToRun || $jobName == $this->jobToRun) {
$jobPath = __DIR__."/Jobs/".$jobName.".php";
if (!file_exists($jobPath)) {
echo "\nConfigured cronjob '".$jobName."' does not exist.\n";
} else {
require_once $jobPath;
$this->jobs[] = new $jobName($this->ocConfig);
}
}
}
}
private function runJobs($reentrant)
{
foreach ($this->jobs as $job) {
if ($reentrant == $job->isReentrant() && ($this->jobToRun || $job->isDue())) {
if ($result = $job->run()) {
echo $result . "\n";
} else {
$job->setLastRun();
}
}
}
}
public function getScheduleStatus()
{
$result = [];
foreach ($this->ocConfig->getCronjobSchedule() as $jobName => $schedule) {
$jobPath = __DIR__."/Jobs/".$jobName.".php";
if (!file_exists($jobPath)) {
$lastRun = '?';
} else {
require_once $jobPath;
$job = new $jobName($this->ocConfig);
$lastRun = $job->getLastRun();
}
$result[$jobName] = [
'shortName' => substr($jobName, 0, strlen($jobName) - 3),
'schedule' => $schedule,
'lastRun' => $lastRun // is null if not run yet
];
}
return $result;
}
}