-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add error resilience in uploadMediaChunked method #774
Comments
Thanks. I'll put this on the list. In the meantime you should wrap the media upload request in a catch and retry if it fails. |
It is happening for some image uploads constantly for me |
This is happening to me too, and I have worked it out easily by just retrying every upload until it works or a number of failed retries is reached, whatever happens first. And with a small time delay in-between, as @abzolv suggest. I have set up a simple convenience function for this: function retry($function, int $maxRetries = 5, int $delay = 500) {
$retries = 0;
$failed = false;
do {
$result = $function();
if ($result) {
return $result;
}
usleep($delay * 1000);
$failed = ++$retries >= $maxRetries;
} while (!$failed);
return false;
} And I am now wrapping every asset upload with it so it is automatically retried: $media_id_string = retry(function() use ($connection, $thumbnail) {
$result = $connection->upload('media/upload', [
'media' => $thumbnail,
]);
return empty($result->media_id_string) ? false : $result->media_id_string;
}); I still keep the error-checking logic in place, but now it only triggers an error when the upload fails for five times in a row in a 2.5 seconds time span, which has never happened to me yet: if (!$media_id_string) {
$error = sprintf('Error uploading %s to Twitter', $thumbnail);
trigger_error($error, E_USER_ERROR);
} Perhaps this issue could be addressed just by suggesting an approach like this one in the docs, leaving to the consumer the responsibility to retry failed API calls. |
this is still an issue. are you planning on fixing it? |
TwitterOAuth is in maintenance mode and major improvements are no longer planned. #1188 |
The last week or so, some upload requests generate a PHP Notice error on
$init->media_id_string
, saying media_id_string does not exist.That's either because the init request failed, or because of a Twitter bug that does not return media_id_string.
I'd suggest adding resilience by checking the init request response code and automatically retrying it a few times, with usleeps inbetween, if the response code is >= 500.
The text was updated successfully, but these errors were encountered: