Skip to content

Commit

Permalink
Added onResponseCode query listen method
Browse files Browse the repository at this point in the history
  • Loading branch information
vldiark committed Sep 16, 2022
1 parent 4c78af8 commit 781de1d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ $amo->queries->setDelay(0.5); // default: 0.15 sec
```
Пользовательская отладка запросов (обновлено с вводом oAuth)
```php
$amo->queries->onResponseCode(429, function(\Ufee\Amo\Base\Models\QueryModel $query) {
echo 'Resp code 429, retry '.$query->retries."\n";
});
$amo->queries->listen(function(\Ufee\Amo\Base\Models\QueryModel $query) {
$code = $query->response->getCode();
echo $query->startDate().' - ['.$query->method.'] '.$query->getUrl()."\n";
Expand Down
10 changes: 6 additions & 4 deletions src/Api/Oauth/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Query extends QueryModel
*/
public function execute()
{
$this->retries++;
$this->attributes['retries']++;
$instance = $this->instance();

$oauth = $instance->getOauth();
Expand Down Expand Up @@ -49,13 +49,15 @@ public function execute()
);
curl_close($this->curl);
$this->attributes['curl'] = null;
$code = $this->response->getCode();
$instance->queries->pushByCode($code, $this);

while ($this->response->getCode() == 429 && $this->retries <= 24) {
if ($code == 429 && $this->retries <= 7) {
// for limit requests
sleep(1);
return $this->setCurl()->execute();
}
if ($this->response->getCode() == 401 && $this->retry) {
if ($code == 401 && $this->retry) {
// multiserver refresh token fix
sleep(1);
$oauth = $instance->getOauth(true);
Expand All @@ -66,7 +68,7 @@ public function execute()
$this->setRetry(false);
return $this->execute();
}
if (in_array($this->response->getCode(), [502,504]) && $this->retry) {
if (in_array($code, [502,504]) && $this->retry) {
// for random api errors
sleep(1);
$this->setCurl();
Expand Down
16 changes: 9 additions & 7 deletions src/Api/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Query extends QueryModel
*/
public function execute()
{
$this->retries++;
$this->attributes['retries']++;
$instance = $this->instance();
$last_time = 1;
if ($last_query = $instance->queries->last()) {
Expand All @@ -34,21 +34,23 @@ public function execute()
);
curl_close($this->curl);
$this->attributes['curl'] = null;
$code = $this->response->getCode();
$instance->queries->pushByCode($code, $this);

if (!in_array($this->response->getCode(), [200, 204]) && file_exists($instance->queries->getCookiePath())) {
if (!in_array($code, [200, 204]) && file_exists($instance->queries->getCookiePath())) {
@unlink($instance->queries->getCookiePath());
}
while ($this->response->getCode() == 401 && $this->url != $instance::AUTH_URL && $this->retries <= 3 && $instance->hasAutoAuth()) {
if ($code == 401 && $this->url != $instance::AUTH_URL && $this->retries <= 3 && $instance->hasAutoAuth()) {
$instance->authorize();
$instance->queries->refreshSession();
$this->setCurl();
return $this->execute();
}
while ($this->response->getCode() == 429 && $this->retries <= 24) {
if ($code == 429 && $this->retries <= 24) {
sleep(1);
return $this->setCurl()->execute();
}
if (in_array($this->response->getCode(), [502,504]) && $this->retry) {
if (in_array($code, [502,504]) && $this->retry) {
sleep(1);
$this->setCurl();
$this->setRetry(false);
Expand All @@ -58,9 +60,9 @@ public function execute()
$this->attributes['execution_time'] = round($this->end_time - $this->start_time, 5);
$this->attributes['memory_usage'] = memory_get_peak_usage(true)/1024/1024;
$this->generateHash();
$instance->queries->pushQuery($this, in_array($this->response->getCode(), [200]));
$instance->queries->pushQuery($this, in_array($code, [200]));

if (!$instance->hasSession() && in_array($this->response->getCode(), [200, 204])) {
if (!$instance->hasSession() && in_array($code, [200, 204])) {
$instance->queries->refreshSession();
}
return $this;
Expand Down
5 changes: 3 additions & 2 deletions src/Base/Models/Oauth/QueryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class QueryModel extends \Ufee\Amo\Base\Models\QueryModel
'sleep_time',
'hash',
'memory_usage',
'headers'
'headers',
'retries'
],
$hidden = [
'account_id',
Expand All @@ -32,7 +33,6 @@ class QueryModel extends \Ufee\Amo\Base\Models\QueryModel
'response'
],
$attributes = [],
$retries = 0,
$cookie = [];

/**
Expand All @@ -51,6 +51,7 @@ public function __construct(Oauthapi &$instance, $service_class = '')
$this->attributes['account_id'] = $instance->getAuth('id');
$this->attributes['service'] = $service_class;
$this->attributes['retry'] = true;
$this->attributes['retries'] = 0;
$this->_boot();
}

Expand Down
5 changes: 3 additions & 2 deletions src/Base/Models/QueryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class QueryModel
'sleep_time',
'hash',
'memory_usage',
'headers'
'headers',
'retries'
],
$hidden = [
'account_id',
Expand All @@ -32,7 +33,6 @@ class QueryModel
'response'
],
$attributes = [],
$retries = 0,
$cookie = [];

/**
Expand All @@ -51,6 +51,7 @@ public function __construct(Amoapi &$instance, $service_class = '')
$this->attributes['account_id'] = $instance->getAuth('id');
$this->attributes['service'] = $service_class;
$this->attributes['retry'] = true;
$this->attributes['retries'] = 0;
$this->_boot();
}

Expand Down
27 changes: 27 additions & 0 deletions src/Collections/QueryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class QueryCollection extends \Ufee\Amo\Base\Collections\Collection
$refresh_time = 900,
$cookie_file,
$_listener,
$_listener_by_code = [],
$logger = null,
$_logs = false,
$_cache_initialized = false;
Expand Down Expand Up @@ -139,6 +140,20 @@ public function pushQuery(QueryModel $query, $save = true)
return $this;
}

/**
* Push queries by code
* @param integer $code - 200,403,429,500,502,504,..
* @param QueryModel $query
* @return QueryCollection
*/
public function pushByCode($code, QueryModel $query)
{
if (array_key_exists($code, $this->_listener_by_code) && is_callable($this->_listener_by_code[$code])) {
call_user_func($this->_listener_by_code[$code], $query);
}
return $this;
}

/**
* Initialize cache queries
* @return QueryCollection
Expand Down Expand Up @@ -219,6 +234,18 @@ public function listen(callable $callback)
$this->_listener = $callback;
return $this;
}

/**
* Debug queries by code
* @param integer $code - 200,403,429,500,502,504,..
* @param callable $callback
* @return QueryCollection
*/
public function onResponseCode($code, callable $callback)
{
$this->_listener_by_code[intval($code)] = $callback;
return $this;
}

/**
* Flush cache queries
Expand Down

0 comments on commit 781de1d

Please sign in to comment.