/** * @param WorkflowMessage $workflowMessage * @return void */ public function handleWorkflowMessage(WorkflowMessage $workflowMessage) { if (!MessageNameUtils::isProcessingCommand($workflowMessage->messageName())) { $this->workflowEngine->dispatch(LogMessage::logUnsupportedMessageReceived($workflowMessage)); } try { if ($workflowMessage->messageType() === MessageNameUtils::COLLECT_DATA) { $processingMessage = $this->handleCollectData($workflowMessage); if (!$processingMessage instanceof ProcessingMessage) { throw new \RuntimeException(sprintf("%s::handleCollectData method returned %s instead of a ProcessingMessage", get_called_class(), is_object($processingMessage) ? get_class($processingMessage) : gettype($processingMessage))); } } else { if ($workflowMessage->messageType() === MessageNameUtils::PROCESS_DATA) { $processingMessage = $this->handleProcessData($workflowMessage); if (!$processingMessage instanceof ProcessingMessage) { throw new \RuntimeException(sprintf("%s::handleProcessData method returned %s instead of a ProcessingMessage", get_called_class(), is_object($processingMessage) ? get_class($processingMessage) : gettype($processingMessage))); } } else { $this->workflowEngine->dispatch(LogMessage::logUnsupportedMessageReceived($workflowMessage)); return; } } $this->workflowEngine->dispatch($processingMessage); return; } catch (\Exception $ex) { $this->workflowEngine->dispatch(LogMessage::logException($ex, $workflowMessage)); return; } }
/** * @param WorkflowMessage $message * @param bool $forceInsert * @return LogMessage|WorkflowMessage */ private function updateOrInsertPayload(WorkflowMessage $message, $forceInsert = false) { $processingType = $message->payload()->getTypeClass(); /** @var $desc Description */ $desc = $processingType::buildDescription(); $successful = 0; $failed = 0; $failedMessages = []; if ($desc->nativeType() == NativeType::COLLECTION) { /** @var $prototype Prototype */ $prototype = $processingType::prototype(); $itemProto = $prototype->typeProperties()['item']->typePrototype(); $typeObj = $message->payload()->toType(); if ($typeObj) { $this->connection->beginTransaction(); $insertStmt = null; /** @var $tableRow TableRow */ foreach ($typeObj as $i => $tableRow) { if (!$tableRow instanceof TableRow) { return LogMessage::logUnsupportedMessageReceived($message); } try { $insertStmt = $this->updateOrInsertTableRow($tableRow, $forceInsert, $insertStmt); $successful++; } catch (\Exception $e) { $datasetIndex = $tableRow->description()->hasIdentifier() ? $tableRow->description()->identifierName() . " = " . $tableRow->property($tableRow->description()->identifierName())->value() : $i; $failed++; $failedMessages[] = sprintf('Dataset %s: %s', $datasetIndex, $e->getMessage()); } } $this->connection->commit(); } $report = [MessageMetadata::SUCCESSFUL_ITEMS => $successful, MessageMetadata::FAILED_ITEMS => $failed, MessageMetadata::FAILED_MESSAGES => $failedMessages]; if ($failed > 0) { return LogMessage::logItemsProcessingFailed($successful, $failed, $failedMessages, $message); } else { return $message->answerWithDataProcessingCompleted($report); } } else { $tableRow = $message->payload()->toType(); if (!$tableRow instanceof TableRow) { return LogMessage::logUnsupportedMessageReceived($message); } $this->updateOrInsertTableRow($tableRow, $forceInsert); return $message->answerWithDataProcessingCompleted(); } }
/** * @test */ public function it_logs_unsupported_message_received_as_error() { $wfMessage = $this->getTestWorkflowMessage(); $logMessage = LogMessage::logUnsupportedMessageReceived($wfMessage); $this->assertTrue($logMessage->isError()); $this->assertEquals(416, $logMessage->msgCode()); $this->assertTrue($wfMessage->processTaskListPosition()->equals($logMessage->processTaskListPosition())); $this->assertTrue(isset($logMessage->msgParams()['workflow_message_handler'])); $this->assertTrue(isset($logMessage->msgParams()['message_name'])); $this->assertEquals($wfMessage->target(), $logMessage->msgParams()['workflow_message_handler']); $this->assertEquals($wfMessage->messageName(), $logMessage->msgParams()['message_name']); }