如何通过API检查交易是否完成?

时间:2016-05-05 12:24:30

标签: python ibm-cloud-infrastructure

当我执行某些操作(例如重新加载新系统)到虚拟服务器时,服务器将进入挂起状态,从门户网站显示“待处理事务”。在此状态下,服务器被禁止接收任何其他操作,否则将抛出异常。所以我需要检查事务的状态,我使用的是“wait_for_transaction”,它属于SoftLayer.managers.vs.VSManager(softlayer python包)。不幸的是,我遇到了一些奇怪的事情。

例如,我调用“upgrade”(SoftLayer.managers.vs.VSManager)来升级服务器的nic_speed,并立即调用“wait_for_transaction”。 “wait_for_transaction”返回“True”。实际上它应该返回“False”而不是“True”,因为服务器应该开始“升级”。几秒钟后,我再次打电话给“wait_for_transaction”,这次它返回“False”。

在我调用API“升级”后,看起来softlayer系统有延迟执行“升级”事务。那么如何检查这个交易的状态。如果将触发“待处理交易”的api确实有延迟,那么我调用它们之间的时间间隔是什么,并且交易是“真正”执行的。

1 个答案:

答案 0 :(得分:0)

延迟是由于您执行升级时创建订单并且此订单必须获得批准,一旦订单获得批准,交易就会启动。

当您请求升级时,此请求会立即存储在数据库中,您可以使用http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/getUpgradeRequest获取此数据 因此,您可以在代码中添加一个验证,该验证将检查是否存在尚未完成的升级请求,如果请求未完成且应该已启动,则您知道该事务将启动任何时候你都要等待它。

查看此代码

import SoftLayer
from pprint import pprint as pp
from datetime import datetime

client = SoftLayer.Client()
vsiId = 15003381
objectMask= "mask[completedFlag,maintenanceStartTimeUtc]"
upgrade = client['SoftLayer_Virtual_Guest'].getUpgradeRequest(id=vsiId, mask=objectMask)
if upgrade:
    if not upgrade['completedFlag']:
        # so there is upgrade request which has not been completed
        # you can see the maintenance Start Time of the upgrade request
        # like this:  print (upgrade['maintenanceStartTimeUtc'])
        # in case the upgrade request is not inmediatly you can perform
        # a verification in order to see if the request should have been started or not
        print ("there is a pending upgrade")
        activeTraqnsaction = []
        while (len(activeTraqnsaction) == 0):
            activeTraqnsaction = client['SoftLayer_Virtual_Guest'].getActiveTransactions(id=vsiId)
        print ('There is a transaction running')
        # here you can add your code to wait until the transaction is completed.
    else:
        print ("there is no pending upgrades")

此致