Skip to content

Commit c814aa2

Browse files
add batch refactor for rewrite handler
1 parent 72ec24c commit c814aa2

4 files changed

Lines changed: 87 additions & 44 deletions

File tree

src/Element/ExecutionEngine/AutomationAction/Messenger/Handler/RewriteRefHandler.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Model\AbortActionData;
2323
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
2424
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
25+
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
2526
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\HandlerProgressTrait;
2627
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\PublishServiceInterface;
2728
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UserTopicServiceInterface;
2829
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
30+
use Pimcore\Model\Element\ElementDescriptor;
2931
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
3032

3133
/**
@@ -57,7 +59,7 @@ public function __invoke(RewriteRefMessage $message): void
5759
}
5860

5961
$jobRun = $this->getJobRun($message);
60-
$validatedParameters = $this->validateFullParameters(
62+
$validatedParameters = $this->validateJobParameters(
6163
$message,
6264
$jobRun,
6365
$this->userResolver,
@@ -71,36 +73,64 @@ public function __invoke(RewriteRefMessage $message): void
7173
$this->abort($validatedParameters);
7274
}
7375

76+
$user = $validatedParameters->getUser();
7477
$environmentVariables = $validatedParameters->getEnvironmentData();
75-
$element = $this->getElementById(
76-
$validatedParameters->getSubject(),
77-
$validatedParameters->getUser(),
78-
$this->elementService
79-
);
78+
$rewriteConfiguration = $environmentVariables[EnvironmentVariables::REWRITE_CONFIGURATION->value];
79+
$rewriteParameters = $environmentVariables[EnvironmentVariables::REWRITE_PARAMETERS->value];
80+
81+
$elementIds = $this->extractConfigFieldFromJobStepConfig($message, StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value);
82+
$elementType = $this->extractConfigFieldFromJobStepConfig($message, StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value);
83+
$totalItems = count($elementIds);
84+
$stepName = $this->getJobStep($message)->getName();
85+
86+
foreach ($elementIds as $elementId) {
87+
$element = $this->getElementById(
88+
new ElementDescriptor($elementType, $elementId),
89+
$user,
90+
$this->elementService,
91+
);
92+
93+
try {
94+
$this->elementReferenceService->rewriteElementReferences(
95+
$user,
96+
$element,
97+
$rewriteConfiguration,
98+
$rewriteParameters,
99+
);
100+
} catch (Exception $exception) {
101+
$this->abort($this->getAbortData(
102+
Config::ELEMENT_REWRITE_REFERENCES_FAILED_MESSAGE->value,
103+
[
104+
'type' => $element->getType(),
105+
'id' => $element->getId(),
106+
'message' => $exception->getMessage(),
107+
],
108+
));
109+
}
80110

81-
try {
82-
$this->elementReferenceService->rewriteElementReferences(
83-
$validatedParameters->getUser(),
84-
$element,
85-
$environmentVariables[EnvironmentVariables::REWRITE_CONFIGURATION->value],
86-
$environmentVariables[EnvironmentVariables::REWRITE_PARAMETERS->value],
111+
$this->updateProgress(
112+
$this->publishService,
113+
$this->userTopicService,
114+
$jobRun,
115+
$stepName,
116+
$totalItems,
117+
100,
87118
);
88-
} catch (Exception $exception) {
89-
$this->abort($this->getAbortData(
90-
Config::ELEMENT_REWRITE_REFERENCES_FAILED_MESSAGE->value,
91-
[
92-
'type' => $element->getType(),
93-
'id' => $element->getId(),
94-
'message' => $exception->getMessage(),
95-
],
96-
));
97119
}
120+
}
98121

99-
$this->updateProgress(
100-
$this->publishService,
101-
$this->userTopicService,
102-
$jobRun,
103-
$this->getJobStep($message)->getName()
122+
protected function configureStep(): void
123+
{
124+
$this->stepConfiguration->setRequired(StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value);
125+
$this->stepConfiguration->setAllowedTypes(
126+
StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value,
127+
StepConfig::CONFIG_TYPE_ARRAY->value
128+
);
129+
130+
$this->stepConfiguration->setRequired(StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value);
131+
$this->stepConfiguration->setAllowedTypes(
132+
StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value,
133+
StepConfig::CONFIG_TYPE_STRING->value
104134
);
105135
}
106136
}

src/Element/Service/ExecutionEngine/ElementReferenceService.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Pimcore\Bundle\StudioBackendBundle\Element\Service\ExecutionEngine;
1515

1616
use Exception;
17+
use Generator;
1718
use Pimcore\Bundle\GenericExecutionEngineBundle\Agent\JobExecutionAgentInterface;
1819
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\Job;
1920
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobStep;
@@ -26,10 +27,10 @@
2627
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
2728
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
2829
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
30+
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
2931
use Pimcore\Model\Asset;
3032
use Pimcore\Model\DataObject\AbstractObject;
3133
use Pimcore\Model\Document;
32-
use Pimcore\Model\Element\ElementDescriptor;
3334
use Pimcore\Model\Element\ElementInterface;
3435
use Pimcore\Model\UserInterface;
3536

@@ -38,6 +39,8 @@
3839
*/
3940
final readonly class ElementReferenceService implements ElementReferenceServiceInterface
4041
{
42+
private const int REWRITE_REFERENCES_BATCH_SIZE = 500;
43+
4144
public function __construct(
4245
private AssetServiceResolverInterface $assetServiceResolver,
4346
private DataObjectServiceResolverInterface $dataObjectServiceResolver,
@@ -82,23 +85,22 @@ public function rewriteReferencesWithExecutionEngine(
8285
array $ids,
8386
string $type
8487
): int {
88+
$jobSteps = [];
89+
foreach ($this->chunkGenerator($ids, self::REWRITE_REFERENCES_BATCH_SIZE) as $batch) {
90+
$jobSteps[] = new JobStep(
91+
JobSteps::ELEMENT_REWRITE_REFERENCE->value,
92+
RewriteRefMessage::class,
93+
'',
94+
[
95+
StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value => $batch,
96+
StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value => $type,
97+
],
98+
);
99+
}
100+
85101
$job = new Job(
86102
name: Jobs::REWRITE_REFERENCES->value,
87-
steps: [
88-
new JobStep(
89-
JobSteps::ELEMENT_REWRITE_REFERENCE->value,
90-
RewriteRefMessage::class,
91-
'',
92-
[]
93-
),
94-
],
95-
selectedElements: array_map(
96-
static fn (int $id) => new ElementDescriptor(
97-
type: $type,
98-
id: $id
99-
),
100-
$ids
101-
),
103+
steps: $jobSteps,
102104
environmentData: [
103105
EnvironmentVariables::REWRITE_CONFIGURATION->value => [$type => $rewriteConfiguration],
104106
EnvironmentVariables::REWRITE_PARAMETERS->value => [],
@@ -160,4 +162,14 @@ private function rewriteDataObjectReferences(
160162
$object->setUserModification($user->getId());
161163
$object->save();
162164
}
165+
166+
// TODO: replace with ChunkGeneratorTrait once https://github.com/pimcore/studio-backend-bundle/pull/1800 is merged
167+
private function chunkGenerator(array $items, int $size): Generator
168+
{
169+
$total = count($items);
170+
171+
for ($i = 0; $i < $total; $i += $size) {
172+
yield array_slice($items, $i, $size);
173+
}
174+
}
163175
}

src/ExecutionEngine/Util/StepConfig.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ enum StepConfig: string
2424
case CUSTOM_REPORT_TO_EXPORT = 'custom_report_to_export';
2525
case ELEMENT_CLASS_ID = 'element_class_id';
2626
case ELEMENTS_TO_EXPORT = 'elements_to_export';
27+
case ELEMENTS_TO_REWRITE_REFERENCES = 'elements_to_rewrite_references';
2728
case ELEMENTS_TO_TAG = 'elements_to_tag';
2829
case ELEMENT_TYPE = 'element_type';
2930
case ELEMENT_TYPE_TO_TAG = 'element_type_to_tag';
31+
case ELEMENT_TYPE_TO_REWRITE_REFERENCES = 'element_type_to_rewrite_references';
3032
case EXPORT_FORMAT = 'export_format';
3133
case FOLDER_TO_EXPORT = 'folder_to_export';
3234
case GRID_EXPORT_DATA = 'grid_export_data';

src/Tag/Service/ExecutionEngine/BatchService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
use Pimcore\Bundle\StudioBackendBundle\Tag\Service\TagServiceInterface;
3535
use Pimcore\Bundle\StudioBackendBundle\Tag\Util\Constant\BatchOperations;
3636
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
37-
use Pimcore\Model\Element\ElementDescriptor;
3837
use function sprintf;
3938

4039
/**
@@ -76,7 +75,7 @@ public function createJobRunForBatchOperation(BatchOperationParameters $paramete
7675
$jobSteps = [];
7776
foreach ($this->chunkGenerator($childrenIds, self::BATCH_TAG_SIZE) as $batch) {
7877
$jobSteps[] = new JobStep(
79-
JobSteps::ELEMENT_DELETION->value,
78+
$this->getJobStepName($parameters->getOperation()),
8079
BatchTagOperationMessage::class,
8180
'',
8281
[

0 commit comments

Comments
 (0)