1
|
|
|
<?php |
2
|
|
|
/* +******************************************************************************* |
3
|
|
|
* The contents of this file are subject to the vtiger CRM Public License Version 1.0 |
4
|
|
|
* ("License"); You may not use this file except in compliance with the License |
5
|
|
|
* The Original Code is: vtiger CRM Open Source |
6
|
|
|
* The Initial Developer of the Original Code is vtiger. |
7
|
|
|
* Portions created by vtiger are Copyright (C) vtiger. |
8
|
|
|
* All Rights Reserved. |
9
|
|
|
* ****************************************************************************** */ |
10
|
|
|
|
11
|
|
|
// Start the cron services configured. |
12
|
|
|
$init = microtime(true); |
13
|
|
|
chdir(__DIR__); |
14
|
|
|
require_once __DIR__ . '/include/main/WebUI.php'; |
15
|
|
|
try { |
16
|
|
|
$checkLibrary = true; |
17
|
|
|
require_once __DIR__ . '/include/RequirementsValidation.php'; |
18
|
|
|
} catch (\Throwable $e) { |
19
|
|
|
file_put_contents(__DIR__ . '/cache/logs/cron_error.log', date('Y-m-d H:i:s', (int) $init) . ' - ' . $e->getMessage() . PHP_EOL, LOCK_EX); |
20
|
|
|
throw $e; |
21
|
|
|
} |
22
|
|
|
\App\Process::$requestMode = 'Cron'; |
23
|
|
|
\App\Utils\ConfReport::$sapi = 'cron'; |
24
|
|
|
\App\Session::init(); |
25
|
|
|
\App\Session::set('last_activity', microtime(true)); |
26
|
|
|
$authenticatedUserId = \App\Session::get('authenticated_user_id'); |
27
|
|
|
$appUniqueKey = \App\Session::get('app_unique_key'); |
28
|
|
|
$user = (!empty($authenticatedUserId) && !empty($appUniqueKey) && $appUniqueKey === App\Config::main('application_unique_key')); |
29
|
|
|
$response = ''; |
30
|
|
|
if (PHP_SAPI === 'cli' || $user || App\Config::main('application_unique_key') === \App\Request::_get('app_key')) { |
|
|
|
|
31
|
|
|
$cronInstance = new \App\Cron(); |
32
|
|
|
$cronInstance->log('SAPI: ' . PHP_SAPI . ', User: ' . Users::getActiveAdminId(), 'info', false); |
33
|
|
|
$cronTasks = false; |
34
|
|
|
$cronInstance->log('Cron start', 'info', false); |
35
|
|
|
$cronInstance::$cronTimeStart = microtime(true); |
|
|
|
|
36
|
|
|
vtlib\Cron::setCronAction(true); |
37
|
|
|
if (\App\Request::_has('service')) { |
|
|
|
|
38
|
|
|
$cronTask = vtlib\Cron::getInstance(\App\Request::_get('service')); |
39
|
|
|
if (!$cronTask) { |
|
|
|
|
40
|
|
|
throw new \App\Exceptions\AppException('ERR_SERVICE_NOT_FOUND'); |
41
|
|
|
} |
42
|
|
|
$cronTasks = [$cronTask]; |
43
|
|
|
} else { |
44
|
|
|
$cronTasks = vtlib\Cron::listAllActiveInstances(); |
45
|
|
|
} |
46
|
|
|
App\User::setCurrentUserId(Users::getActiveAdminId()); |
47
|
|
|
if ($user) { |
48
|
|
|
$response .= '<pre>'; |
49
|
|
|
} |
50
|
|
|
$response .= sprintf('--------------- %s (init: %s) | Start CRON ----------', date('Y-m-d H:i:s'), date('H:i:s', (int) $init)) . PHP_EOL; |
51
|
|
|
foreach ($cronTasks as $cronTask) { |
52
|
|
|
try { |
53
|
|
|
$cronTask->setCronInstance($cronInstance); |
54
|
|
|
$cronTask->refreshData(); |
55
|
|
|
$cronInstance->log('Task start: ' . $cronTask->getName(), 'info', false); |
56
|
|
|
$startTaskTime = microtime(true); |
57
|
|
|
\App\Log::trace($cronTask->getName() . ' - Start', 'Cron'); |
58
|
|
|
if ($cronInstance->checkCronTimeout()) { |
59
|
|
|
$response .= sprintf("%s | %s - skipped \nCron execution time exceeded" . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName()); |
60
|
|
|
$cronInstance->log('Cron execution time exceeded'); |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
if ($cronTask->isDisabled()) { |
64
|
|
|
$response .= sprintf('%s | %s - Cron task had been disabled' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName()); |
65
|
|
|
$cronInstance->log('Cron task had been disabled'); |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
// Timeout could happen if intermediate cron-tasks fails |
69
|
|
|
// and affect the next task. Which need to be handled in this cycle. |
70
|
|
|
if ($cronTask->hadTimeout()) { |
71
|
|
|
$response .= sprintf('%s | %s - Cron task had timedout as it was not completed last time it run' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName()); |
72
|
|
|
$cronInstance->log('Cron task had timedout as it was not completed last time it run'); |
73
|
|
|
if (App\Config::main('unblockedTimeoutCronTasks')) { |
74
|
|
|
$cronTask->unlockTask(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
// Not ready to run yet? |
78
|
|
|
if ($cronTask->isRunning()) { |
79
|
|
|
\App\Log::trace($cronTask->getName() . ' - Task omitted, it has not been finished during the last scanning', 'Cron'); |
80
|
|
|
$cronInstance->log('Task omitted, it has not been finished during the last scanning', 'warning'); |
81
|
|
|
$response .= sprintf('%s | %s - Task omitted, it has not been finished during the last scanning' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName()); |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
// Not ready to run yet? |
85
|
|
|
if (!$cronTask->isRunnable()) { |
86
|
|
|
$cronInstance->log('Not ready to run as the time to run again is not completed'); |
87
|
|
|
\App\Log::trace($cronTask->getName() . ' - Not ready to run as the time to run again is not completed', 'Cron'); |
88
|
|
|
$response .= sprintf('%s | %s - Not ready to run as the time to run again is not completed' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName()); |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
// Mark the status - running |
92
|
|
|
$cronTask->markRunning(); |
93
|
|
|
$response .= sprintf('%s | %s - Start task' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName()); |
94
|
|
|
|
95
|
|
|
ob_start(); |
96
|
|
|
$className = $cronTask->getHandlerClass(); |
97
|
|
|
if (class_exists($className)) { |
98
|
|
|
$cronHandler = new $className($cronTask); |
99
|
|
|
if ($cronHandler instanceof \App\CronHandler) { |
100
|
|
|
$cronHandler->process(); |
101
|
|
|
} else { |
102
|
|
|
throw new \App\Exceptions\AppException('ERR_CLASS_MUST_BE||' . \App\CronHandler::class); |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
throw new \App\Exceptions\AppException('ERR_CLASS_NOT_FOUND||' . $className); |
106
|
|
|
} |
107
|
|
|
$taskResponse = ob_get_contents(); |
108
|
|
|
ob_end_clean(); |
109
|
|
|
|
110
|
|
|
$taskTime = round(microtime(true) - $startTaskTime, 2); |
111
|
|
|
if ('' !== $taskResponse) { |
112
|
|
|
$cronInstance->log('The task returned a message: ' . PHP_EOL . $taskResponse, 'error'); |
113
|
|
|
\App\Log::warning($cronTask->getName() . ' - The task returned a message:' . PHP_EOL . $taskResponse, 'Cron'); |
114
|
|
|
$response .= 'Task response:' . PHP_EOL . $taskResponse . PHP_EOL; |
115
|
|
|
} |
116
|
|
|
// Mark the status - finished |
117
|
|
|
$cronTask->markFinished(); |
118
|
|
|
$response .= sprintf('%s | %s - End task (%s s) | %s', date('Y-m-d H:i:s'), $cronTask->getName(), $taskTime, $cronHandler->getTaskLog()) . PHP_EOL; |
119
|
|
|
\App\Log::trace($cronTask->getName() . ' - End', 'Cron'); |
120
|
|
|
$cronInstance->log('End task, time: ' . $taskTime); |
121
|
|
|
} catch (\Throwable $e) { |
122
|
|
|
\App\Log::error("Cron task '{$cronTask->getName()}' throwed exception: " . PHP_EOL . $e->__toString() . PHP_EOL, 'Cron'); |
123
|
|
|
$cronInstance->log('Cron task execution throwed exception: ' . PHP_EOL . $response . PHP_EOL . $e->__toString(), 'error'); |
124
|
|
|
$cronTask->setError($response . PHP_EOL . $e->getMessage()); |
125
|
|
|
echo $response; |
126
|
|
|
echo sprintf('%s | ERROR: %s - Cron task throwed exception.', date('Y-m-d H:i:s'), $cronTask->getName()) . PHP_EOL; |
127
|
|
|
echo $e->__toString() . PHP_EOL; |
128
|
|
|
if ('test' === App\Config::main('systemMode')) { |
129
|
|
|
throw $e; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
$cronInstance->log('End CRON (' . $cronInstance->getCronExecutionTime() . ')', 'info', false); |
134
|
|
|
$response .= sprintf('=============== %s (Tasks: ' . $cronInstance->getCronExecutionTime() . ') (Script: %s) | End CRON ==========', date('Y-m-d H:i:s'), round(microtime(true) - $init, 2)) . PHP_EOL; |
135
|
|
|
echo $response; |
136
|
|
|
} |
137
|
|
|
|