def GenerateOnAllChildSuccess(parentkey, initialvalue, combineresultf, failonerror=True):
def OnAllChildSuccess():
logdebug("Enter GenerateOnAllChildSuccess: %s" % parentkey)
parentfuture = parentkey.get() if parentkey else None
if parentfuture and not parentfuture.has_result():
if not parentfuture.initialised or not parentfuture.readyforresult:
raise Exception("Parent not initialised, retry")
@ndb.transactional()
def get_children_trans():
return get_children(parentfuture.key)
children = get_children_trans()
logdebug("children: %s" % [child.key for child in children])
if children:
result = initialvalue
error = None
finished = True
for childfuture in children:
logdebug("childfuture: %s" % childfuture.key)
if childfuture.has_result():
try:
childresult = childfuture.get_result()
logdebug("childresult(%s): %s" % (childfuture.status, childresult))
result = combineresultf(result, childresult)
logdebug("hasresult:%s" % result)
except Exception, ex:
logdebug("haserror:%s" % repr(ex))
error = ex
break
else:
logdebug("noresult")
finished = False
if error:
logwarning("Internal error, child has error in OnAllChildSuccess: %s" % error)
if failonerror:
parentfuture.set_failure(error)
else:
raise error
elif finished:
logdebug("result: %s" % result)
parentfuture.set_success(result)#(result, initialamount, keyrange))
else:
logdebug("child not finished in OnAllChildSuccess, skipping")
else:
logwarning("Internal error, parent has no children in OnAllChildSuccess")
parentfuture.set_failure(Exception("no children found"))
return OnAllChildSuccess
评论列表
文章目录