Esempio n. 1
0
 public function executeCommand(ProcessEngine $engine)
 {
     $name = $this->builder->getName();
     if ($this->builder->count() < 1) {
         throw new \RuntimeException(sprintf('Cannot deploy "%s" because it does not contain any resources', $name));
     }
     $id = UUID::createRandom();
     $sql = "\tINSERT INTO `#__bpmn_deployment`\n\t\t\t\t\t\t(`id`, `name`, `deployed_at`)\n\t\t\t\t\tVALUES\n\t\t\t\t\t\t(:id, :name, :time)\n\t\t";
     $stmt = $engine->prepareQuery($sql);
     $stmt->bindValue('id', $id);
     $stmt->bindValue('name', $name);
     $stmt->bindValue('time', time());
     $stmt->execute();
     $engine->info('Created deployment "{name}" with identifier <{id}>', ['name' => $name, 'id' => (string) $id]);
     $sql = "\tINSERT INTO `#__bpmn_resource`\n\t\t\t\t\t\t(`id`, `deployment_id`, `name`, `data`)\n\t\t\t\t\tVALUES\n\t\t\t\t\t\t(:id, :deployment, :name, :data)\n\t\t";
     $stmt = $engine->prepareQuery($sql);
     $stmt->bindValue('deployment', $id);
     $parser = new DiagramLoader();
     foreach ($this->builder as $name => $stream) {
         $in = $stream->getContents();
         $resourceId = UUID::createRandom();
         $stmt->bindValue('id', $resourceId);
         $stmt->bindValue('name', $name);
         $stmt->bindValue('data', new BinaryData($in));
         $stmt->execute();
         $engine->debug('Deployed resource "{name}" with identifer <{resourceId}>', ['name' => $name, 'resourceId' => (string) $resourceId]);
         if ($this->builder->isProcessResource($name)) {
             foreach ($parser->parseDiagramString($in) as $process) {
                 $engine->pushCommand(new DeployBusinessProcessCommand($process, $id, $resourceId));
             }
         }
     }
     return $id;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function executeJob(Job $job, VirtualExecution $execution, ProcessEngine $engine)
 {
     if ($execution->isTerminated()) {
         throw new \RuntimeException(sprintf('%s is terminated', $execution));
     }
     $data = (array) $job->getHandlerData();
     $command = $data[self::PARAM_COMMAND];
     if (!$command instanceof CommandInterface) {
         throw new \RuntimeException(sprintf('Expecting command, given %s', is_object($command) ? get_class($command) : gettype($command)));
     }
     // Move execution to start node if param is set.
     if (array_key_exists(self::PARAM_NODE_ID, $data)) {
         $execution->setNode($execution->getProcessModel()->findNode($data[self::PARAM_NODE_ID]));
         $engine->debug('Moved {execution} to node "{node}"', ['execution' => (string) $execution, 'node' => $execution->getNode()->getId()]);
     }
     $engine->debug('Executing async command {cmd} using {execution}', ['cmd' => get_class($command), 'execution' => (string) $execution]);
     $engine->pushCommand($command);
 }
 /**
  * {@inheritdoc}
  */
 public function executeCommand(ProcessEngine $engine)
 {
     $sql = "\tSELECT s.`id`, s.`execution_id`, s.`activity_id`, s.`node`\r\n\t\t\t\t\tFROM `#__bpmn_event_subscription` AS s\r\n\t\t\t\t\tINNER JOIN `#__bpmn_execution` AS e ON (e.`id` = s.`execution_id`)\r\n\t\t\t\t\tWHERE s.`name` = :signal\r\n\t\t\t\t\tAND s.`flags` = :flags\r\n\t\t\t\t\tORDER BY e.`depth` DESC\r\n\t\t";
     if ($this->executionId !== NULL) {
         $sql .= ' AND s.`execution_id` = :eid';
     }
     $stmt = $engine->prepareQuery($sql);
     $stmt->bindValue('signal', $this->signal);
     $stmt->bindValue('flags', EventSubscription::TYPE_SIGNAL);
     if ($this->executionId !== NULL) {
         $stmt->bindValue('eid', $this->executionId);
     }
     $stmt->transform('execution_id', new UUIDTransformer());
     $stmt->execute();
     $ids = [];
     $executions = [];
     $delegations = [];
     foreach ($stmt->fetchRows() as $row) {
         $execution = $executions[] = $engine->findExecution($row['execution_id']);
         $ids[(string) $execution->getId()] = [$execution->getId(), $row['activity_id']];
         if ($row['node'] !== NULL) {
             $delegations[(string) $execution->getId()] = ['nodeId' => $row['node']];
         }
     }
     if (!empty($ids)) {
         $sql = "SELECT `job_id` FROM `#__bpmn_event_subscription` WHERE `flags` = :flags AND `job_id` IS NOT NULL AND (";
         $where = [];
         $params = ['flags' => EventSubscription::TYPE_TIMER];
         foreach (array_values($ids) as $i => $tmp) {
             $where[] = sprintf("(`execution_id` = :e%u AND `activity_id` = :a%u)", $i, $i);
             $params['e' . $i] = $tmp[0];
             $params['a' . $i] = $tmp[1];
         }
         $stmt = $engine->prepareQuery($sql . implode(' OR ', $where) . ')');
         $stmt->bindAll($params);
         $stmt->transform('job_id', new UUIDTransformer());
         $stmt->execute();
         $management = $engine->getManagementService();
         foreach ($stmt->fetchColumns('job_id') as $jobId) {
             $management->removeJob($jobId);
         }
         unset($params['flags']);
         $stmt = $engine->prepareQuery("DELETE FROM `#__bpmn_event_subscription` WHERE " . implode(' OR ', $where));
         $stmt->bindAll($params);
         $count = $stmt->execute();
         $message = sprintf('Cleared {count} event subscription%s related to signal <{signal}>', $count == 1 ? '' : 's');
         $engine->debug($message, ['count' => $count, 'signal' => $this->signal === NULL ? 'NULL' : $this->signal]);
     }
     $vars = unserialize($this->variables);
     foreach ($executions as $execution) {
         $id = (string) $execution->getId();
         $execution->signal($this->signal, $vars, empty($delegations[$id]) ? [] : $delegations[$id]);
     }
     // Include signal start events subscriptions.
     $sql = "\tSELECT s.`name` AS signal_name, d.* \r\n\t\t\t\t\tFROM `#__bpmn_process_subscription` AS s\r\n\t\t\t\t\tINNER JOIN `#__bpmn_process_definition` AS d ON (d.`id` = s.`definition_id`)\r\n\t\t\t\t\tWHERE s.`flags` = :flags\r\n\t\t\t\t\tAND s.`name` = :name\r\n\t\t";
     $stmt = $engine->prepareQuery($sql);
     $stmt->bindValue('flags', EventSubscription::TYPE_SIGNAL);
     $stmt->bindValue('name', $this->signal);
     $stmt->transform('id', new UUIDTransformer());
     $stmt->transform('deployment_id', new UUIDTransformer());
     $stmt->execute();
     $source = $this->sourceExecutionId === NULL ? NULL : $engine->findExecution($this->sourceExecutionId);
     while ($row = $stmt->fetchNextRow()) {
         $definition = new ProcessDefinition($row['id'], $row['process_key'], $row['revision'], unserialize(BinaryData::decode($row['definition'])), $row['name'], new \DateTimeImmutable('@' . $row['deployed_at']), $row['deployment_id']);
         $engine->pushCommand(new StartProcessInstanceCommand($definition, $definition->findSignalStartEvent($row['signal_name']), $source === NULL ? NULL : $source->getBusinessKey(), $vars));
     }
     if ($source !== NULL) {
         $source->signal();
     }
 }