Marks a flow as being processed on this worker and returns it.
196260
def LeaseFlowForProcessing(self,
                             client_id,
                             flow_id,
                             processing_time,
                             cursor=None):
    """"""
    query = ("SELECT " + self.FLOW_DB_FIELDS +
             "FROM flows WHERE client_id=%s AND flow_id=%s")
    cursor.execute(
        query,
        [db_utils.ClientIDToInt(client_id),
         db_utils.FlowIDToInt(flow_id)])
    response = cursor.fetchall()
    if not response:
      raise db.UnknownFlowError(client_id, flow_id)

    row, = response
    rdf_flow = self._FlowObjectFromRow(row)

    now = rdfvalue.RDFDatetime.Now()
    if rdf_flow.processing_on and rdf_flow.processing_deadline > now:
      raise ValueError("Flow %s on client %s is already being processed." %
                       (client_id, flow_id))

    if (rdf_flow.parent_hunt_id is not None and
        # TODO(user): remove the check for a legacy hunt prefix as soon as
        # AFF4 is gone.
        not rdf_flow.parent_hunt_id.startswith("H:")):

      query = "SELECT hunt_state FROM hunts WHERE hunt_id=%s"
      args = [db_utils.HuntIDToInt(rdf_flow.parent_hunt_id)]
      rows_found = cursor.execute(query, args)
      if rows_found == 1:
        hunt_state, = cursor.fetchone()
        if (hunt_state is not None and
            not rdf_hunt_objects.IsHuntSuitableForFlowProcessing(hunt_state)):
          raise db.ParentHuntIsNotRunningError(client_id, flow_id,
                                               rdf_flow.parent_hunt_id,
                                               hunt_state)

    update_query = ("UPDATE flows SET "
                    "processing_on=%s, "
                    "processing_since=FROM_UNIXTIME(%s), "
                    "processing_deadline=FROM_UNIXTIME(%s) "
                    "WHERE client_id=%s and flow_id=%s")
    processing_deadline = now + processing_time
    process_id_string = utils.ProcessIdString()

    args = [
        process_id_string,
        mysql_utils.RDFDatetimeToTimestamp(now),
        mysql_utils.RDFDatetimeToTimestamp(processing_deadline),
        db_utils.ClientIDToInt(client_id),
        db_utils.FlowIDToInt(flow_id)
    ]
    cursor.execute(update_query, args)

    # This needs to happen after we are sure that the write has succeeded.
    rdf_flow.processing_on = process_id_string
    rdf_flow.processing_since = now
    rdf_flow.processing_deadline = processing_deadline
    return rdf_flow
1337972
def _RegisterFlowProcessingHandler(self, handler):
    """"""
    self.flow_handler_stop = False
    self.flow_handler_thread = threading.Thread(
        name="flow_processing_handler",
        target=self._HandleFlowProcessingRequestLoop,
        args=(handler,))
    self.flow_handler_thread.daemon = True
    self.flow_handler_thread.start()
1093224
def RegisterFlowProcessingHandler(self, handler):
    """"""
    self.UnregisterFlowProcessingHandler()

    if handler:
      self.flow_processing_request_handler_stop = False
      self.flow_processing_request_handler_thread = threading.Thread(
          name="flow_processing_request_handler",
          target=self._FlowProcessingRequestHandlerLoop,
          args=(handler,))
      self.flow_processing_request_handler_thread.daemon = True
      self.flow_processing_request_handler_thread.start()
1317942
def _recv_flow_ok(self, method_frame):
        '''
        
        '''
        self.channel._active = method_frame.args.read_bit()
        if self._flow_control_cb is not None:
            self._flow_control_cb()
54054
def WaitUntilNoFlowsToProcess(self, timeout=None):
    
    t = self.flow_handler_thread
    if not t:
      return

    start_time = time.time()
    while True:
      with self.lock:
        # If the thread is dead, or there are no requests
        # to be processed/being processed, we stop waiting
        # and return from the function.
        if (not t.isAlive() or
            (not self._GetFlowRequestsReadyForProcessing() and
             not self.flow_handler_num_being_processed)):
          return

      time.sleep(0.2)

      if timeout and time.time() - start_time > timeout:
        raise TimeOutWhileWaitingForFlowsToBeProcessedError(
            "Flow processing didn't finish in time.")
