| | 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 | |
|---|
| 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); |
|---|