Naming Jobs

There are only two hard things in Computer Science: cache invalidation and naming things.
– Phil Karlton

Within the library, Jobs are identified with a name, inside the JobRegistry. A name is a simple string, with the only requirement that every job must have a unique one.

Decide on a convention

There is no specific recommandation, you can use whichever notation you prefer, such as:

  • ImportUser

  • import_user

  • import.user

  • or any other format

However, we strongly recommend that you choose a convention and adhere to it consistently.

In a Symfony project

Because of the way the framework works, Jobs must be registered as services.
All services tagged with yokai_batch.job are collected in a CompilerPass, and provided to the JobRegistry.

You can manually tag all your jobs and specify the job name withing the tag:

<?php

declare(strict_types=1);

namespace App\Job;

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Yokai\Batch\Job\JobInterface;
use Yokai\Batch\JobExecution;

#[AutoconfigureTag('yokai_batch.job')]
final class ImportUserJob implements JobInterface
{
    public function execute(JobExecution $jobExecution): void
    {
        // TODO: Implement execute() method.
    }
}

Alternatively, you can autoconfigure all services implementing JobInterface to be tagged. In this case, the job name will be the service ID (usually the FQCN):

services:
    _defaults:
        autowire: true
        autoconfigure: true

    _instanceof:
        Yokai\Batch\Job\JobInterface:
            tags: ['yokai_batch.job']

    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

Finally, if you want to autoconfigure all services while still manually specifying job names, you can implement the JobWithStaticNameInterface in your jobs:

<?php

declare(strict_types=1);

namespace App\Job;

use Yokai\Batch\Bridge\Symfony\Framework\JobWithStaticNameInterface;
use Yokai\Batch\Job\JobInterface;
use Yokai\Batch\JobExecution;

final class ImportUserJob implements JobInterface, JobWithStaticNameInterface
{
    public static function getJobName(): string
    {
        return 'import.user';
    }

    public function execute(JobExecution $jobExecution): void
    {
        // TODO: Implement execute() method.
    }
}