Skip to content

Commit 326afb4

Browse files
authored
Mark jobs as final once again, but add an abstract decorator that helps construction (#82)
1 parent 16983be commit 326afb4

File tree

8 files changed

+100
-39
lines changed

8 files changed

+100
-39
lines changed

src/batch-symfony-framework/docs/getting-started.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ use Symfony\Component\HttpKernel\KernelInterface;
8888
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
8989
use Yokai\Batch\Bridge\Doctrine\Persistence\ObjectWriter;
9090
use Yokai\Batch\Bridge\Symfony\Serializer\DenormalizeItemProcessor;
91+
use Yokai\Batch\Job\AbstractDecoratedJob;
9192
use Yokai\Batch\Job\Item\ItemJob;
9293
use Yokai\Batch\Job\Item\Reader\Filesystem\JsonLinesReader;
9394
use Yokai\Batch\Job\Parameters\DefaultParameterAccessor;
9495
use Yokai\Batch\Job\Parameters\JobExecutionParameterAccessor;
9596
use Yokai\Batch\Storage\JobExecutionStorageInterface;
9697

97-
final class ImportUsersJob extends ItemJob
98+
final class ImportUsersJob extends AbstractDecoratedJob
9899
{
99100
public function __construct(
100101
JobExecutionStorageInterface $executionStorage,
@@ -103,16 +104,18 @@ final class ImportUsersJob extends ItemJob
103104
KernelInterface $kernel,
104105
) {
105106
parent::__construct(
106-
500,
107-
new JsonLinesReader(
108-
new DefaultParameterAccessor(
109-
new JobExecutionParameterAccessor('importFile'),
110-
$kernel->getProjectDir() . '/var/import/users.jsonl'
111-
)
112-
),
113-
new DenormalizeItemProcessor($denormalizer, User::class),
114-
new ObjectWriter($doctrine),
115-
$executionStorage
107+
new ItemJob(
108+
500,
109+
new JsonLinesReader(
110+
new DefaultParameterAccessor(
111+
new JobExecutionParameterAccessor('importFile'),
112+
$kernel->getProjectDir() . '/var/import/users.jsonl'
113+
)
114+
),
115+
new DenormalizeItemProcessor($denormalizer, User::class),
116+
new ObjectWriter($doctrine),
117+
$executionStorage
118+
)
116119
);
117120
}
118121
}

src/batch/docs/domain/job.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ The only requirement is implementing [`JobInterface`](../../src/Job/JobInterface
3131
## What types of job exists ?
3232

3333
**Built-in jobs:**
34+
- [AbstractDecoratedJob](../../src/Job/AbstractDecoratedJob.php):
35+
a job that is designed to be extended, helps job construction.
3436
- [ItemJob](../../src/Job/Item/ItemJob.php):
3537
ETL like, batch processing job ([documentation](item-job.md)).
3638
- [JobWithChildJobs](../../src/Job/JobWithChildJobs.php):
3739
a job that trigger other jobs ([documentation](job-with-children.md)).
3840
- [TriggerScheduledJobsJob](../../src/Trigger/TriggerScheduledJobsJob.php):
41+
a job that trigger other jobs when schedule is due (todo documentation).
3942

4043
**Jobs from bridges:**
4144
- [CopyFilesJob (`league/flysystem`)](https://github.com/yokai-php/batch-league-flysystem/blob/0.x/src/Job/CopyFilesJob.php):
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Job;
6+
7+
use Yokai\Batch\JobExecution;
8+
9+
/**
10+
* This {@see JobInterface} is designed to be extended in your project.
11+
* It decorates another {@see JobInterface} that will actually run the code.
12+
* It might be used as a "constructor helper".
13+
*/
14+
abstract class AbstractDecoratedJob implements JobInterface
15+
{
16+
public function __construct(
17+
private JobInterface $job,
18+
) {
19+
}
20+
21+
final public function execute(JobExecution $jobExecution): void
22+
{
23+
$this->preExecute($jobExecution);
24+
$this->job->execute($jobExecution);
25+
$this->postExecute($jobExecution);
26+
}
27+
28+
/**
29+
* Overrides this method if you want to do something before the job is executed.
30+
*/
31+
protected function preExecute(JobExecution $jobExecution): void
32+
{
33+
}
34+
35+
/**
36+
* Overrides this method if you want to do something after the job is executed.
37+
*/
38+
protected function postExecute(JobExecution $jobExecution): void
39+
{
40+
}
41+
}

src/batch/src/Job/Item/ItemJob.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yokai\Batch\Job\Item;
66

7+
use Yokai\Batch\Job\AbstractDecoratedJob;
78
use Yokai\Batch\Job\Item\Exception\SkipItemException;
89
use Yokai\Batch\Job\JobInterface;
910
use Yokai\Batch\JobExecution;
@@ -17,6 +18,8 @@
1718
* Items are Extracted using an {@see ItemReaderInterface}.
1819
* Then Transformed using an {@see ItemProcessorInterface}.
1920
* And finally Loaded using an {@see ItemWriterInterface}.
21+
*
22+
* @final use {@see AbstractDecoratedJob} instead.
2023
*/
2124
class ItemJob implements JobInterface
2225
{

src/batch/src/Job/JobWithChildJobs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
/**
1212
* This {@see JobInterface} will execute by triggering child jobs.
1313
* If a child job fails, following child jobs won't be executed.
14+
*
15+
* @final use {@see AbstractDecoratedJob} instead.
1416
*/
1517
class JobWithChildJobs implements JobInterface
1618
{

tests/symfony/src/Job/Country/CountryJob.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yokai\Batch\Bridge\Box\Spout\Writer\FlatFileWriter;
99
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\CSVOptions;
1010
use Yokai\Batch\Bridge\Symfony\Framework\JobWithStaticNameInterface;
11+
use Yokai\Batch\Job\AbstractDecoratedJob;
1112
use Yokai\Batch\Job\Item\ElementConfiguratorTrait;
1213
use Yokai\Batch\Job\Item\FlushableInterface;
1314
use Yokai\Batch\Job\Item\ItemJob;
@@ -43,7 +44,7 @@
4344
* - a JSONL file, like :
4445
* {"iso2":"FR","iso3":"FRA","name":"France","continent":"EU","currency":"EUR","phone":"33"}
4546
*/
46-
final class CountryJob extends ItemJob implements
47+
final class CountryJob extends AbstractDecoratedJob implements
4748
JobWithStaticNameInterface,
4849
JobExecutionAwareInterface,
4950
ItemProcessorInterface,
@@ -83,11 +84,13 @@ public function __construct(JobExecutionStorageInterface $executionStorage, Kern
8384
]);
8485

8586
parent::__construct(
86-
\PHP_INT_MAX,
87-
new SequenceReader(\array_map($reader, $fragments)),
88-
$this,
89-
$this,
90-
$executionStorage
87+
new ItemJob(
88+
\PHP_INT_MAX,
89+
new SequenceReader(\array_map($reader, $fragments)),
90+
$this,
91+
$this,
92+
$executionStorage
93+
),
9194
);
9295
}
9396

tests/symfony/src/Job/StarWars/AbstractImportStartWarsEntityJob.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Yokai\Batch\Bridge\Doctrine\Persistence\ObjectWriter;
1414
use Yokai\Batch\Bridge\Symfony\Framework\JobWithStaticNameInterface;
1515
use Yokai\Batch\Bridge\Symfony\Validator\SkipInvalidItemProcessor;
16+
use Yokai\Batch\Job\AbstractDecoratedJob;
1617
use Yokai\Batch\Job\Item\ItemJob;
1718
use Yokai\Batch\Job\Item\Processor\ArrayMapProcessor;
1819
use Yokai\Batch\Job\Item\Processor\CallbackProcessor;
@@ -35,7 +36,7 @@
3536
* Writer :
3637
* - Write processed entities to the database.
3738
*/
38-
abstract class AbstractImportStartWarsEntityJob extends ItemJob implements JobWithStaticNameInterface
39+
abstract class AbstractImportStartWarsEntityJob extends AbstractDecoratedJob implements JobWithStaticNameInterface
3940
{
4041
public function __construct(
4142
string $file,
@@ -45,21 +46,23 @@ public function __construct(
4546
JobExecutionStorageInterface $executionStorage
4647
) {
4748
parent::__construct(
48-
50, // could be much higher, but set this way for demo purpose
49-
new FlatFileReader(
50-
new StaticValueParameterAccessor($file),
51-
new CSVOptions(),
52-
HeaderStrategy::combine()
53-
),
54-
new ChainProcessor([
55-
new ArrayMapProcessor(
56-
fn(string $value) => $value === 'NA' ? null : $value
49+
new ItemJob(
50+
50, // could be much higher, but set this way for demo purpose
51+
new FlatFileReader(
52+
new StaticValueParameterAccessor($file),
53+
new CSVOptions(),
54+
HeaderStrategy::combine()
5755
),
58-
new CallbackProcessor($process),
59-
new SkipInvalidItemProcessor($validator),
60-
]),
61-
new ObjectWriter($doctrine),
62-
$executionStorage
56+
new ChainProcessor([
57+
new ArrayMapProcessor(
58+
fn(string $value) => $value === 'NA' ? null : $value
59+
),
60+
new CallbackProcessor($process),
61+
new SkipInvalidItemProcessor($validator),
62+
]),
63+
new ObjectWriter($doctrine),
64+
$executionStorage
65+
)
6366
);
6467
}
6568
}

tests/symfony/src/Job/StarWars/ImportStarWarsJob.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yokai\Batch\Sources\Tests\Symfony\App\Job\StarWars;
66

77
use Yokai\Batch\Bridge\Symfony\Framework\JobWithStaticNameInterface;
8+
use Yokai\Batch\Job\AbstractDecoratedJob;
89
use Yokai\Batch\Job\JobExecutor;
910
use Yokai\Batch\Job\JobWithChildJobs;
1011
use Yokai\Batch\Storage\JobExecutionStorageInterface;
@@ -15,7 +16,7 @@
1516
* - {@see ImportStarWarsSpecieJob} : import species
1617
* - {@see ImportStarWarsCharacterJob} : import characters
1718
*/
18-
final class ImportStarWarsJob extends JobWithChildJobs implements JobWithStaticNameInterface
19+
final class ImportStarWarsJob extends AbstractDecoratedJob implements JobWithStaticNameInterface
1920
{
2021
public static function getJobName(): string
2122
{
@@ -24,11 +25,13 @@ public static function getJobName(): string
2425

2526
public function __construct(JobExecutionStorageInterface $executionStorage, JobExecutor $jobExecutor)
2627
{
27-
parent::__construct($executionStorage, $jobExecutor, [
28-
// in that case, job order matters
29-
ImportStarWarsPlanetJob::getJobName(),
30-
ImportStarWarsSpecieJob::getJobName(),
31-
ImportStarWarsCharacterJob::getJobName(),
32-
]);
28+
parent::__construct(
29+
new JobWithChildJobs($executionStorage, $jobExecutor, [
30+
// in that case, job order matters
31+
ImportStarWarsPlanetJob::getJobName(),
32+
ImportStarWarsSpecieJob::getJobName(),
33+
ImportStarWarsCharacterJob::getJobName(),
34+
])
35+
);
3336
}
3437
}

0 commit comments

Comments
 (0)