使用SoftLayer Ruby API时出现超时错误

时间:2015-12-07 22:16:26

标签: ruby ibm-cloud-infrastructure

我正在尝试使用下面的脚本获取简单的结算信息。脚本因超时错误而失败。有人可以帮我解决问题吗?

require 'rubygems'
require 'softlayer_api'
require 'pp'

billing = SoftLayer::Service.new("SoftLayer_Account",:username => "USER", :api_key => "KEY", :timeout => 99999)
object_mask = "mask[orderItem[order[userRecord[username]]], invoiceItem[totalRecurringAmount]]"
user_bill= billing.object_mask(object_mask).getNextInvoiceTopLevelBillingItems

pp user_bill

2 个答案:

答案 0 :(得分:1)

如果API对其他调用正常响应,则很可能是由请求的数据量引起的。当请求的信息超出其处理范围时,SLAPI通常会超时。

您可以通过使用结果限制或仅指定所需的特定属性来避免这种情况。

默认情况下引用关系属性时,将返回整个本地属性集。即使是通过一个房产到另一个房产。上述调用将返回整个结算项目集及其关联的本地属性,订单的所有本地属性(冗余地为每个订单项提取),以及整个发票项目与totalRecurringAmount。

通过在每个级别指定id,您可以减少返回的数据量:

mask[
  orderItem[
    id,
    order[
      id,
      userRecord[username]
    ]
  ], 
  invoiceItem[
    id,
    totalRecurringAmount
  ]
]

但是,在某些设备/产品中,呼叫将再次开始过载,并且需要对结果进行分页并批量处理。

renderingOrder

答案 1 :(得分:0)

您的帐户可能包含多个结算项目并导致异常,请查看以下文章:

http://sldn.softlayer.com/blog/phil/How-Solve-Error-fetching-http-headers

我可以建议使用resultLimits,为了避免异常,看看下面的Ruby脚本:

# Get Next Invoice Top Level Billing Items
#
# This script retrieves the billing items that will be on an account's next invoice
#
# Important manual pages:
# http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNextInvoiceTopLevelBillingItems
# http://sldn.softlayer.com/reference/datatypes/SoftLayer_Billing_Item
#
# @license <http://sldn.softlayer.com/article/License>
# @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
require 'rubygems' 
require 'softlayer_api' 
require 'pp'

# Helper function to fetch through all results from SoftLayer api
# using small page sizes and sleeping before every new page fetch.
def fetch_all(service, method)
  records = []; offset = 0; limit = 1000
  loop do
    results = service.result_limit(offset, limit).send(method)
    records += results
    break if results.size < limit
    offset += limit
    sleep 3
  end
  records
end

# Creating the account service.
billing = SoftLayer::Service.new("SoftLayer_Account", :username => "set me", :api_key => "set me", :timeout => 200)
# object mask 
object_mask = "mask[orderItem[order[userRecord[username]]], invoiceItem[totalRecurringAmount]]"
begin
  # Calling helper function to get all the result for getNextInvoiceTopLevelBillingItems method
  user_bill = fetch_all(billing.object_mask(object_mask), :getNextInvoiceTopLevelBillingItems) 
  # Print
  pp user_bill
  rescue StandardError => exception
    puts "Error: #{exception}"
end

我希望它有所帮助