4. Un batch, c’est quoi ?
• permet les traitements par lots
• annuler les défauts du code métier :
• performances
• consommation de mémoire
• découpe des gros volumes de données
5. akeneo/batch
• composant apparu dans Akeneo 1.6
• à l’origine, intégré à AkeneoBatchBundle depuis
les premières versions
• désormais découplé du bundle et autonome
• framework-agnostic
7. $batchJob = new Job(
‘inventory_job',
$eventDispatcher,
$jobRepository,
[
new ItemStep(
‘inventory_step',
$eventDispatcher,
$jobRepository,
new InventoryFileReader(),
new InventoryImportProcessor(),
new InventoryDatabaseWriter()
),
]
);
A quoi ressemble un Job ?
8. $execution = new JobExecution();
$execution->setJobParameters(
new JobParameters(
[
'cachePath' => $filename,
]
)
);
$batchJob->execute($execution);
Comment lancer un job ?
10. $batchJob = new Job(
‘foo_job',
$eventDispatcher,
new JobRepository(),
[
new ItemStep(
...
),
new ItemStep(
...
),
new MyAwesomeNotificationStep(
...
),
new RandomStep(
...
),
]
);
Pas de limitation de Step
11. Principes
• Chaque Job peut être décomposé en Step
• 2 familles de Step :
• déclencheur
• traitement
• Le job dispose d’un objet JobParameters qui
transmet des paramètres d’exécution
12. Déclencheur
• lance une action unique, rapide (immédiate) sur un
composant externe au batch
• ne fournit qu’un statut de succès / échec
• Exemples : télécharger un fichier, vider un cache,
envoyer un mail, lancer une commande, etc
13. class ApiCacheFetching extends AbstractStep
{
public function doExecute(StepExecution $stepExecution)
{
$file = fopen(
$stepExecution->getJobParameters()->get(‘cachePath'),
‘w'
);
$api = fopen(‘http://api.example.com/foo?bar=3', 'r');
stream_copy_to_stream($api, $file);
fclose($api);
fclose($file);
}
}
Un Step déclencheur :
télécharger un fichier en cache
14. class ApiCacheCleanup extends AbstractStep
{
public function doExecute(StepExecution $stepExecution)
{
$file = $stepExecution->getJobParameters()
->get('cachePath');
unlink($file);
}
}
Un Step déclencheur :
supprimer le fichier du cache
15. public function doExecute(StepExecution $stepExecution)
{
$builder = new ProcessBuilder(
[
'/usr/bin/env',
'php',
$this->indexerCommandPath,
'--reindex',
$this->index,
]
);
$process = $builder->getProcess();
$process->setTimeout(3600);
$process->run();
if (!$process->isSuccessful()) {
$stepExecution->addFailureException(
new ProcessFailedException($process));
}
$stepExecution->addSummaryInfo('index', $process->getOutput());
}
Un Step déclencheur :
Lancer l’indexation Magento
16. $batchJob = new Job(
‘inventory_job',
$eventDispatcher,
$jobRepository,
[
new ApiCacheFetching(...),
new ItemStep(
‘inventory_step',
$eventDispatcher,
$jobRepository,
new InventoryFileReader(),
new InventoryDefaultProcessor(),
new InventoryDatabaseWriter()
),
new ApiCacheCleanup(...),
]
);
Et le Job dans tout ça ?
On peut ajouter
nos 2 déclencheurs
dans le Job
17. Traitement d’Item
• traite une source de données ligne à ligne
• les données en entrée et en sortie peuvent avoir
des types de stockage différents
• peut rejeter ou ignorer des Item
• fournit un statut de succès / échec
23. public function initialize()
{
$filePath = $this->stepExecution
->getJobParameters()->get('cachePath')
$xml = simplexml_load_file($filePath);
$this->iterator = new ArrayIterator(
$xml->xpath(‘//inventory/stock/virtual')
);
$this->iterator->rewind();
}
public function read()
{
if (!$this->iterator->valid()) {
return null;
}
$this->stepExecution->incrementReadCount();
$item = this->iterator->current();
$this->iterator->next();
return $item;
}
24. Le JobRepository
• instancie les objets JobExecution et StepExecution
• permet de stocker l’état de marche d’une exécution
• permet de reprendre l’exécution sur erreur ou halte
27. Comment paralléliser ?
• Parallélisation verticale
• Lancer plusieurs processus, chacun responsable
d’un Step
• Parallélisation horizontale
• Partitioner les sources, traiter les partitions dans
des processus différents
28. Aller plus loin…
• utiliser un sérialiseur (symfony/serializer)
• utiliser un système de data mapping
• l’intégrer à son framework ou application favori(te)