Question

I'm using the AWS SDK for PHP, and have a command-line tool waiting for a DB instance to be created with waitUntilDBInstanceAvailable():

$this->rdsClient->waitUntilDBInstanceAvailable([
    'DBInstanceIdentifier' => 'test'
]);

Is there a way to register a callback function, so that every time the SDK polls RDS, my callback function is called?

Something like:

$this->rdsClient->waitUntilDBInstanceAvailable([
    'DBInstanceIdentifier' => 'test',
    'CallbackFunction'     => function() {
        echo '.';
    }
]);

That would give the user some feedback about the fact that the script is still waiting, and did not hang arbitrarily.

The doc says:

The input array uses the parameters of the DescribeDBInstances operation and waiter specific settings

But I couldn't find out what these waiter specific settings are.

Was it helpful?

Solution

There is a page specifically about waiters in the AWS SDK for PHP User Guide. On that page it talks about how use event listeners with waiters. You need to interact directly with the waiter object.

// Get and configure the waiter object
$waiter = $client->getWaiter('BucketExists')
    ->setConfig(array('Bucket' => 'my-bucket'))
    ->setInterval(10)
    ->setMaxAttempts(3);

// Get the event dispatcher and register listeners for both events emitted by the waiter
$dispatcher = $waiter->getEventDispatcher();
$dispatcher->addListener('waiter.before_attempt', function () {
    echo "Checking if the wait condition has been met…\n";
});
$dispatcher->addListener('waiter.before_wait', function () use ($waiter) {
    $interval = $waiter->getInterval();
    echo "Sleeping for {$interval} seconds…\n";
});

$waiter->wait();

// Also Licensed under version 2.0 of the Apache License.

OTHER TIPS

You can do what you're looking for by implementing a custom waiter. Unfortunately, it's not as simple as supporting a callback on an existing waiter, but you can still implement what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top