/** * @test */ public function it_translates_to_service_bus_message_and_back() { $subProcessDefinition = ["process_type" => Definition::PROCESS_LINEAR_MESSAGING, "tasks" => [["task_type" => Definition::TASK_COLLECT_DATA, "source" => 'test-case', "processing_type" => 'Prooph\\ProcessingTest\\Mock\\UserDictionary']]]; $parentTaskListPosition = TaskListPosition::at(TaskListId::linkWith(NodeName::defaultName(), ProcessId::generate()), 1); $command = StartSubProcess::at($parentTaskListPosition, $subProcessDefinition, false, 'sub-processor'); $sbMessage = $command->toServiceBusMessage(); $this->assertInstanceOf('Prooph\\Common\\Messaging\\RemoteMessage', $sbMessage); $copyOfCommand = StartSubProcess::fromServiceBusMessage($sbMessage); $this->assertInstanceOf('Prooph\\Processing\\Processor\\Command\\StartSubProcess', $copyOfCommand); $this->assertTrue($parentTaskListPosition->equals($copyOfCommand->parentTaskListPosition())); $this->assertEquals($subProcessDefinition, $copyOfCommand->subProcessDefinition()); $this->assertFalse($copyOfCommand->syncLogMessages()); $this->assertEquals(NodeName::defaultName()->toString(), $copyOfCommand->origin()); $this->assertEquals('sub-processor', $copyOfCommand->target()); }
/** * @param RemoteMessage $message * @return LogMessage|WorkflowMessage|StartSubProcess|SubProcessFinished * @throws \InvalidArgumentException */ public function translateToProcessingMessage(RemoteMessage $message) { if (MessageNameUtils::isWorkflowMessage($message->name())) { return WorkflowMessage::fromServiceBusMessage($message); } else { if (MessageNameUtils::isProcessingLogMessage($message->name())) { return LogMessage::fromServiceBusMessage($message); } else { if (StartSubProcess::MSG_NAME === $message->name()) { return StartSubProcess::fromServiceBusMessage($message); } else { if (SubProcessFinished::MSG_NAME === $message->name()) { return SubProcessFinished::fromServiceBusMessage($message); } } } } throw new \InvalidArgumentException(sprintf('Message with name %s can not be translated. Unknown type provided.', $message->name())); }
public function provideMessages() { $localTaskListPosition = TaskListPosition::at(TaskListId::linkWith(NodeName::defaultName(), ProcessId::generate()), 1); $remoteTaskListPosition = TaskListPosition::at(TaskListId::linkWith(NodeName::fromString("remote-system"), ProcessId::generate()), 1); $wfMessageWithoutTaskListPosition = WorkflowMessage::collectDataOf(TestUser::prototype(), 'test-case', 'localhost'); $expectedDataWfMessageWithoutTaskListPosition = ['message_id' => $wfMessageWithoutTaskListPosition->uuid()->toString(), 'message_name' => $wfMessageWithoutTaskListPosition->messageName(), 'version' => $wfMessageWithoutTaskListPosition->version(), 'task_list_position' => null, 'process_id' => null, 'status' => MessageStatus::PENDING, 'failure_msg' => null]; $wfMessageWithTaskListPosition = WorkflowMessage::collectDataOf(TestUser::prototype(), 'test-case', 'localhost'); $wfMessageWithTaskListPosition->connectToProcessTask($localTaskListPosition); $expectedDataWfMessageWithTaskListPosition = ['message_id' => $wfMessageWithTaskListPosition->uuid()->toString(), 'message_name' => $wfMessageWithTaskListPosition->messageName(), 'version' => $wfMessageWithTaskListPosition->version(), 'task_list_position' => $wfMessageWithTaskListPosition->processTaskListPosition()->toString(), 'process_id' => $wfMessageWithTaskListPosition->processTaskListPosition()->taskListId()->processId()->toString(), 'status' => MessageStatus::PENDING, 'failure_msg' => null]; $logMessage = LogMessage::logDebugMsg("Log message", $wfMessageWithTaskListPosition); $expectedDataLogMessage = ['message_id' => $logMessage->uuid()->toString(), 'message_name' => $logMessage->messageName(), 'version' => 1, 'task_list_position' => $logMessage->processTaskListPosition()->toString(), 'process_id' => $logMessage->processTaskListPosition()->taskListId()->processId()->toString(), 'status' => MessageStatus::PENDING, 'failure_msg' => null]; $startSubProcess = StartSubProcess::at($localTaskListPosition, ["process_type" => "faked"], false, "remote-system"); $expectedDataStartSubProcess = ['message_id' => $startSubProcess->uuid()->toString(), 'message_name' => $startSubProcess->messageName(), 'version' => $startSubProcess->version(), 'task_list_position' => $startSubProcess->parentTaskListPosition()->toString(), 'process_id' => $startSubProcess->parentTaskListPosition()->taskListId()->processId()->toString(), 'status' => MessageStatus::PENDING, 'failure_msg' => null]; $wfMessageWithRemoteTaskListPosition = WorkflowMessage::collectDataOf(TestUser::prototype(), 'test-case', 'localhost'); $wfMessageWithRemoteTaskListPosition->connectToProcessTask($remoteTaskListPosition); $logMessageSubProcess = LogMessage::logErrorMsg("Faked error", $wfMessageWithRemoteTaskListPosition); $subProcessFinished = SubProcessFinished::record(NodeName::fromString("remote-system"), $remoteTaskListPosition->taskListId()->processId(), false, $logMessageSubProcess, $localTaskListPosition); $expectedDataSubProcessFinished = ['message_id' => $subProcessFinished->uuid()->toString(), 'message_name' => $subProcessFinished->messageName(), 'version' => $subProcessFinished->version(), 'task_list_position' => $logMessageSubProcess->processTaskListPosition()->toString(), 'process_id' => $logMessageSubProcess->processTaskListPosition()->taskListId()->processId()->toString(), 'status' => MessageStatus::PENDING, 'failure_msg' => null]; return [[$wfMessageWithoutTaskListPosition, $expectedDataWfMessageWithoutTaskListPosition], [$wfMessageWithoutTaskListPosition->toServiceBusMessage(), $expectedDataWfMessageWithoutTaskListPosition], [$wfMessageWithTaskListPosition, $expectedDataWfMessageWithTaskListPosition], [$wfMessageWithTaskListPosition->toServiceBusMessage(), $expectedDataWfMessageWithTaskListPosition], [$logMessage, $expectedDataLogMessage], [$logMessage->toServiceBusMessage(), $expectedDataLogMessage], [$startSubProcess, $expectedDataStartSubProcess], [$startSubProcess->toServiceBusMessage(), $expectedDataStartSubProcess], [$subProcessFinished, $expectedDataSubProcessFinished], [$subProcessFinished->toServiceBusMessage(), $expectedDataSubProcessFinished]]; }
/** * @test */ public function it_sends_a_start_sub_process_command_via_message_dispatcher_to_a_handler() { $taskListPosition = TaskListPosition::at(TaskListId::linkWith(NodeName::defaultName(), ProcessId::generate()), 1); $startSupProcess = StartSubProcess::at($taskListPosition, ['process_type' => 'faked'], true, 'sub-processor'); $commandBus = new CommandBus(); $commandRouter = new CommandRouter(); $commandRouter->route(StartSubProcess::MSG_NAME)->to($this->messageDispatcher); $commandBus->utilize($commandRouter); $commandBus->utilize(new ForwardToRemoteMessageDispatcherStrategy(new FromProcessingMessageTranslator())); $commandBus->dispatch($startSupProcess); /** @var $receivedMessage StartSubProcess */ $receivedMessage = $this->receivedMessage; $this->assertInstanceOf(get_class($startSupProcess), $receivedMessage); $this->assertTrue($taskListPosition->equals($receivedMessage->parentTaskListPosition())); $this->assertTrue($startSupProcess->uuid()->equals($receivedMessage->uuid())); $this->assertEquals($startSupProcess->payload(), $receivedMessage->payload()); $this->assertEquals($startSupProcess->createdAt()->format('Y-m-d H:i:s'), $receivedMessage->createdAt()->format('Y-m-d H:i:s')); $this->assertEquals($startSupProcess->target(), $receivedMessage->target()); }
/** * @param StartSubProcess $command * @throws \Exception */ private function startSubProcess(StartSubProcess $command) { $subProcess = $this->processFactory->createProcessFromDefinition($command->subProcessDefinition(), $this->nodeName, $command->parentTaskListPosition()); $this->beginTransaction(); try { $subProcess->perform($this->workflowEngine, $command->previousWorkflowMessage()); $this->processRepository->add($subProcess); $this->commitTransaction(); } catch (\Exception $ex) { $this->rollbackTransaction(); throw $ex; } }
/** * @param StartSubProcess $message * @return MessageLogEntry */ public static function logStartSubProcess(StartSubProcess $message) { return self::createFromMessageProps($message->uuid(), $message->messageName(), $message->version(), $message->parentTaskListPosition()); }
/** * @param TaskListPosition $parentTaskListPosition * @param WorkflowMessage|null $previousMessage * @return StartSubProcess */ public function generateStartCommandForSubProcess(TaskListPosition $parentTaskListPosition, WorkflowMessage $previousMessage = null) { return StartSubProcess::at($parentTaskListPosition, $this->processDefinition, $this->syncLogMessages, $this->targetNodeName()->toString(), $previousMessage); }
/** * @test */ public function it_dispatches_a_start_sub_process_command() { $taskListPosition = TaskListPosition::at(TaskListId::linkWith(NodeName::defaultName(), ProcessId::generate()), 1); $startSupProcess = StartSubProcess::at($taskListPosition, ['process_type' => 'faked'], true, 'sub-processor'); $this->workflowEngine->dispatch($startSupProcess); $this->assertSame($startSupProcess, $this->receivedMessage); }