Mantis#0008596 Expresso#6568
authoremersonfaria <emerson-faria.nobre@serpro.gov.br>
Thu, 8 Aug 2013 17:16:32 +0000 (14:16 -0300)
committerGerrit Code Review <syncroton@tine20-git.metaways.net>
Fri, 30 Aug 2013 10:00:42 +0000 (12:00 +0200)
Redundant Ping commands running for long time

Change-Id: Ia5b62591ed40afc40ea2019ed56a8d487a859854

docs/syncroton.sql
lib/Syncroton/Command/Ping.php
lib/Syncroton/Model/IDevice.php

index 98954a4..f5bae70 100644 (file)
@@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS `Syncroton_device` (
     `remotewipe` int(11) DEFAULT '0',
     `pingfolder` longblob,
     `lastsynccollection` longblob DEFAULT NULL,
+    `lastping` datetime DEFAULT NULL,
     'contactsfilter_id' varchar(40) DEFAULT NULL,
     'calendarfilter_id' varchar(40) DEFAULT NULL,
     'tasksfilter_id' varchar(40) DEFAULT NULL,
index dc06215..08ad41f 100644 (file)
@@ -78,10 +78,12 @@ class Syncroton_Command_Ping extends Syncroton_Command_Wbxml
                 }
                 $this->_device->pingfolder = serialize(array_keys($folders));
             }
+        }
 
-            if ($this->_device->isDirty() && $status == self::STATUS_NO_CHANGES_FOUND) {
-                $this->_device = $this->_deviceBackend->update($this->_device);
-            }
+        $this->_device->lastping = new DateTime('now', new DateTimeZone('utc'));
+
+        if ($status == self::STATUS_NO_CHANGES_FOUND) {
+            $this->_device = $this->_deviceBackend->update($this->_device);
         }
         
         $lifeTime = $this->_device->pinglifetime;
@@ -106,36 +108,48 @@ class Syncroton_Command_Ping extends Syncroton_Command_Wbxml
             do {
                 // take a break to save battery lifetime
                 sleep(Syncroton_Registry::getPingTimeout());
-                
+
+                // if another Ping command updated lastping property, we can stop processing this Ping command request
+                $device = $this->_deviceBackend->get($this->_device->id);
+                if ((isset($device->lastping) && $device->lastping instanceof DateTime) &&
+                    $device->pingfolder === $this->_device->pingfolder &&
+                    $device->lastping->getTimestamp() > $this->_device->lastping->getTimestamp() ) {
+                    break;
+                }
+
                 $now = new DateTime('now', new DateTimeZone('utc'));
                 
                 foreach ($folders as $folderId) {
                     try {
                         $folder         = $this->_folderBackend->get($folderId);
                         $dataController = Syncroton_Data_Factory::factory($folder->class, $this->_device, $this->_syncTimeStamp);
+                        
                     } catch (Syncroton_Exception_NotFound $e) {
                         if ($this->_logger instanceof Zend_Log)
                             $this->_logger->debug(__METHOD__ . '::' . __LINE__ . " " . $e->getMessage());
                         $status = self::STATUS_FOLDER_NOT_FOUND;
+                        
                         break;
+                        
                     } catch (Exception $e) {
                         if ($this->_logger instanceof Zend_Log)
                             $this->_logger->err(__METHOD__ . '::' . __LINE__ . " " . $e->getMessage());
+                        
                         // do nothing, maybe temporal issue, should we stop?
                         continue;
                     }
 
                     try {
                         $syncState = $this->_syncStateBackend->getSyncState($this->_device, $folder);
-                        \r
-                        // another process synchronized data of this folder already. let's skip it\r
-                        if ($syncState->lastsync > $this->_syncTimeStamp) {\r
+                        
+                        // another process synchronized data of this folder already. let's skip it
+                        if ($syncState->lastsync > $this->_syncTimeStamp) {
                             continue;
                         }
-                        \r
-                        // safe battery time by skipping folders which got synchronied less than Syncroton_Registry::getQuietTime() seconds ago\r
-                        if (($now->getTimestamp() - $syncState->lastsync->getTimestamp()) < Syncroton_Registry::getQuietTime()) {\r
-                            continue;\r
+                        
+                        // safe battery time by skipping folders which got synchronied less than Syncroton_Registry::getQuietTime() seconds ago
+                        if (($now->getTimestamp() - $syncState->lastsync->getTimestamp()) < Syncroton_Registry::getQuietTime()) {
+                            continue;
                         }
                         
                         $foundChanges = $dataController->hasChanges($this->_contentStateBackend, $folder, $syncState);
index 944a1f7..852e6e6 100644 (file)
@@ -37,6 +37,7 @@
  * @property    string   tasksfilter_id
  * @property    string   emailfilter_id
  * @property    string   lastsynccollection
+ * @property    DateTime lastping
  */
 interface Syncroton_Model_IDevice extends Syncroton_Model_IEntry
 {
@@ -46,6 +47,5 @@ interface Syncroton_Model_IDevice extends Syncroton_Model_IEntry
      * @return int/string
      */
     public function getMajorVersion();
-    
 }