Notifis hunt about a given hunt-induced flow completion.

def ProcessHuntFlowDone(flow_obj, status_msg=None):
  """Notifis hunt about a given hunt-induced flow completion."""

  if not hunt.IsLegacyHunt(flow_obj.parent_hunt_id):
    hunt_obj = hunt.StopHuntIfCPUOrNetworkLimitsExceeded(
        flow_obj.parent_hunt_id)
    hunt.CompleteHuntIfExpirationTimeReached(hunt_obj)
    return

  hunt_urn = rdfvalue.RDFURN("hunts").Add(flow_obj.parent_hunt_id)
  client_urn = rdf_client.ClientURN(flow_obj.client_id)

  # Update the counter metrics separately from collections to minimize
  # contention.
  with aff4.FACTORY.Open(hunt_urn, mode="rw") as fd:
    # Legacy AFF4 code expects token to be set.
    fd.token = access_control.ACLToken(username=fd.creator)

    if flow_obj.num_replies_sent:
      fd.context.clients_with_results_count += 1

    fd.context.completed_clients_count += 1
    fd.context.results_count += flow_obj.num_replies_sent

    fd.GetRunner().SaveResourceUsage(flow_obj.client_id, status_msg)

  with aff4.FACTORY.Open(hunt_urn, mode="rw") as fd:
    # Legacy AFF4 code expects token to be set.
    fd.token = access_control.ACLToken(username=fd.creator)

    fd.RegisterCompletedClient(client_urn)
    if flow_obj.num_replies_sent:
      fd.RegisterClientWithResults(client_urn)

    fd.StopHuntIfAverageLimitsExceeded()