|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Temporal\Internal\Mapper; |
| 6 | + |
| 7 | +use Temporal\Activity\ActivityType as ActivityTypeDto; |
| 8 | +use Temporal\Api\Activity\V1\ActivityOptions as ActivityOptionsMessage; |
| 9 | +use Temporal\Api\Common\V1\Priority as PriorityMessage; |
| 10 | +use Temporal\Api\Common\V1\RetryPolicy; |
| 11 | +use Temporal\Api\Deployment\V1\WorkerDeploymentVersion as WorkerDeploymentVersionMessage; |
| 12 | +use Temporal\Api\Failure\V1\Failure; |
| 13 | +use Temporal\Api\Workflow\V1\PendingActivityInfo; |
| 14 | +use Temporal\Api\Workflow\V1\PendingActivityInfo\PauseInfo; |
| 15 | +use Temporal\Common\Priority as PriorityDto; |
| 16 | +use Temporal\Common\Versioning\WorkerDeploymentVersion; |
| 17 | +use Temporal\DataConverter\DataConverterInterface; |
| 18 | +use Temporal\DataConverter\EncodedValues; |
| 19 | +use Temporal\DataConverter\ValuesInterface; |
| 20 | +use Temporal\Exception\Failure\FailureConverter; |
| 21 | +use Temporal\Exception\Failure\TemporalFailure; |
| 22 | +use Temporal\Internal\Support\DateInterval; |
| 23 | +use Temporal\Workflow\PendingActivityInfo as PendingActivityInfoDto; |
| 24 | +use Temporal\Workflow\PendingActivityOptions; |
| 25 | +use Temporal\Workflow\PendingActivityPauseInfo; |
| 26 | +use Temporal\Workflow\PendingActivityPauseInfoManual; |
| 27 | +use Temporal\Workflow\PendingActivityPauseInfoRule; |
| 28 | +use Temporal\Workflow\PendingActivityRetryPolicy; |
| 29 | +use Temporal\Workflow\PendingActivityState; |
| 30 | + |
| 31 | +final class PendingActivityInfoMapper |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly DataConverterInterface $converter, |
| 35 | + ) {} |
| 36 | + |
| 37 | + /** |
| 38 | + * @psalm-suppress DocblockTypeContradiction, RedundantConditionGivenDocblockType |
| 39 | + */ |
| 40 | + public function fromMessage(PendingActivityInfo $message): PendingActivityInfoDto |
| 41 | + { |
| 42 | + $activityType = new ActivityTypeDto(); |
| 43 | + /** @psalm-suppress InaccessibleProperty */ |
| 44 | + $activityType->name = $message->getActivityType()?->getName() ?? ''; |
| 45 | + |
| 46 | + $retryInterval = $message->getCurrentRetryInterval(); |
| 47 | + $retryInterval === null or $retryInterval = DateInterval::parse($retryInterval); |
| 48 | + |
| 49 | + return new PendingActivityInfoDto( |
| 50 | + activityId: $message->getActivityId(), |
| 51 | + activityType: $activityType, |
| 52 | + state: PendingActivityState::from($message->getState()), |
| 53 | + heartbeatDetails: $this->prepareHeartbeatDetails($message), |
| 54 | + lastHeartbeatTime: $message->getLastHeartbeatTime()?->toDateTime(), |
| 55 | + lastStartedTime: $message->getLastStartedTime()?->toDateTime(), |
| 56 | + attempt: $message->getAttempt(), |
| 57 | + maximumAttempts: $message->getMaximumAttempts(), |
| 58 | + scheduledTime: $message->getScheduledTime()?->toDateTime(), |
| 59 | + expirationTime: $message->getExpirationTime()?->toDateTime(), |
| 60 | + lastFailure: $this->prepareFailure($message->getLastFailure()), |
| 61 | + lastWorkerIdentity: $message->getLastWorkerIdentity(), |
| 62 | + currentRetryInterval: $retryInterval, |
| 63 | + lastAttemptCompleteTime: $message->getLastAttemptCompleteTime()?->toDateTime(), |
| 64 | + nextAttemptScheduleTime: $message->getNextAttemptScheduleTime()?->toDateTime(), |
| 65 | + paused: $message->getPaused(), |
| 66 | + lastDeploymentVersion: $this->prepareDeploymentVersion($message->getLastDeploymentVersion()), |
| 67 | + priority: $this->preparePriority($message->getPriority()), |
| 68 | + pauseInfo: $this->preparePauseInfo($message->getPauseInfo()), |
| 69 | + activityOptions: $this->prepareActivityOptions($message->getActivityOptions()), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + private function prepareHeartbeatDetails(PendingActivityInfo $message): ValuesInterface |
| 74 | + { |
| 75 | + $details = $message->getHeartbeatDetails(); |
| 76 | + |
| 77 | + return $details === null |
| 78 | + ? EncodedValues::empty() |
| 79 | + : EncodedValues::fromPayloads($details, $this->converter); |
| 80 | + } |
| 81 | + |
| 82 | + private function prepareFailure(?Failure $failure): ?TemporalFailure |
| 83 | + { |
| 84 | + return $failure === null |
| 85 | + ? null |
| 86 | + : FailureConverter::mapFailureToException($failure, $this->converter); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @psalm-suppress ArgumentTypeCoercion |
| 91 | + */ |
| 92 | + private function prepareDeploymentVersion(?WorkerDeploymentVersionMessage $version): ?WorkerDeploymentVersion |
| 93 | + { |
| 94 | + return $version === null |
| 95 | + ? null |
| 96 | + : WorkerDeploymentVersion::new($version->getDeploymentName(), $version->getBuildId()); |
| 97 | + } |
| 98 | + |
| 99 | + private function preparePriority(?PriorityMessage $priority): ?PriorityDto |
| 100 | + { |
| 101 | + if ($priority === null) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + $result = PriorityDto::new($priority->getPriorityKey()) |
| 106 | + ->withFairnessKey($priority->getFairnessKey()); |
| 107 | + |
| 108 | + $weight = $priority->getFairnessWeight(); |
| 109 | + $weight >= 0.001 && $weight <= 1000.0 and $result = $result->withFairnessWeight($weight); |
| 110 | + |
| 111 | + return $result; |
| 112 | + } |
| 113 | + |
| 114 | + private function preparePauseInfo(?PauseInfo $pauseInfo): ?PendingActivityPauseInfo |
| 115 | + { |
| 116 | + if ($pauseInfo === null) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + $manual = $pauseInfo->getManual(); |
| 121 | + $rule = $pauseInfo->getRule(); |
| 122 | + |
| 123 | + return new PendingActivityPauseInfo( |
| 124 | + pauseTime: $pauseInfo->getPauseTime()?->toDateTime(), |
| 125 | + manual: $manual === null |
| 126 | + ? null |
| 127 | + : new PendingActivityPauseInfoManual($manual->getIdentity(), $manual->getReason()), |
| 128 | + rule: $rule === null |
| 129 | + ? null |
| 130 | + : new PendingActivityPauseInfoRule($rule->getRuleId(), $rule->getIdentity(), $rule->getReason()), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @psalm-suppress DocblockTypeContradiction, RedundantConditionGivenDocblockType |
| 136 | + */ |
| 137 | + private function prepareActivityOptions(?ActivityOptionsMessage $options): ?PendingActivityOptions |
| 138 | + { |
| 139 | + if ($options === null) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + $scheduleToClose = $options->getScheduleToCloseTimeout(); |
| 144 | + $scheduleToStart = $options->getScheduleToStartTimeout(); |
| 145 | + $startToClose = $options->getStartToCloseTimeout(); |
| 146 | + $heartbeat = $options->getHeartbeatTimeout(); |
| 147 | + $retryPolicy = $options->getRetryPolicy(); |
| 148 | + |
| 149 | + return new PendingActivityOptions( |
| 150 | + taskQueue: $options->getTaskQueue()?->getName(), |
| 151 | + scheduleToCloseTimeout: $scheduleToClose === null ? null : DateInterval::parse($scheduleToClose), |
| 152 | + scheduleToStartTimeout: $scheduleToStart === null ? null : DateInterval::parse($scheduleToStart), |
| 153 | + startToCloseTimeout: $startToClose === null ? null : DateInterval::parse($startToClose), |
| 154 | + heartbeatTimeout: $heartbeat === null ? null : DateInterval::parse($heartbeat), |
| 155 | + retryPolicy: $retryPolicy === null ? null : $this->prepareRetryPolicy($retryPolicy), |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @psalm-suppress DocblockTypeContradiction, RedundantConditionGivenDocblockType, TooManyTemplateParams |
| 161 | + */ |
| 162 | + private function prepareRetryPolicy(RetryPolicy $policy): PendingActivityRetryPolicy |
| 163 | + { |
| 164 | + $initialInterval = $policy->getInitialInterval(); |
| 165 | + $maximumInterval = $policy->getMaximumInterval(); |
| 166 | + |
| 167 | + $nonRetryableErrorTypes = []; |
| 168 | + foreach ($policy->getNonRetryableErrorTypes() as $errorType) { |
| 169 | + $nonRetryableErrorTypes[] = $errorType; |
| 170 | + } |
| 171 | + |
| 172 | + return new PendingActivityRetryPolicy( |
| 173 | + initialInterval: $initialInterval === null ? null : DateInterval::parse($initialInterval), |
| 174 | + backoffCoefficient: $policy->getBackoffCoefficient(), |
| 175 | + maximumInterval: $maximumInterval === null ? null : DateInterval::parse($maximumInterval), |
| 176 | + maximumAttempts: $policy->getMaximumAttempts(), |
| 177 | + nonRetryableErrorTypes: $nonRetryableErrorTypes, |
| 178 | + ); |
| 179 | + } |
| 180 | +} |
0 commit comments