Changeset 14100

Show
Ignore:
Timestamp:
12/27/07 15:19:22 (8 months ago)
Author:
rambo
Message:

minor refactoring and bulletproofing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/MidCOM_2_8/midcom.helper.replicator/importer/archive.php

    r5666 r14100  
    202202            $files[] = $filepath; 
    203203        } 
     204        // readdir may return files in "funny" order (compared to ls etc) 
     205        sort($files); 
    204206    } 
    205207 
  • branches/MidCOM_2_8/midcom.helper.replicator/transporter/http.php

    r11102 r14100  
    1313class midcom_helper_replicator_transporter_http extends midcom_helper_replicator_transporter 
    1414{ 
    15  
     15    var $_client = false; 
    1616    var $url = false; 
    1717    var $_username = false; 
    1818    var $_password = false; 
    1919    var $use_force = false; 
    20  
    2120 
    2221    function midcom_helper_replicator_transporter_http($subscription) 
     
    8079    } 
    8180 
     81    function _post_item(&$key, &$items, $retry_count = 0) 
     82    { 
     83        $data =& $items[$key]; 
     84        $this->_client = new org_openpsa_httplib(); 
     85        $client =& $this->_client; 
     86        $client->basicauth['user'] = $this->_username; 
     87        $client->basicauth['password'] = $this->_password; 
     88        $post_vars = array 
     89        ( 
     90            'midcom_helper_replicator_import_xml' => &$data, 
     91            'midcom_helper_replicator_use_force' => (int)$this->use_force, 
     92        ); 
     93        $response = $client->post($this->url, $post_vars); 
     94        if (   $response === false 
     95            || stristr($response, 'error')) 
     96        { 
     97            if ($response) 
     98            { 
     99                $error_string = strip_tags(str_replace("\n", ' ', $response)); 
     100                $response_body = $response; 
     101            } 
     102            else 
     103            { 
     104                $error_string = $client->error; 
     105                $reponse_body = $client->_client->getResponseBody(); 
     106            } 
     107            if (   $error_string === 'Malformed response.' 
     108                && $retry_count < 5) 
     109            { 
     110                // Likely the remote end segfaulted, recursing to retry up-to 5 times 
     111                debug_push_class(__CLASS__, __FUNCTION__); 
     112                debug_add("Remote returned malformed response, most likely segfault, retry_count={$retry_count}", MIDCOM_LOG_INFO); 
     113                debug_pop(); 
     114                usleep(250000); // 0.25 second delay 
     115                if ($this->_post_item($key, $items, $retry_count+1)) 
     116                { 
     117                    debug_push_class(__CLASS__, __FUNCTION__); 
     118                    debug_add("Malformed response retry succeeded on count {$retry_count}", MIDCOM_LOG_INFO); 
     119                    debug_pop(); 
     120                    return true; 
     121                } 
     122            } 
     123            $msg = "Failed to send key {$key}, error: {$error_string}"; 
     124            debug_push_class(__CLASS__, __FUNCTION__); 
     125            debug_add($msg, MIDCOM_LOG_WARN); 
     126            debug_print_r('Response body: ', $response_body); 
     127            unset($response_body); 
     128            debug_pop(); 
     129            $GLOBALS['midcom_helper_replicator_logger']->log($msg, MIDCOM_LOG_WARN); 
     130            unset($msg); 
     131            return false; 
     132        } 
     133        return true; 
     134    } 
     135 
     136    function _real_process(&$items, $retry_count = 0) 
     137    { 
     138        foreach ($items as $key => $data) 
     139        { 
     140            if (!$this->_post_item(&$key, &$items)) 
     141            { 
     142                /** 
     143                 * PONDER: try next items in queue (some of them might depend on this) or just return ? 
     144                 * (NOTE: with true, because otherwise none of the previous items are removed from queue) 
     145                 */ 
     146                 continue; 
     147            } 
     148            $GLOBALS['midcom_helper_replicator_logger']->log("Succesfully sent key {$key}", MIDCOM_LOG_INFO); 
     149            unset($items[$key]); 
     150        } 
     151        unset($key, $data); 
     152         
     153        if (   !empty($items) 
     154            && $retry_count < 3) 
     155        { 
     156            /** 
     157             * Recursing retries  
     158             * 
     159             *  - There might be some dependencies that couldn't get queued in correct order for some reason 
     160             *  - There might have been some temporary error that _post_item would not catch correctly 
     161             */ 
     162            debug_push_class(__CLASS__, __FUNCTION__); 
     163            debug_add(sprintf('We still have %d items left, retrying (retry_count=%d)', count($items), $retry_count), MIDCOM_LOG_INFO); 
     164            debug_pop(); 
     165            usleep(1500000); // 1.5 second delay 
     166            return $this->_real_process($items, $retry_count+1); 
     167        } 
     168 
     169        return true; 
     170    } 
     171 
    82172    /** 
    83173     * Main entry point for processing the items received from queue manager 
     
    88178        // POST each item as midcom_helper_replicator_import_xml 
    89179        $GLOBALS['midcom_helper_replicator_logger']->log(sprintf('Sending %d keys to %s', count($items), $this->url), MIDCOM_LOG_INFO); 
    90         foreach ($items as $key => $data) 
    91         { 
    92             $client = new org_openpsa_httplib(); 
    93             $client->basicauth['user'] = $this->_username; 
    94             $client->basicauth['password'] = $this->_password; 
    95             $post_vars = array 
    96             ( 
    97                 'midcom_helper_replicator_import_xml' => &$data, 
    98                 'midcom_helper_replicator_use_force' => (int)$this->use_force, 
    99             ); 
    100             $response = $client->post($this->url, $post_vars); 
    101             if (   $response === false 
    102                 || stristr($response, 'error')) 
    103             { 
    104                 if ($response) 
    105                 { 
    106                     $error_string = strip_tags(str_replace("\n", ' ', $response)); 
    107                 } 
    108                 else 
    109                 { 
    110                     $error_string = $client->error; 
    111                 } 
    112                 $GLOBALS['midcom_helper_replicator_logger']->log("Failed to send key {$key}, error: {$error_string}", MIDCOM_LOG_WARN); 
    113                 /** 
    114                  * PONDER: try next items in queue (some of them might have depended on this 
    115                  * or just return (NOTE: with true, because otherwise previous items are not removed from queue) 
    116                  */ 
    117                 continue; 
    118             } 
    119             $GLOBALS['midcom_helper_replicator_logger']->log("Succesfully sent key {$key}", MIDCOM_LOG_INFO); 
    120             unset($items[$key]); 
    121         } 
    122         unset($key, $data); 
    123  
     180        $ret = $this->_real_process($items); 
    124181        $GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); 
    125         return true
     182        return $ret
    126183    } 
    127184