Skip to content

Commit

Permalink
Mark jobs as final once again, but add an abstract decorator that hel…
Browse files Browse the repository at this point in the history
…ps construction (#82)
  • Loading branch information
yann-eugone authored Jun 8, 2023
1 parent f548a96 commit ea92362
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/domain/job.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ The only requirement is implementing [`JobInterface`](../../src/Job/JobInterface
## What types of job exists ?

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

**Jobs from bridges:**
- [CopyFilesJob (`league/flysystem`)](https://github.com/yokai-php/batch-league-flysystem/blob/0.x/src/Job/CopyFilesJob.php):
Expand Down
41 changes: 41 additions & 0 deletions src/Job/AbstractDecoratedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Job;

use Yokai\Batch\JobExecution;

/**
* This {@see JobInterface} is designed to be extended in your project.
* It decorates another {@see JobInterface} that will actually run the code.
* It might be used as a "constructor helper".
*/
abstract class AbstractDecoratedJob implements JobInterface
{
public function __construct(
private JobInterface $job,
) {
}

final public function execute(JobExecution $jobExecution): void
{
$this->preExecute($jobExecution);
$this->job->execute($jobExecution);
$this->postExecute($jobExecution);
}

/**
* Overrides this method if you want to do something before the job is executed.
*/
protected function preExecute(JobExecution $jobExecution): void
{
}

/**
* Overrides this method if you want to do something after the job is executed.
*/
protected function postExecute(JobExecution $jobExecution): void
{
}
}
3 changes: 3 additions & 0 deletions src/Job/Item/ItemJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yokai\Batch\Job\Item;

use Yokai\Batch\Job\AbstractDecoratedJob;
use Yokai\Batch\Job\Item\Exception\SkipItemException;
use Yokai\Batch\Job\JobInterface;
use Yokai\Batch\JobExecution;
Expand All @@ -17,6 +18,8 @@
* Items are Extracted using an {@see ItemReaderInterface}.
* Then Transformed using an {@see ItemProcessorInterface}.
* And finally Loaded using an {@see ItemWriterInterface}.
*
* @final use {@see AbstractDecoratedJob} instead.
*/
class ItemJob implements JobInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/Job/JobWithChildJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/**
* This {@see JobInterface} will execute by triggering child jobs.
* If a child job fails, following child jobs won't be executed.
*
* @final use {@see AbstractDecoratedJob} instead.
*/
class JobWithChildJobs implements JobInterface
{
Expand Down

0 comments on commit ea92362

Please sign in to comment.