1214559
def InitFromFlowObject(self, fo):
    """"""

    self.client_id = fo.client_id
    if fo.HasField("backtrace"):
      self.backtrace = fo.backtrace
    self.log_message = fo.error_message
    self.timestamp = fo.last_update_time

    return self
1234454
public void processDI() {

    if (processDefinitions.isEmpty()) {
      return;
    }

    if (!bpmnModel.getLocationMap().isEmpty()) {

      // Verify if all referenced elements exist
      for (String bpmnReference : bpmnModel.getLocationMap().keySet()) {
        if (bpmnModel.getFlowElement(bpmnReference) == null) {
          // ACT-1625: don't warn when artifacts are referenced from DI
          if (bpmnModel.getArtifact(bpmnReference) == null) {
            // Check if it's a Pool or Lane, then DI is ok
            if (bpmnModel.getPool(bpmnReference) == null && bpmnModel.getLane(bpmnReference) == null) {
              LOGGER.warn("Invalid reference in diagram interchange definition: could not find " + bpmnReference);
            }
          }
        } else if (!(bpmnModel.getFlowElement(bpmnReference) instanceof FlowNode)) {
          LOGGER.warn("Invalid reference in diagram interchange definition: " + bpmnReference + " does not reference a flow node");
        }
      }
      
      for (String bpmnReference : bpmnModel.getFlowLocationMap().keySet()) {
        if (bpmnModel.getFlowElement(bpmnReference) == null) {
          // ACT-1625: don't warn when artifacts are referenced from DI
          if (bpmnModel.getArtifact(bpmnReference) == null) {
            LOGGER.warn("Invalid reference in diagram interchange definition: could not find " + bpmnReference);
          }
        } else if (!(bpmnModel.getFlowElement(bpmnReference) instanceof SequenceFlow)) {
          LOGGER.warn("Invalid reference in diagram interchange definition: " + bpmnReference + " does not reference a sequence flow");
        }
      }

      for (Process process : bpmnModel.getProcesses()) {
        if (!process.isExecutable()) {
          continue;
        }

        // Parse diagram interchange information
        ProcessDefinitionEntity processDefinition = getProcessDefinition(process.getId());
        if (processDefinition != null) {
          processDefinition.setGraphicalNotationDefined(true);
          
          for (String edgeId : bpmnModel.getFlowLocationMap().keySet()) {
            if (bpmnModel.getFlowElement(edgeId) != null) {
              createBPMNEdge(edgeId, bpmnModel.getFlowLocationGraphicInfo(edgeId));
            }
          }
        }
      }
    }
  }
967269
private void dispatch(final ExecutionReference reference, final ExecutableFlow exflow,
      final Executor choosenExecutor) throws ExecutorManagerException {
    exflow.setUpdateTime(System.currentTimeMillis());

    this.executorLoader.assignExecutor(choosenExecutor.getId(),
        exflow.getExecutionId());
    try {
      this.apiGateway.callWithExecutable(exflow, choosenExecutor,
          ConnectorParams.EXECUTE_ACTION);
    } catch (final ExecutorManagerException ex) {
      logger.error("Rolling back executor assignment for execution id:"
          + exflow.getExecutionId(), ex);
      this.executorLoader.unassignExecutor(exflow.getExecutionId());
      throw new ExecutorManagerException(ex);
    }
    reference.setExecutor(choosenExecutor);

    // move from flow to running flows
    this.runningExecutions.get().put(exflow.getExecutionId(), new Pair<>(reference, exflow));
    synchronized (this.runningExecutions.get()) {
      // Wake up RunningExecutionsUpdaterThread from wait() so that it will immediately check status
      // from executor(s). Normally flows will run at least some time and can't be cleaned up
      // immediately, so there will be another wait round (or many, actually), but for unit tests
      // this is significant to let them run quickly.
      this.runningExecutions.get().notifyAll();
    }
    synchronized (this) {
      // wake up all internal waiting threads, too
      this.notifyAll();
    }

    logger.info(String.format(
        "Successfully dispatched exec %d with error count %d",
        exflow.getExecutionId(), reference.getNumErrors()));
  }
