Skip to content
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

Closed
abzolv opened this issue Apr 8, 2019 · 5 comments
Closed

Add error resilience in uploadMediaChunked method #774

abzolv opened this issue Apr 8, 2019 · 5 comments

Comments

@abzolv
Copy link

abzolv commented Apr 8, 2019

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.

@abraham
Copy link
Owner

abraham commented Apr 18, 2019

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.

@usamaejaz
Copy link

It is happening for some image uploads constantly for me

@JaimeObregon
Copy link

JaimeObregon commented Oct 16, 2020

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.

@ldunphyAK
Copy link

this is still an issue. are you planning on fixing it?

@abraham
Copy link
Owner

abraham commented Jul 24, 2023

TwitterOAuth is in maintenance mode and major improvements are no longer planned. #1188

@abraham abraham closed this as completed Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants