Skip to content

Commit ac6507b

Browse files
[Export] Added batching export of resources data
1 parent b79c773 commit ac6507b

File tree

11 files changed

+141
-25
lines changed

11 files changed

+141
-25
lines changed

config/doctrine/Process.orm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
<field name="type" column="type" type="string" />
1313
<field name="status" column="status" type="string" />
1414
<field name="resource" column="resource" type="string" />
15+
<field name="grid" column="grid" type="string" />
1516
<field name="output" column="output" type="text" nullable="true" />
1617
<field name="errorMessage" column="error_message" type="text" nullable="true" />
1718
<field name="format" column="format" type="string" />
19+
<field name="parameters" column="parameters" type="json" />
1820
<field name="resourceIds" column="resource_ids" type="json" nullable="true" />
1921
<field name="createdAt" column="created_at" type="datetime">
2022
<gedmo:timestampable on="create" />

config/services.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
</imports>
1111

1212
<services>
13+
<service id="sylius_import_export.messenger.command_handler.create_export_process" class="Sylius\GridImportExport\Messenger\Handler\CreateExportProcessHandler">
14+
<argument type="service" id="sylius_import_export.custom_factory.process" />
15+
<argument type="service" id="sylius_grid_import_export.repository.process" />
16+
<argument type="service" id="sylius.command_bus" />
17+
18+
<tag name="messenger.message_handler" bus="sylius.command_bus" />
19+
</service>
20+
1321
<service id="sylius_import_export.messenger.command_handler.export" class="Sylius\GridImportExport\Messenger\Handler\ExportCommandHandler">
1422
<argument type="service" id="sylius.resource_registry" />
15-
<argument type="service" id="sylius_import_export.custom_factory.process" />
1623
<argument type="service" id="sylius_grid_import_export.repository.process" />
1724
<argument type="service" id="sylius_import_export.registry.resource_data_provider" />
1825
<argument type="service" id="sylius_grid_import_export.exporter_resolver" />

src/Controller/ExportAction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use Sylius\Bundle\ResourceBundle\Controller\ParametersParserInterface;
1717
use Sylius\Component\Grid\Provider\GridProviderInterface;
18-
use Sylius\GridImportExport\Messenger\Command\ExportCommand;
18+
use Sylius\GridImportExport\Messenger\Command\CreateExportProcess;
1919
use Sylius\GridImportExport\Provider\ResourceIds\ResourcesIdsProviderInterface;
2020
use Sylius\Resource\Metadata\RegistryInterface;
2121
use Symfony\Component\Form\FormFactoryInterface;
@@ -62,12 +62,12 @@ public function __invoke(Request $request, string $grid): Response
6262
$request,
6363
);
6464

