forked from tsolucio/corebos
/
vtigercron.php
94 lines (81 loc) · 3.01 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
<?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;
if (isset($_REQUEST['service']) or ($argc==2 and !empty($argv[1]))) {
// Run specific service
$srv = empty($argv[1]) ? $_REQUEST['service'] : $argv[1];
$srvcron = Vtiger_Cron::getInstance(vtlib_purify($srv));
if ($srvcron !== false) {
$cronTasks = array($srvcron);
} else {
echo "** Service $srv not found **";
die();
}
}
else {
// Run all service
$cronTasks = Vtiger_Cron::listAllActiveInstances();
}
foreach ($cronTasks as $cronTask) {
try {
$cronTask->setBulkMode(true);
// Not ready to run yet?
if (!$cronTask->isRunnable()) {
$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();
// Mark the status - finished
$cronTask->markFinished();
} 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');
global $HELPDESK_SUPPORT_EMAIL_ID,$HELPDESK_SUPPORT_NAME;
$from_name = $HELPDESK_SUPPORT_NAME;
$form_mail = $HELPDESK_SUPPORT_EMAIL_ID;
$mailsubject = "[ERROR]: ".$cronTask->getName()." - cron task execution throwed exception.";
$mailcontent = '<pre>'.$e.'</pre>';
send_mail('Emails',$mailto,$from_name,$form_mail,$mailsubject,$mailcontent);
}
}
}
}
else{
echo("Access denied!");
}
?>