Is it possible to register a callback function to waitUntilDBInstanceAvailable()?

陌路散爱 提交于 2019-12-25 05:13:53

问题


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.


回答1:


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.



回答2:


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.



来源:https://stackoverflow.com/questions/18847639/is-it-possible-to-register-a-callback-function-to-waituntildbinstanceavailable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!