/** * @inheritdoc */ public function dispatch($message, $sender = null) { if ($message instanceof RemoteMessage) { $message = $this->getToProcessingMessageTranslator()->translateToProcessingMessage($message); } if (!$message instanceof ProcessingMessage) { throw new \InvalidArgumentException(sprintf('Message can not be dispatched. Unknown type provided: %s', is_object($message) ? get_class($message) : gettype($message))); } $channelGetter = null; if (MessageNameUtils::isProcessingCommand($message->messageName())) { $channelGetter = "getCommandChannelFor"; } if (MessageNameUtils::isProcessingEvent($message->messageName())) { $channelGetter = "getEventChannelFor"; } if (MessageNameUtils::isProcessingLogMessage($message->messageName())) { $channelGetter = "getEventChannelFor"; } if (StartSubProcess::MSG_NAME === $message->messageName()) { $channelGetter = "getCommandChannelFor"; } if (SubProcessFinished::MSG_NAME === $message->messageName()) { $channelGetter = "getEventChannelFor"; } if (is_null($channelGetter)) { throw new \InvalidArgumentException(sprintf('Channel detection for message %s was not possible', $message->messageName())); } /** @var $channelBus CommandBus|EventBus */ $channelBus = $this->{$channelGetter}($message->target(), $message->origin(), $sender); $channelBus->dispatch($message); }
/** * @throws \RuntimeException * @return RemoteMessage */ public function toServiceBusMessage() { $messageType = null; if (MessageNameUtils::isProcessingCommand($this->messageName())) { $messageType = MessageHeader::TYPE_COMMAND; } else { if (MessageNameUtils::isProcessingEvent($this->messageName())) { $messageType = MessageHeader::TYPE_EVENT; } else { throw new \RuntimeException(sprintf('Processing message %s can not be converted to service bus message. Type of the message could not be detected', $this->messageName())); } } $messageHeader = new MessageHeader($this->uuid(), $this->createdAt(), $this->version(), $messageType); $msgPayload = array('json' => json_encode($this->payload()), 'metadata' => $this->metadata, 'origin' => $this->origin(), 'target' => $this->target()); if ($this->processTaskListPosition()) { $msgPayload['processTaskListPosition'] = $this->processTaskListPosition()->toString(); } return new RemoteMessage($this->messageName(), $messageHeader, $msgPayload); }
/** * Start or continue the process with the help of given WorkflowEngine and optionally with given WorkflowMessage * * @param WorkflowEngine $workflowEngine * @param WorkflowMessage $workflowMessage * @throws \RuntimeException * @throws \BadMethodCallException * @return void */ public function perform(WorkflowEngine $workflowEngine, WorkflowMessage $workflowMessage = null) { $taskListEntry = $this->taskList->getNextNotStartedTaskListEntry(); if (is_null($taskListEntry)) { throw new \RuntimeException('ForEachProcess::perform was called but there are no tasks configured!'); } if (is_null($workflowMessage)) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->receiveMessage(LogMessage::logNoMessageReceivedFor($taskListEntry->task(), $taskListEntry->taskListPosition()), $workflowEngine); return; } $workflowMessage = $workflowMessage->reconnectToProcessTask($taskListEntry->taskListPosition()); if (count($this->taskList->getAllTaskListEntries()) > 1) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->receiveMessage(LogMessage::logErrorMsg('The ForEachProcess can only handle a single RunSubProcess task but there are more tasks configured', $workflowMessage), $workflowEngine); return; } if (!$taskListEntry->task() instanceof RunSubProcess) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->receiveMessage(LogMessage::logErrorMsg(sprintf('The ForEachProcess can only handle a RunSubProcess task but there is a %s task configured', get_class($taskListEntry->task())), $workflowMessage), $workflowEngine); return; } if (!MessageNameUtils::isProcessingEvent($workflowMessage->messageName())) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->receiveMessage(LogMessage::logWrongMessageReceivedFor($taskListEntry->task(), $taskListEntry->taskListPosition(), $workflowMessage), $workflowEngine); return; } $collection = $workflowMessage->payload()->toType(); if (!$collection instanceof CollectionType) { $this->receiveMessage(LogMessage::logErrorMsg(sprintf('The ForEachProcess requires a Prooph\\ProcessingType\\CollectionType as payload of the incoming message, but it is a %s type given', $workflowMessage->payload()->getTypeClass()), $workflowMessage), $workflowEngine); return; } $this->startSubProcessForEachItem($collection, $workflowEngine); }
/** * @param ManipulatePayload $task * @param TaskListPosition $taskListPosition * @param WorkflowEngine $workflowEngine * @param WorkflowMessage $previousMessage */ protected function performManipulatePayload(ManipulatePayload $task, TaskListPosition $taskListPosition, WorkflowEngine $workflowEngine, WorkflowMessage $previousMessage) { if (!MessageNameUtils::isProcessingEvent($previousMessage->messageName())) { $this->receiveMessage(LogMessage::logWrongMessageReceivedFor($task, $taskListPosition, $previousMessage), $workflowEngine); return; } $payload = $previousMessage->payload(); try { $task->performManipulationOn($payload); } catch (\Exception $ex) { $this->receiveMessage(LogMessage::logException($ex, $taskListPosition), $workflowEngine); return; } $newEvent = $previousMessage->prepareDataProcessing($taskListPosition, $this->taskList->taskListId()->nodeName())->answerWithDataProcessingCompleted(); $this->receiveMessage($newEvent, $workflowEngine); }
/** * Start or continue the process with the help of given WorkflowEngine and optionally with given WorkflowMessage * * @param WorkflowEngine $workflowEngine * @param WorkflowMessage $workflowMessage * @throws \RuntimeException * @return void */ public function perform(WorkflowEngine $workflowEngine, WorkflowMessage $workflowMessage = null) { $taskListEntry = $this->taskList->getNextNotStartedTaskListEntry(); if (is_null($taskListEntry)) { throw new \RuntimeException('ChunkProcess::perform was called but there are no tasks configured!'); } if (is_null($workflowMessage)) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->receiveMessage(LogMessage::logNoMessageReceivedFor($taskListEntry->task(), $taskListEntry->taskListPosition()), $workflowEngine); return; } $workflowMessage = $workflowMessage->reconnectToProcessTask($taskListEntry->taskListPosition()); if (count($this->taskList->getAllTaskListEntries()) > 1) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->logErrorMsg('The ChunkProcess can only handle a single RunSubProcess task but there are more tasks configured', $workflowMessage, $workflowEngine); return; } if (!$taskListEntry->task() instanceof RunSubProcess) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->logErrorMsg(sprintf('The ChunkProcess can only handle a RunSubProcess task but there is a %s task configured', get_class($taskListEntry->task())), $workflowMessage, $workflowEngine); return; } if (!MessageNameUtils::isProcessingEvent($workflowMessage->messageName())) { $this->recordThat(TaskEntryMarkedAsRunning::at($taskListEntry->taskListPosition())); $this->receiveMessage(LogMessage::logWrongMessageReceivedFor($taskListEntry->task(), $taskListEntry->taskListPosition(), $workflowMessage), $workflowEngine); return; } $collection = $workflowMessage->payload()->toType(); if (!$collection instanceof CollectionType) { $this->logErrorMsg(sprintf('The ChunkProcess requires a Prooph\\ProcessingType\\CollectionType as payload of the incoming message, but it is a %s type given', $workflowMessage->payload()->getTypeClass()), $workflowMessage, $workflowEngine); return; } $metadata = $workflowMessage->metadata(); if (!isset($metadata[self::META_OFFSET])) { $this->logErrorMsg('The ChunkProcess requires a offset key in the message metadata.', $workflowMessage, $workflowEngine); return; } if ((int) $metadata[self::META_OFFSET] !== 0) { $this->logErrorMsg('The ChunkProcess requires that the first chunk starts with a offset of 0.', $workflowMessage, $workflowEngine); return; } if (!isset($metadata[self::META_LIMIT])) { $this->logErrorMsg('The ChunkProcess requires a limit key in the message metadata.', $workflowMessage, $workflowEngine); return; } if ((int) $metadata[self::META_LIMIT] <= 0) { $this->logErrorMsg('The ChunkProcess requires that metadata.limit is greater than 0.', $workflowMessage, $workflowEngine); return; } if (!isset($metadata[self::META_TOTAL_ITEMS])) { $this->logErrorMsg('The ChunkProcess requires a total_items key in the message metadata.', $workflowMessage, $workflowEngine); return; } $this->startSubProcessForEachChunk($workflowMessage, $workflowEngine); }
/** * @test */ public function it_detects_processing_messages_by_name() { $this->assertTrue(MessageNameUtils::isWorkflowMessage('processing-message-proophprocessingTestmockuserdictionary-collect-data')); $this->assertTrue(MessageNameUtils::isProcessingCommand('processing-message-proophprocessingTestmockuserdictionary-collect-data')); $this->assertFalse(MessageNameUtils::isProcessingEvent('processing-message-proophprocessingTestmockuserdictionary-collect-data')); $this->assertTrue(MessageNameUtils::isWorkflowMessage('processing-message-proophprocessingTestmockuserdictionary-data-collected')); $this->assertFalse(MessageNameUtils::isProcessingCommand('processing-message-proophprocessingTestmockuserdictionary-data-collected')); $this->assertTrue(MessageNameUtils::isProcessingEvent('processing-message-proophprocessingTestmockuserdictionary-data-collected')); $this->assertTrue(MessageNameUtils::isWorkflowMessage('processing-message-proophprocessingTestmockuserdictionary-process-data')); $this->assertTrue(MessageNameUtils::isProcessingCommand('processing-message-proophprocessingTestmockuserdictionary-process-data')); $this->assertFalse(MessageNameUtils::isProcessingEvent('processing-message-proophprocessingTesttypemockuserdictionary-process-data')); $this->assertTrue(MessageNameUtils::isWorkflowMessage('processing-message-proophprocessingTestmockuserdictionary-data-processed')); $this->assertFalse(MessageNameUtils::isProcessingCommand('processing-message-proophprocessingTestmockuserdictionary-data-processed')); $this->assertTrue(MessageNameUtils::isProcessingEvent('processing-message-proophprocessingTestmockuserdictionary-data-processed')); }