是否可以注册一个回调函数来等待直到DBInstanceAvailable()


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

我正在使用适用于 PHP 的 AWS 开发工具包,并且有一个命令行工具等待使用 waitUntilDBInstanceAvailable() 创建数据库实例:

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

有没有办法注册一个回调函数,这样每次 SDK 轮询 RDS 时,都会调用我的回调函数?

像这样:

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

这将为用户提供一些反馈,说明脚本仍在等待,并且没有任意挂起。

文档说:

输入数组使用描述DBInstances操作和服务员特定设置的参数

但是我找不到这些服务员特定的设置是什么。

适用于 PHP 的 AWS 开发工具包用户指南中有一个专门介绍服务员的页面。在该页面上,它讨论了如何将事件侦听器与服务员一起使用。您需要直接与服务员对象交互。

// 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.

您可以通过实现自定义服务员来执行所需的操作。不幸的是,这并不像支持现有服务员的回调那么简单,但您仍然可以实现您正在寻找的内容。