-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
vtigercron.php
115 lines (110 loc) · 4.09 KB
/
vtigercron.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
<?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.
*/
chdir(__DIR__);
include_once 'vtlib/Vtiger/Cron.php';
require_once 'config.inc.php';
if (PHP_SAPI === 'cli' || PHP_SAPI === 'cgi-fcgi' || PHP_SAPI === 'apache2handler'
|| (isset($_SESSION['authenticated_user_id']) && isset($_SESSION['app_unique_key']) && $_SESSION['app_unique_key'] == $application_unique_key)
) {
$cronTasks = false;
$quiet = false;
if (isset($_REQUEST['service']) || ($argc==2 && !empty($argv[1]))) {
// Run specific service
$srv = empty($argv[1]) ? $_REQUEST['service'] : $argv[1];
$srv = vtlib_purify($srv);
if ($srv == '--quiet' || $srv == '-q') {
$cronTasks = Vtiger_Cron::listAllActiveInstances();
$quiet = true;
} else {
$srvcron = Vtiger_Cron::getInstance($srv);
if ($srvcron !== false) {
$cronTasks = array($srvcron);
} else {
echo "** Service $srv not found **";
die();
}
}
} else {
// Run all service
$cronTasks = Vtiger_Cron::listAllActiveInstances();
}
global $current_language;
if (empty($current_language)) {
$current_language = $default_language;
}
$app_strings = return_application_language($current_language);
foreach ($cronTasks as $cronTask) {
try {
$cronTask->setBulkMode(true);
// Not ready to run yet?
if (!$cronTask->isRunnable()) {
$run_task = false;
if ($cronTask->getName() == 'cronWatcherService' && $cronTask->isRunning()) {
$time = 10;
$last = $cronTask->getLastStart();
$now = time();
if ($now-$last > $time*60) {
$msg = sprintf("[INFO]: %s - cron task had timedout as it is not completed for 10 minutes - restarting\n", $cronTask->getName());
echo $msg;
$logbg->info($msg);
$run_task = true;
}
}
if (!$run_task) {
if (!$quiet) {
$msg = sprintf("[INFO]: %s - not ready to run as the time to run again is not completed\n", $cronTask->getName());
echo $msg;
$logbg->info($msg);
}
continue;
}
}
// Timeout could happen if intermediate cron-tasks fails
// and affect the next task. Which need to be handled in this cycle.
if ($cronTask->hadTimedout()) {
$msg = sprintf("[INFO]: %s - cron task had timedout as it is not completed last time it run- restarting\n", $cronTask->getName());
echo $msg;
$logbg->info($msg);
}
// Mark the status - running
$cronTask->markRunning();
checkFileAccess($cronTask->getHandlerFile());
$logbg->info('Execute: '.$cronTask->getHandlerFile());
require_once $cronTask->getHandlerFile();
$daily=$cronTask->getdaily();
$timestart=$cronTask->getLastStart();
// Mark the status - finished
$cronTask->markFinished($daily, $timestart);
} catch (Exception $e) {
$msg = sprintf("[ERROR]: %s - cron task execution throwed exception.\n", $cronTask->getName());
$msg .= $e->getMessage();
$msg .= "\n";
echo $msg;
$logbg->info($msg);
//Send email with error.
$mailto = GlobalVariable::getVariable('Debug_Send_VtigerCron_Error', '');
if ($mailto != '') {
require_once 'modules/Emails/mail.php';
require_once 'modules/Emails/Emails.php';
$HELPDESK_SUPPORT_EMAIL_ID = GlobalVariable::getVariable('HelpDesk_Support_EMail', 'support@your_support_domain.tld', 'HelpDesk');
$HELPDESK_SUPPORT_NAME = GlobalVariable::getVariable('HelpDesk_Support_Name', 'your-support name', 'HelpDesk');
$mailsubject = '[ERROR]: '.$cronTask->getName().' - cron task execution throwed exception.';
$mailcontent = '<pre>'.$e.'</pre>';
send_mail('Emails', $mailto, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $mailsubject, $mailcontent);
}
}
}
} else {
echo 'Access denied!';
}
?>