views:

27

answers:

1

I'm using Zend framework provided IMAP classes to access gmail messages over imap. I access the message headers of all the messages in the inbox one by one and index them locally. The script works fine for inboxes with messages less than 10000. For larger inboxes the script looses the connection, probably a timeout.

Here is the stack trace:

Exception Message: cannot read - connection closed?

trace:

#0 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(168): Zend_Mail_Protocol_Imap->_nextLine() #1 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(285): Zend_Mail_Protocol_Imap->_nextTaggedLine(NULL) #2 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(587): Zend_Mail_Protocol_Imap->readLine(NULL, 'TAG103') #3 /home/dev/trunk/Zend/Mail/Storage/Imap.php(353): Zend_Mail_Protocol_Imap->fetch('UID', 12267) #4 /home/dev/trunk/model/gmail_imap_oauth.class.php(121): Zend_Mail_Storage_Imap->getUniqueId(12267)

Is there a possible way to keep the connection alive for a longer duration ? I'm running this script via command line and tried increasing the script max runtime in php.ini, it didn't help.

A: 

Here the function

public function indexAllMails($startIndex=1) {

$this->_imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
$authenticateParams = array('XOAUTH', $initClientRequestEncoded);
$this->_imap->requestAndResponse('AUTHENTICATE', $authenticateParams);

//Create the mail storage Object
$this->_storage = new Zend_Mail_Storage_Imap_Wrapper($this->_imap);

//Select Folder
$this->_storage->selectFolder("[Gmail]/All Mail");


$numMessagesTotal = $this->_storage->countMessages();
if($numMessagesTotal == 0 ) return true;

for($i=$startIndex;$i<=$numMessagesTotal;$i++)
{
  try {
    $uniqueId = $this->_storage->getUniqueId($i);
    $message = $this->_storage->getMessage($i);
  }
  catch(Exception $ex)
  {
      log("Error getting Unique id",'index');
      log($ex->getMessage(),'index');
      log($ex->getTraceAsString(),'index');

      if($ex->getMessage() == 'cannot read - connection closed?')
      {
          //Timeout :(
          return true;
      }
      else
        continue;
  }

  $from = $message->from;
  echo $from;
}

}

Nands
This is a server side disconnect (ie, from gmail) and it happens in around 45 minutes after connection is made. I've tried via java and python libraries and they all behave the same so this isn't a client issue for sure. problem is that if i reconnect immediately (on getting disconnected) gmail disconnects me within 10 seconds again. If I try reconnecting again in lets say 10 minutes it works fine for next 45 minutes.
Nands
I'll have to figure out some way to speed up message fetching from gmail and finish within 45 minutes ! Looking for C libraries now
Nands