The promise was rejected with reason: Invoking the wait callback did not resolve the promise

Viewed 1589

I'm trying to send bulk emails via Amazon SES, with their SDK. I can see they are using GuzzleHttp for promises.

Emails are being sent, and after a while (sometimes after 7 emails, sometimes after 10,000) I get an exception thrown from $promise->wait(); with the following error:

PHP Fatal error: Uncaught GuzzleHttp\Promise\RejectionException: The promise was rejected with reason: Invoking the wait callback did not resolve the promise in ...

Relevant Stack trace:

#0 /path/to/aws/GuzzleHttp/Promise/Promise.php(75): GuzzleHttp\Promise\exception_for()
#1 /path/to/myfile.php(725): GuzzleHttp\Promise\Promise->wait()

My code:

function sendMassEmails($subject, $message, $recipients)
{
    $SesClient = new SesClient([
        'profile' => 'default',
        'version' => '2010-12-01',
        'region' => 'eu-central-1'
    ]);

    $sender_email = 'MyCompany <noreply@mycompany.com>';
    $char_set = 'UTF-8';
    $commands = [];

    foreach ($recipients as $recipient) {
        $commands[] = $SesClient->getCommand('SendEmail', [
            'Destination' => [
                'ToAddresses' => [$recipient],
            ],
            'ReplyToAddresses' => [$sender_email],
            'Source' => $sender_email,
            'Message' => [
                'Body' => [
                    'Html' => [
                        'Charset' => $char_set,
                        'Data' => $message,
                    ],
                    'Text' => [
                        'Charset' => $char_set,
                        'Data' => convert_html_to_text($message),
                    ],
                ],
                'Subject' => [
                    'Charset' => $char_set,
                    'Data' => $subject,
                ],
            ],
            'ConfigurationSetName' => 'MyConfigurationSetName',
        ]);
    }

    $pool = new CommandPool($SesClient, $commands, [
        'concurrency' => 2,
        'before' => function (CommandInterface $cmd, $iteratorId) {
            $a = $cmd->toArray();
            echo sprintf('About to send %d: %s' . PHP_EOL, $iteratorId, $a['Destination']['ToAddresses'][0]);
        },
        'fulfilled' => function (ResultInterface $result, $iteratorId) use ($commands) {
            // log $commands[$iteratorId]['Destination']['ToAddresses'][0]
        },
        'rejected' => function (AwsException $reason, $iteratorId) use ($commands) {
            $logData = sprintf(
                '%s | Reason: %s' . PHP_EOL,
                $commands[$iteratorId]['Destination']['ToAddresses'][0],
                $reason
            );
            // log $logData
        },
    ]);

    $promise = $pool->promise();

    $promise->wait(); // <-- this line throws the exception

    $promise->then(function () {
        // Log 'done'
    });
}

I call the function like that:

sendMassEmails('Subject', 'Message', ['email1@example.com', 'email2@example.com', ...]);

Any idea why this is happening?

1 Answers

The wait() method means wait for the requests to complete, even if some of them fail. In the code I believe that you have extended EachPromise Class in CommandPool .
So you can try to increase the concurrency from 2 to 4, which means that instead of earlier 2, now 4 computations are happening at the same time that is good when we deal with a lot of request with the same time.

Related