65-
$this->commandBus->dispatch(new ExportCommand(
65+
$this->commandBus->dispatch(new CreateExportProcess(
6666
resource: $metadata->getAlias(),
67-
grid: $grid,
6867
format: $format,
69-
resourceIds: $resourceIds,
68+
grid: $grid,
7069
parameters: $parameters,
70+
resourceIds: $resourceIds,
7171
));
7272

7373
/** @var Session $session */

src/Entity/Process.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ class Process implements ProcessInterface
2525

2626
protected string $resource;
2727

28+
protected string $grid;
29+
2830
protected string $format;
2931

3032
protected string $status;
3133

34+
protected array $parameters = [];
35+
3236
protected array $resourceIds = [];
3337

3438
protected string $output;
@@ -85,6 +89,26 @@ public function setResource(string $resource): void
8589
$this->resource = $resource;
8690
}
8791

92+
public function getGrid(): string
93+
{
94+
return $this->grid;
95+
}
96+
97+
public function setGrid(string $grid): void
98+
{
99+
$this->grid = $grid;
100+
}
101+
102+
public function getParameters(): array
103+
{
104+
return $this->parameters;
105+
}
106+
107+
public function setParameters(array $parameters): void
108+
{
109+
$this->parameters = $parameters;
110+
}
111+
88112
public function setUuid(string $uuid): void
89113
{
90114
$this->uuid = $uuid;

src/Entity/ProcessInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public function getResource(): string;
4242

4343
public function setResource(string $resource): void;
4444

45+
public function getGrid(): string;
46+
47+
public function setGrid(string $grid): void;
48+
49+
public function getParameters(): array;
50+
51+
public function setParameters(array $parameters): void;
52+
4553
public function getResourceIds(): array;
4654

4755
public function setResourceIds(array $resourceIds): void;

src/Factory/ProcessFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Sylius\GridImportExport\Factory;
1515

1616
use Sylius\GridImportExport\Entity\ProcessInterface;
17-
use Sylius\GridImportExport\Messenger\Command\ExportCommand;
17+
use Sylius\GridImportExport\Messenger\Command\CreateExportProcess;
1818
use Sylius\Resource\Factory\FactoryInterface;
1919
use Symfony\Component\Uid\Uuid;
2020

@@ -30,13 +30,15 @@ public function createNew(): ProcessInterface
3030
return $this->factory->createNew();
3131
}
3232

33-
public function createFromExportCommand(ExportCommand $command): ProcessInterface
33+
public function createForExport(CreateExportProcess $command): ProcessInterface
3434
{
3535
$process = $this->createNew();
3636
$process->setUuid(Uuid::v7()->toRfc4122());
3737
$process->setType(ProcessInterface::TYPE_EXPORT);
3838
$process->setResource($command->resource);
39+
$process->setGrid($command->grid);
3940
$process->setFormat($command->format);
41+
$process->setParameters($command->parameters);
4042
$process->setResourceIds($command->resourceIds);
4143
$process->setStatus('processing');
4244

src/Factory/ProcessFactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
namespace Sylius\GridImportExport\Factory;
1515

1616
use Sylius\GridImportExport\Entity\ProcessInterface;
17-
use Sylius\GridImportExport\Messenger\Command\ExportCommand;
17+
use Sylius\GridImportExport\Messenger\Command\CreateExportProcess;
1818
use Sylius\Resource\Factory\FactoryInterface;
1919

2020
/** @extends FactoryInterface<ProcessInterface> */
2121
interface ProcessFactoryInterface extends FactoryInterface
2222
{
23-
public function createFromExportCommand(ExportCommand $command): ProcessInterface;
23+
public function createForExport(CreateExportProcess $command): ProcessInterface;
2424
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\GridImportExport\Messenger\Command;
15+
16+
class CreateExportProcess
17+
{
18+
/**
19+
* @param array<mixed> $parameters
20+
* @param array<array-key, string|int> $resourceIds
21+
*/
22+
public function __construct(
23+
public string $resource,
24+
public string $format,
25+
public string $grid,
26+
public array $parameters,
27+
public array $resourceIds,
28+
) {
29+
}
30+
}

src/Messenger/Command/ExportCommand.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
class ExportCommand
1717
{
1818
public function __construct(
19-
public string $resource,
20-
public string $grid,
21-
public string $format,
19+
public string $processId,
2220
public array $resourceIds,
23-
public array $parameters,
2421
) {
2522
}
2623
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\GridImportExport\Messenger\Handler;
15+
16+
use Sylius\GridImportExport\Entity\ProcessInterface;
17+
use Sylius\GridImportExport\Factory\ProcessFactoryInterface;
18+
use Sylius\GridImportExport\Messenger\Command\CreateExportProcess;
19+
use Sylius\GridImportExport\Messenger\Command\ExportCommand;
20+
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
21+
use Symfony\Component\Messenger\MessageBusInterface;
22+
23+
class CreateExportProcessHandler
24+
{
25+
/**
26+
* @param RepositoryInterface<ProcessInterface> $processRepository
27+
* @param int<1, max> $batchSize
28+
*/
29+
public function __construct(
30+
public ProcessFactoryInterface $processFactory,
31+
public RepositoryInterface $processRepository,
32+
public MessageBusInterface $messageBus,
33+
public int $batchSize = 100,
34+
) {
35+
}
36+
37+
public function __invoke(CreateExportProcess $command): void
38+
{
39+
$process = $this->processFactory->createForExport($command);
40+
41+
$this->processRepository->add($process);
42+
43+
foreach (array_chunk($process->getResourceIds(), $this->batchSize) as $batch) {
44+
$this->messageBus->dispatch(new ExportCommand(
45+
processId: $process->getUuid(),
46+
resourceIds: $batch,
47+
));
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)