Skip to content

Commit 7a76c06

Browse files
authored
Fill naming jobs best practices documentation (#139)
1 parent a952544 commit 7a76c06

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

docs/docs/best-practices/naming-jobs.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,44 @@ Naming Jobs
44
| *There are only two hard things in Computer Science: cache invalidation and naming things.*
55
| -- Phil Karlton
66
7-
todo
7+
8+
Within the library, ``Jobs`` are identified with a ``name``, inside the ``JobRegistry``.
9+
A name is a simple ``string``, with the only requirement that every job must have a unique one.
10+
11+
12+
Decide on a convention
13+
------------------------------------------------------------
14+
15+
There is no recommandation we can make here, you can use whatever notation you prefer:
16+
17+
* ``ImportUser``
18+
* ``import_user``
19+
* ``import.user``
20+
* or anything else
21+
22+
But we strongly recommend that you pick a convention and try to stick to it.
23+
24+
25+
In a Symfony project
26+
------------------------------------------------------------
27+
28+
| Because this is how the Framework works, ``Jobs`` must be registered as services.
29+
| All services tagged with ``yokai_batch.job`` are collected in a ``CompilerPass``, and given to the ``JobRegistry``.
30+
31+
You can tag manually all your jobs, and fill the job name withing the tag:
32+
33+
.. literalinclude:: naming-jobs/symfony-manual-tag.php
34+
:language: php
35+
36+
You can autoconfigure all services with ``JobInterface`` to be tagged,
37+
and the job name will be the service id (most of the time the FQCN):
38+
39+
.. literalinclude:: naming-jobs/symfony-autowire-tag.yaml
40+
:language: yaml
41+
42+
Finally, if you want all services to be tagged using autoconfigure, but still want to pick job names,
43+
you can implement ``JobWithStaticNameInterface`` in your jobs:
44+
45+
.. literalinclude:: naming-jobs/symfony-interface-tag.php
46+
:language: php
847

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
6+
_instanceof:
7+
Yokai\Batch\Job\JobInterface:
8+
tags: ['yokai_batch.job']
9+
10+
App\:
11+
resource: '../src/'
12+
exclude:
13+
- '../src/DependencyInjection/'
14+
- '../src/Entity/'
15+
- '../src/Kernel.php'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Job;
6+
7+
use Yokai\Batch\Bridge\Symfony\Framework\JobWithStaticNameInterface;
8+
use Yokai\Batch\Job\JobInterface;
9+
use Yokai\Batch\JobExecution;
10+
11+
final class ImportUserJob implements JobInterface, JobWithStaticNameInterface
12+
{
13+
public static function getJobName(): string
14+
{
15+
return 'import.user';
16+
}
17+
18+
public function execute(JobExecution $jobExecution): void
19+
{
20+
// TODO: Implement execute() method.
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Job;
6+
7+
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
8+
use Yokai\Batch\Job\JobInterface;
9+
use Yokai\Batch\JobExecution;
10+
11+
#[AutoconfigureTag('yokai_batch.job')]
12+
final class ImportUserJob implements JobInterface
13+
{
14+
public function execute(JobExecution $jobExecution): void
15+
{
16+
// TODO: Implement execute() method.
17+
}
18+
}

0 commit comments

Comments
 (0)