grr/test_lib/worker_test_lib.py
def Next(self):
    """Very simple emulator of the worker.

    We wake each flow in turn and run it.

    Returns:
      total number of flows still alive.

    Raises:
      RuntimeError: if the flow terminates with an error.
    """
    with queue_manager.QueueManager(token=self.token) as manager:
      run_sessions = []
      for queue in self.queues:
        notifications_available = manager.GetNotificationsForAllShards(queue)
        # Run all the flows until they are finished

        # Only sample one session at the time to force serialization of flows
        # after each state run.
        for notification in notifications_available[:1]:
          session_id = notification.session_id
          manager.DeleteNotification(session_id, end=notification.timestamp)
          run_sessions.append(session_id)

          # Handle well known flows here.
          flow_name = session_id.FlowName()
          if flow_name in self.well_known_flows:
            well_known_flow = self.well_known_flows[flow_name]
            with well_known_flow:
              responses = well_known_flow.FetchAndRemoveRequestsAndResponses(
                  well_known_flow.well_known_session_id)
            well_known_flow.ProcessResponses(responses, self.pool)
            continue

          if (not data_store.RelationalDBReadEnabled("hunts") and
              data_store.RelationalDBFlowsEnabled()):
            # TODO(user): special-casing for tests with old hunts and
            # new flows. Using OpenWithLock here would lead to a deadlock,
            # as everything runs in a single thread. Call chain would involve
            # two consecutive OpenWithLock calls.
            flow_obj = aff4.FACTORY.Open(
                session_id,
                mode="rw",
                token=self.token,
            )
          else:
            flow_obj = aff4.FACTORY.OpenWithLock(
                session_id, token=self.token, blocking=False)

          with flow_obj:
            # Run it
            runner = flow_obj.GetRunner()
            cpu_used = runner.context.client_resources.cpu_usage
            user_cpu = self.cpu_user.next()
            system_cpu = self.cpu_system.next()
            network_bytes = self.network_bytes.next()
            cpu_used.user_cpu_time += user_cpu
            cpu_used.system_cpu_time += system_cpu
            runner.context.network_bytes_sent += network_bytes
            runner.ProcessCompletedRequests(notification, self.pool)

            if (self.check_flow_errors and
                isinstance(flow_obj, flow.GRRFlow) and
                runner.context.state == rdf_flow_runner.FlowContext.State.ERROR
               ):
              logging.exception("Flow terminated in state %s with an error: %s",
                                runner.context.current_state,
                                runner.context.backtrace)
              raise RuntimeError(runner.context.backtrace)

    return run_sessions
Very simple emulator of the worker.

    We wake each flow in turn and run it.

    Returns:
      total number of flows still alive.

    Raises:
      RuntimeError: if the flow terminates with an error.
    
Very simple emulator of the worker.
We wake each flow in turn and run it.
Very simple emulator of the worker . We wake each flow in turn and run it .
def Next(self):
    with queue_manager.QueueManager(token=self.token) as manager:
      run_sessions = []
      for queue in self.queues:
        notifications_available = manager.GetNotificationsForAllShards(queue)
        for notification in notifications_available[:1]:
          session_id = notification.session_id
          manager.DeleteNotification(session_id, end=notification.timestamp)
          run_sessions.append(session_id)
          flow_name = session_id.FlowName()
          if flow_name in self.well_known_flows:
            well_known_flow = self.well_known_flows[flow_name]
            with well_known_flow:
              responses = well_known_flow.FetchAndRemoveRequestsAndResponses(
                  well_known_flow.well_known_session_id)
            well_known_flow.ProcessResponses(responses, self.pool)
            continue
          if (not data_store.RelationalDBReadEnabled("hunts") and
              data_store.RelationalDBFlowsEnabled()):
            flow_obj = aff4.FACTORY.Open(
                session_id,
                mode="rw",
                token=self.token,
            )
          else:
            flow_obj = aff4.FACTORY.OpenWithLock(
                session_id, token=self.token, blocking=False)
          with flow_obj:
            runner = flow_obj.GetRunner()
            cpu_used = runner.context.client_resources.cpu_usage
            user_cpu = self.cpu_user.next()
            system_cpu = self.cpu_system.next()
            network_bytes = self.network_bytes.next()
            cpu_used.user_cpu_time += user_cpu
            cpu_used.system_cpu_time += system_cpu
            runner.context.network_bytes_sent += network_bytes
            runner.ProcessCompletedRequests(notification, self.pool)
            if (self.check_flow_errors and
                isinstance(flow_obj, flow.GRRFlow) and
                runner.context.state == rdf_flow_runner.FlowContext.State.ERROR
               ):
              logging.exception("Flow terminated in state %s with an error: %s",
                                runner.context.current_state,
                                runner.context.backtrace)
              raise RuntimeError(runner.context.backtrace)
    return run_sessions
def Next ( self ) : with queue_manager . QueueManager ( token = self . token ) as manager : run_sessions = [ ] for queue in self . queues : notifications_available = manager . GetNotificationsForAllShards ( queue ) for notification in notifications_available [ : 1 ] : session_id = notification . session_id manager . DeleteNotification ( session_id , end = notification . timestamp ) run_sessions . append ( session_id ) flow_name = session_id . FlowName ( ) if flow_name in self . well_known_flows : well_known_flow = self . well_known_flows [ flow_name ] with well_known_flow : responses = well_known_flow . FetchAndRemoveRequestsAndResponses ( well_known_flow . well_known_session_id ) well_known_flow . ProcessResponses ( responses , self . pool ) continue if ( not data_store . RelationalDBReadEnabled ( "hunts" ) and data_store . RelationalDBFlowsEnabled ( ) ) : flow_obj = aff4 . FACTORY . Open ( session_id , mode = "rw" , token = self . token , ) else : flow_obj = aff4 . FACTORY . OpenWithLock ( session_id , token = self . token , blocking = False ) with flow_obj : runner = flow_obj . GetRunner ( ) cpu_used = runner . context . client_resources . cpu_usage user_cpu = self . cpu_user . next ( ) system_cpu = self . cpu_system . next ( ) network_bytes = self . network_bytes . next ( ) cpu_used . user_cpu_time += user_cpu cpu_used . system_cpu_time += system_cpu runner . context . network_bytes_sent += network_bytes runner . ProcessCompletedRequests ( notification , self . pool ) if ( self . check_flow_errors and isinstance ( flow_obj , flow . GRRFlow ) and runner . context . state == rdf_flow_runner . FlowContext . State . ERROR ) : logging . exception ( "Flow terminated in state %s with an error: %s" , runner . context . current_state , runner . context . backtrace ) raise RuntimeError ( runner . context . backtrace ) return run_sessions
Very simple emulator of the worker.
Very simple emulator of the worker .
"""Very simple emulator of the worker.

    We wake each flow in turn and run it.

    Returns:
      total number of flows still alive.

    Raises:
      RuntimeError: if the flow terminates with an error.
    """ # Run all the flows until they are finished # Only sample one session at the time to force serialization of flows # after each state run. # Handle well known flows here. # TODO(user): special-casing for tests with old hunts and # new flows. Using OpenWithLock here would lead to a deadlock, # as everything runs in a single thread. Call chain would involve # two consecutive OpenWithLock calls. # Run it
total number of flows still alive.
total number of flows still alive .
if the flow terminates with an error.
if the flow terminates with an error .
RuntimeError
self