我开始将我的代码迁移到boto 3,我注意到一个很好的补充是服务员。
我想从数据库实例创建一个快照,我想在恢复代码之前检查它的可用性。
我的方法如下:
# Notice: Step : Check snapshot availability [1st account - Oregon]
print "--- Check snapshot availability [1st account - Oregon] ---"
new_snap = client1.describe_db_snapshots(DBSnapshotIdentifier=new_snapshot_name)['DBSnapshots'][0]
# print pprint.pprint(new_snap) #debug
waiter = client1.get_waiter('db_snapshot_completed')
print "Manual snapshot is -pending-"
sleep(60)
waiter.wait(
DBSnapshotIdentifier = new_snapshot_name,
IncludeShared = True,
IncludePublic = False
)
print "OK. Manual snapshot is -available-"
,但文档说它每15秒轮询状态40次。那是10分钟。然而,一个相当大的数据库将需要更多。
我怎样才能使用服务员来减轻这种情况?
答案 0 :(得分:2)
服务员有配置参数'delay'和'max_attempts' 像这样:
waiter = rds_client.get_waiter('db_instance_available')
print( "waiter delay: " + str(waiter.config.delay) )
答案 1 :(得分:1)
如果你愿意,你可以在没有服务员的情况下完成。
来自该服务员的文件: 每15秒轮询一次RDS.Client.describe_db_snapshots(),直到达到成功状态。 40次检查失败后返回错误。
基本上这意味着它会执行以下操作:
RDS = boto3.client('rds')
RDS.describe_db_snapshots()
你可以运行它,但过滤到你的快照ID,这是语法。http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_snapshots
response = client.describe_db_snapshots(
DBInstanceIdentifier='string',
DBSnapshotIdentifier='string',
SnapshotType='string',
Filters=[
{
'Name': 'string',
'Values': [
'string',
]
},
],
MaxRecords=123,
Marker='string',
IncludeShared=True|False,
IncludePublic=True|False
)
这最终会看起来像这样:
snapshot_description = RDS.describe_db_snapshots(DBSnapshotIdentifier='YOURIDHERE')
然后你可以循环直到返回一个可用的快照。所以这是一个非常粗略的想法。
import boto3
import time
RDS = boto3.client('rds')
RDS.describe_db_snapshots()
snapshot_description = RDS.describe_db_snapshots(DBSnapshotIdentifier='YOURIDHERE')
while snapshot_description['DBSnapshots'][0]['Status'] != 'available' :
print("still waiting")
time.sleep(15)
snapshot_description = RDS.describe_db_snapshots(DBSnapshotIdentifier='YOURIDHERE')
答案 2 :(得分:0)
我认为另一个答案提到了这个解决方案,但这里明确表示。
[snip]
...
# Create your waiter
waiter_db_snapshot = client1.get_waiter('db_snapshot_completed')
# Increase the max number of tries as appropriate
waiter_db_snapshot.config.max_attempts = 120
# Add a 60 second delay between attempts
waiter_db_snapshot.config.delay = 60
print "Manual snapshot is -pending-"
....
[snip]