1235453
def handle_wf_finalization(self):
        
        if ((not self.current.flow_enabled or (
            self.current.task_type.startswith('End') and not self.are_we_in_subprocess())) and
                    'token' in self.current.output):
            del self.current.output['token']
758438
ClientStream returnStream() {
    synchronized (lock) {
      if (returnedStream == null) {
        // apply() has not been called, needs to buffer the requests.
        delayedStream = new DelayedStream();
        return returnedStream = delayedStream;
      } else {
        return returnedStream;
      }
    }
  }
1039963
def add_flow(self, flow):
        
        from .flows import Flow
        flow = Flow.as_flow(flow)

        if flow in self.flows:
            raise self.Error("Cannot add same flow twice!")

        if not flow.allocated:
            # Set the workdir of the flow here. Create a dir in self.workdir with name flow.name
            flow_workdir = os.path.join(self.workdir, os.path.basename(flow.name))
            if flow_workdir in (flow.workdir for flow in self.flows):
                raise self.Error("Two flows have the same name and hence the same workdir!")
            flow.allocate(workdir=flow_workdir)

        # Check if we are already using a scheduler to run this flow
        flow.check_pid_file()
        flow.set_spectator_mode(False)

        flow.check_status(show=False)

        #if flow.all_ok:
        #    print("flow.all_ok: Ignoring %s" % flow)
        #    return 0

        self.flows.append(flow)
        #print("Flow %s added to the BatchLauncher" % flow)

        return 1
948222
function flow (functions) {
  if (!functions.length) {
    return function (arg) { return arg }
  }

  return function () {
    var result = functions[0].apply(this, arguments)
    for (var i = 1; i < functions.length; i++) {
      result = functions[i](result)
    }
    return result
  }
}
565225
def consumer_process_task(processor, log_groups, check_point_tracker):
    
    try:
        check_point = processor.process(log_groups, check_point_tracker)
        check_point_tracker.flush_check()
    except Exception as e:
        return TaskResult(e)
    return ProcessTaskResult(check_point)
609432
function Flow(waits,    // @arg Integer: Number of wait for processes
              callback, // @arg Function/FunctionHash/Flow/FlowHash: callback and forks
                        //         callback on completion of processes.
                        //         callback(err:Error, args:MixArray)
              tag) {    // @arg String(= ""): tag name for Flow.dump()
                        // @desc: http://www.slideshare.net/uupaa/flowjs
    this._ = {
        missable: 0,        // Integer: Number of missable. default 0
        reason: "",         // String: exit or miss reason.
        waits:  waits,
        state:  PROGRESS,   // String: "progress", "done", "fail", "exit"
        name:   "",         // String: #fork(name)
        pass:   0,          // Integer: #pass() called
        miss:   0,          // Integer: #miss() called
        args:   [],         // MixArray: #pass(arg), #miss(arg) collections
        tag:    tag || "",
        fn:     callback
    };
    tag && (_progress[tag] = this); // register instance
    _updateState(this);
}
693389
def task_transaction(channel):
    """"""
    with channel.lock:
        if channel.poll(0):
            task = channel.recv()
            channel.send(Acknowledgement(os.getpid(), task.id))
        else:
            raise RuntimeError("Race condition between workers")

    return task
1018415
public FlowElement getCurrentFlowElement() {
    if (currentFlowElement == null) {
      String processDefinitionId = getProcessDefinitionId();
      if (processDefinitionId != null) {
        org.activiti.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
        currentFlowElement = process.getFlowElement(getCurrentActivityId(), true);
      }
    }
    return currentFlowElement;
  }
435837
def start_workflow(flow)
      @http.post(
        instance_worker_url,
        ATTR_PROG => PROG,
        ATTR_CANONICAL => canonical_name(flow),
        ATTR_NAME => class_name(flow),
        ATTR_DATA => @serializer.encode(@properties.from(flow)),
        ATTR_ID => parse_custom_id_from(flow)
      )
    end