Skip to content
Snippets Groups Projects
Commit 5f214727 authored by Jared Hancock's avatar Jared Hancock
Browse files

Merge pull request #468 from protich/feature/mailfetch


Timeout after max timeout while fetching emails

Reviewed-By: default avatarJared Hancock <jared@osticket.com>
Reviewed-By: Alan Wilkes
parents 4a448b5b 5ec872a4
No related branches found
No related tags found
No related merge requests found
...@@ -643,24 +643,31 @@ class MailFetcher { ...@@ -643,24 +643,31 @@ class MailFetcher {
.' WHERE mail_active=1 ' .' WHERE mail_active=1 '
.' AND (mail_errors<='.$MAXERRORS.' OR (TIME_TO_SEC(TIMEDIFF(NOW(), mail_lasterror))>'.($TIMEOUT*60).') )' .' AND (mail_errors<='.$MAXERRORS.' OR (TIME_TO_SEC(TIMEDIFF(NOW(), mail_lasterror))>'.($TIMEOUT*60).') )'
.' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(), mail_lastfetch))>mail_fetchfreq*60)' .' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(), mail_lastfetch))>mail_fetchfreq*60)'
.' ORDER BY mail_lastfetch DESC' .' ORDER BY mail_lastfetch ASC';
.' LIMIT 10'; //Processing up to 10 emails at a time.
// echo $sql; if (!($res=db_query($sql)) || !db_num_rows($res))
if(!($res=db_query($sql)) || !db_num_rows($res))
return; /* Failed query (get's logged) or nothing to do... */ return; /* Failed query (get's logged) or nothing to do... */
//TODO: Lock the table here?? //Get max execution time so we can figure out how long we can fetch
// take fetching emails.
if (!($max_time = ini_get('max_execution_time')))
$max_time = 300;
//Start time
$start_time = Misc::micro_time();
while (list($emailId, $errors)=db_fetch_row($res)) {
//Break if we're 80% into max execution time
if ((Misc::micro_time()-$start_time) > ($max_time*0.80))
break;
while(list($emailId, $errors)=db_fetch_row($res)) {
$fetcher = new MailFetcher($emailId); $fetcher = new MailFetcher($emailId);
if($fetcher->connect()) { if ($fetcher->connect()) {
db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id='.db_input($emailId)); db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id='.db_input($emailId));
$fetcher->fetchEmails(); $fetcher->fetchEmails();
$fetcher->close(); $fetcher->close();
} else { } else {
db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id='.db_input($emailId)); db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id='.db_input($emailId));
if(++$errors>=$MAXERRORS) { if (++$errors>=$MAXERRORS) {
//We've reached the MAX consecutive errors...will attempt logins at delayed intervals //We've reached the MAX consecutive errors...will attempt logins at delayed intervals
$msg="\nosTicket is having trouble fetching emails from the following mail account: \n". $msg="\nosTicket is having trouble fetching emails from the following mail account: \n".
"\nUser: ".$fetcher->getUsername(). "\nUser: ".$fetcher->getUsername().
......
...@@ -26,6 +26,12 @@ header('Cache-Control: no-cache, must-revalidate'); ...@@ -26,6 +26,12 @@ header('Cache-Control: no-cache, must-revalidate');
header('Content-Length: '.strlen($data)); header('Content-Length: '.strlen($data));
header('Connection: Close'); header('Connection: Close');
print $data; print $data;
// Flush the request buffer
while(@ob_end_flush());
flush();
//Terminate the request
if (function_exists('fastcgi_finish_request'))
fastcgi_finish_request();
ob_start(); //Keep the image output clean. Hide our dirt. ob_start(); //Keep the image output clean. Hide our dirt.
//TODO: Make cron DB based to allow for better time limits. Direct calls for now sucks big time. //TODO: Make cron DB based to allow for better time limits. Direct calls for now sucks big time.
...@@ -37,7 +43,7 @@ if($sec>180 && $ost && !$ost->isUpgradePending()): //user can call cron once eve ...@@ -37,7 +43,7 @@ if($sec>180 && $ost && !$ost->isUpgradePending()): //user can call cron once eve
require_once(INCLUDE_DIR.'class.cron.php'); require_once(INCLUDE_DIR.'class.cron.php');
$thisstaff = null; //Clear staff obj to avoid false credit internal notes & auto-assignment $thisstaff = null; //Clear staff obj to avoid false credit internal notes & auto-assignment
Cron::TicketMonitor(); //Age tickets: We're going to age tickets regardless of cron settings. Cron::TicketMonitor(); //Age tickets: We're going to age tickets regardless of cron settings.
if($cfg && $cfg->isAutoCronEnabled()) { //ONLY fetch tickets if autocron is enabled! if($cfg && $cfg->isAutoCronEnabled()) { //ONLY fetch tickets if autocron is enabled!
Cron::MailFetcher(); //Fetch mail. Cron::MailFetcher(); //Fetch mail.
$ost->logDebug('Auto Cron', 'Mail fetcher cron call ['.$caller.']'); $ost->logDebug('Auto Cron', 'Mail fetcher cron call ['.$caller.']');
...@@ -45,6 +51,5 @@ if($cfg && $cfg->isAutoCronEnabled()) { //ONLY fetch tickets if autocron is enab ...@@ -45,6 +51,5 @@ if($cfg && $cfg->isAutoCronEnabled()) { //ONLY fetch tickets if autocron is enab
$_SESSION['lastcroncall']=time(); $_SESSION['lastcroncall']=time();
endif; endif;
$output = ob_get_contents();
ob_end_clean(); ob_end_clean();
?> ?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment