forked from YetiForceCompany/YetiForceCRM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
135 lines (132 loc) · 6.54 KB
/
cron.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
<?php
/* +*******************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
* ****************************************************************************** */
// Start the cron services configured.
$init = microtime(true);
chdir(__DIR__);
require_once __DIR__ . '/include/main/WebUI.php';
try {
$checkLibrary = true;
require_once __DIR__ . '/include/RequirementsValidation.php';
} catch (\Throwable $e) {
file_put_contents(__DIR__ . '/cache/logs/cron_error.log', date('Y-m-d H:i:s', (int) $init) . ' - ' . $e->getMessage() . PHP_EOL, LOCK_EX);
throw $e;
}
\App\Process::$requestMode = 'Cron';
\App\Utils\ConfReport::$sapi = 'cron';
\App\Session::init();
\App\Session::set('last_activity', microtime(true));
$authenticatedUserId = \App\Session::get('authenticated_user_id');
$appUniqueKey = \App\Session::get('app_unique_key');
$user = (!empty($authenticatedUserId) && !empty($appUniqueKey) && $appUniqueKey === App\Config::main('application_unique_key'));
$response = '';
if (PHP_SAPI === 'cli' || $user || App\Config::main('application_unique_key') === \App\Request::_get('app_key')) {
$cronInstance = new \App\Cron();
$cronInstance->log('Cron start | SAPI: ' . PHP_SAPI . ', User: ' . Users::getActiveAdminId(), 'info', false);
$cronInstance::$cronTimeStart = microtime(true);
vtlib\Cron::setCronAction(true);
$cronTasks = false;
if (\App\Request::_has('service')) {
$cronTask = vtlib\Cron::getInstance(\App\Request::_get('service'));
if (!$cronTask) {
throw new \App\Exceptions\AppException('ERR_SERVICE_NOT_FOUND');
}
$cronTasks = [$cronTask];
} else {
$cronTasks = vtlib\Cron::listAllActiveInstances();
}
App\User::setCurrentUserId(Users::getActiveAdminId());
if ($user) {
$response .= '<pre>';
}
$response .= sprintf('--------------- %s (init: %s) | Start CRON ----------', date('Y-m-d H:i:s'), date('H:i:s', (int) $init)) . PHP_EOL;
foreach ($cronTasks as $cronTask) {
try {
$cronTask->setCronInstance($cronInstance);
$cronTask->refreshData();
$cronInstance->log('Task start: ' . $cronTask->getName(), 'info', false);
$startTaskTime = microtime(true);
\App\Log::trace($cronTask->getName() . ' - Start', 'Cron');
if ($cronInstance->checkCronTimeout()) {
$response .= sprintf("%s | %s - skipped \nCron execution time exceeded" . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName());
$cronInstance->log('Cron execution time exceeded');
break;
}
if ($cronTask->isDisabled()) {
$response .= sprintf('%s | %s - Cron task had been disabled' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName());
$cronInstance->log('Cron task had been disabled');
continue;
}
// Timeout could happen if intermediate cron-tasks fails
// and affect the next task. Which need to be handled in this cycle.
if ($cronTask->hadTimeout()) {
$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());
$cronInstance->log('Cron task had timedout as it was not completed last time it run');
if (App\Config::main('unblockedTimeoutCronTasks')) {
$cronTask->unlockTask();
}
}
// Not ready to run yet?
if ($cronTask->isRunning()) {
\App\Log::trace($cronTask->getName() . ' - Task omitted, it has not been finished during the last scanning', 'Cron');
$cronInstance->log('Task omitted, it has not been finished during the last scanning', 'warning');
$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());
continue;
}
// Not ready to run yet?
if (!$cronTask->isRunnable()) {
$cronInstance->log('Not ready to run as the time to run again is not completed');
\App\Log::trace($cronTask->getName() . ' - Not ready to run as the time to run again is not completed', 'Cron');
$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());
continue;
}
// Mark the status - running
$cronTask->markRunning();
$response .= sprintf('%s | %s - Start task' . PHP_EOL, date('Y-m-d H:i:s'), $cronTask->getName());
ob_start();
$className = $cronTask->getHandlerClass();
if (class_exists($className)) {
$cronHandler = new $className($cronTask);
if ($cronHandler instanceof \App\CronHandler) {
$cronHandler->process();
} else {
throw new \App\Exceptions\AppException('ERR_CLASS_MUST_BE||' . \App\CronHandler::class);
}
} else {
throw new \App\Exceptions\AppException('ERR_CLASS_NOT_FOUND||' . $className);
}
$taskResponse = ob_get_contents();
ob_end_clean();
$taskTime = round(microtime(true) - $startTaskTime, 2);
if ('' !== $taskResponse) {
$cronInstance->log('The task returned a message: ' . PHP_EOL . $taskResponse, 'error');
\App\Log::warning($cronTask->getName() . ' - The task returned a message:' . PHP_EOL . $taskResponse, 'Cron');
$response .= 'Task response:' . PHP_EOL . $taskResponse . PHP_EOL;
}
// Mark the status - finished
$cronTask->markFinished();
$response .= sprintf('%s | %s - End task (%s s) | %s', date('Y-m-d H:i:s'), $cronTask->getName(), $taskTime, $cronHandler->getTaskLog()) . PHP_EOL;
\App\Log::trace($cronTask->getName() . ' - End', 'Cron');
$cronInstance->log('End task, time: ' . $taskTime);
} catch (\Throwable $e) {
\App\Log::error("Cron task '{$cronTask->getName()}' throwed exception: " . PHP_EOL . $e->__toString() . PHP_EOL, 'Cron');
$cronInstance->log('Cron task execution throwed exception: ' . PHP_EOL . $response . PHP_EOL . $e->__toString(), 'error');
$cronTask->setError($response . PHP_EOL . $e->getMessage());
echo $response;
echo sprintf('%s | ERROR: %s - Cron task throwed exception.', date('Y-m-d H:i:s'), $cronTask->getName()) . PHP_EOL;
echo $e->__toString() . PHP_EOL;
if ('test' === App\Config::main('systemMode')) {
throw $e;
}
}
}
$cronInstance->log('End CRON (' . $cronInstance->getCronExecutionTime() . ')', 'info', false);
$response .= sprintf('=============== %s (Tasks: ' . $cronInstance->getCronExecutionTime() . ') (Script: %s) | End CRON ==========', date('Y-m-d H:i:s'), round(microtime(true) - $init, 2)) . PHP_EOL;
echo $response;
}