SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Synchroniser ses
applis simplement, avec
akeneo/batch
Grégory Planchat
@gplanchat
Akeneo, c’est quoi ?
L’intégration logicielle,
c’est faire des
imports et exports de fichiers
Je suis pas venu ici pour souffrir, OK ?
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
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
Utilisation
• Import de données
• Export de données
• Actions de masse
• …
$batchJob = new Job(
‘inventory_job',
$eventDispatcher,
$jobRepository,
[
new ItemStep(
‘inventory_step',
$eventDispatcher,
$jobRepository,
new InventoryFileReader(),
new InventoryImportProcessor(),
new InventoryDatabaseWriter()
),
]
);
A quoi ressemble un Job ?
$execution = new JobExecution();
$execution->setJobParameters(
new JobParameters(
[
'cachePath' => $filename,
]
)
);
$batchJob->execute($execution);
Comment lancer un job ?
Fonctionnement
Run Tier Job Tier Step Tier Data Tier
$batchJob = new Job(
‘foo_job',
$eventDispatcher,
new JobRepository(),
[
new ItemStep(
...
),
new ItemStep(
...
),
new MyAwesomeNotificationStep(
...
),
new RandomStep(
...
),
]
);
Pas de limitation de Step
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
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
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
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
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
$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
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
Traitement d’Item
Traitement d’Item, la lecture
Traitement d’Item, l’écriture
Utilisation avec les itérateurs
• simplifie la lecture
• nombreuses classes dans la SPL
• compatibilité avec de nombreux paquets
class IteratorReader implements
ItemReaderInterface,
StepExecutionAwareInterface,
InitializableInterface
{
public function setStepExecution(StepExecution $stepExecution)
{
$this->stepExecution = $stepExecution;
}
public function initialize()
{
$this->iterator = new FooIterator(...);
$this->iterator->rewind();
}
public function read()
{
...
}
}
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;
}
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
Défauts
• Pas d’exécution conditionnelle
• Pas de parallélisation native
Comment conditionner
l’exécution ?
• utiliser une chaine de responsabilité
• décorer une classe Step
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
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)
Merci
Grégory Planchat
@gplanchat

Contenu connexe

Tendances

Plpython et Triggers
Plpython et TriggersPlpython et Triggers
Plpython et TriggersAffinitic
 
Node.jsエンジニアErlangに入門するの巻
Node.jsエンジニアErlangに入門するの巻Node.jsエンジニアErlangに入門するの巻
Node.jsエンジニアErlangに入門するの巻Recruit Technologies
 
Node.jsエンジニア Erlangに入門するの巻
Node.jsエンジニア Erlangに入門するの巻Node.jsエンジニア Erlangに入門するの巻
Node.jsエンジニア Erlangに入門するの巻Recruit Technologies
 
La programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlLa programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlStéphane Legrand
 
Atelier WordPress: Création d’extension WordPress
Atelier WordPress: Création d’extension WordPressAtelier WordPress: Création d’extension WordPress
Atelier WordPress: Création d’extension WordPressIZZA Samir
 
CocoaHeads Rennes #13 : Magical Record
CocoaHeads Rennes #13 : Magical RecordCocoaHeads Rennes #13 : Magical Record
CocoaHeads Rennes #13 : Magical RecordCocoaHeadsRNS
 
Top 10 chausse trappes
Top 10 chausse trappesTop 10 chausse trappes
Top 10 chausse trappesDamien Seguy
 
Automatisez vos tâches répétitives avec Grunt (Blend 2013)
Automatisez vos tâches répétitives avec Grunt (Blend 2013)Automatisez vos tâches répétitives avec Grunt (Blend 2013)
Automatisez vos tâches répétitives avec Grunt (Blend 2013)Corinne Schillinger
 
Symfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantSymfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantHugo Hamon
 
Solution Linux 2009 - JavaScript
Solution Linux 2009 - JavaScriptSolution Linux 2009 - JavaScript
Solution Linux 2009 - JavaScriptRaphaël Semeteys
 
Le spaceship operator
Le spaceship operatorLe spaceship operator
Le spaceship operatorDarkmira
 
Notions de base de JavaScript
Notions de base de JavaScriptNotions de base de JavaScript
Notions de base de JavaScriptKristen Le Liboux
 
Php mysql cours
Php mysql coursPhp mysql cours
Php mysql courszan
 
Comment créer un moteur de recherche
Comment créer un moteur de rechercheComment créer un moteur de recherche
Comment créer un moteur de rechercheekofficiel
 

Tendances (18)

Plpython et Triggers
Plpython et TriggersPlpython et Triggers
Plpython et Triggers
 
Node.jsエンジニアErlangに入門するの巻
Node.jsエンジニアErlangに入門するの巻Node.jsエンジニアErlangに入門するの巻
Node.jsエンジニアErlangに入門するの巻
 
Node.jsエンジニア Erlangに入門するの巻
Node.jsエンジニア Erlangに入門するの巻Node.jsエンジニア Erlangに入門するの巻
Node.jsエンジニア Erlangに入門するの巻
 
La programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCamlLa programmation fonctionnelle avec le langage OCaml
La programmation fonctionnelle avec le langage OCaml
 
Atelier WordPress: Création d’extension WordPress
Atelier WordPress: Création d’extension WordPressAtelier WordPress: Création d’extension WordPress
Atelier WordPress: Création d’extension WordPress
 
CocoaHeads Rennes #13 : Magical Record
CocoaHeads Rennes #13 : Magical RecordCocoaHeads Rennes #13 : Magical Record
CocoaHeads Rennes #13 : Magical Record
 
Ns python-flask
Ns python-flaskNs python-flask
Ns python-flask
 
Top 10 chausse trappes
Top 10 chausse trappesTop 10 chausse trappes
Top 10 chausse trappes
 
Automatisez vos tâches répétitives avec Grunt (Blend 2013)
Automatisez vos tâches répétitives avec Grunt (Blend 2013)Automatisez vos tâches répétitives avec Grunt (Blend 2013)
Automatisez vos tâches répétitives avec Grunt (Blend 2013)
 
Symfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 PerformantSymfony2 - Un Framework PHP 5 Performant
Symfony2 - Un Framework PHP 5 Performant
 
Solution Linux 2009 - JavaScript
Solution Linux 2009 - JavaScriptSolution Linux 2009 - JavaScript
Solution Linux 2009 - JavaScript
 
Présentation Dashing
Présentation DashingPrésentation Dashing
Présentation Dashing
 
Le spaceship operator
Le spaceship operatorLe spaceship operator
Le spaceship operator
 
Notions de base de JavaScript
Notions de base de JavaScriptNotions de base de JavaScript
Notions de base de JavaScript
 
Tp-jquery
Tp-jqueryTp-jquery
Tp-jquery
 
Php mysql cours
Php mysql coursPhp mysql cours
Php mysql cours
 
Comment créer un moteur de recherche
Comment créer un moteur de rechercheComment créer un moteur de recherche
Comment créer un moteur de recherche
 
Ns python web 1
Ns python web 1Ns python web 1
Ns python web 1
 

Similaire à Synchroniser ses applis simplement avec akeneo/batch

Javascript - Fonctions : que fait ce code ?
Javascript - Fonctions : que fait ce code ?Javascript - Fonctions : que fait ce code ?
Javascript - Fonctions : que fait ce code ?Ruau Mickael
 
Cours yeoman backbone box2d
Cours yeoman backbone box2dCours yeoman backbone box2d
Cours yeoman backbone box2dhugomallet
 
Javascript : fondamentaux et OOP
Javascript : fondamentaux et OOPJavascript : fondamentaux et OOP
Javascript : fondamentaux et OOPJean-Pierre Vincent
 
Node.js, le pavé dans la mare
Node.js, le pavé dans la mareNode.js, le pavé dans la mare
Node.js, le pavé dans la mareValtech
 
Spring Batch 17-05-2011
Spring Batch 17-05-2011Spring Batch 17-05-2011
Spring Batch 17-05-2011Normandy JUG
 
Quoi de neuf dans Zend Framework 1.10 ?
Quoi de neuf dans Zend Framework 1.10 ?Quoi de neuf dans Zend Framework 1.10 ?
Quoi de neuf dans Zend Framework 1.10 ?Mickael Perraud
 
GWT : under the hood
GWT : under the hoodGWT : under the hood
GWT : under the hoodsvuillet
 
Open close principle, on a dit étendre, pas extends !
Open close principle, on a dit étendre, pas extends !Open close principle, on a dit étendre, pas extends !
Open close principle, on a dit étendre, pas extends !Engineor
 
Comprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractibleComprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractibleThierry Gayet
 
Firefox OS, le web de demain - Epita - 2014-06-06
Firefox OS, le web de demain - Epita - 2014-06-06Firefox OS, le web de demain - Epita - 2014-06-06
Firefox OS, le web de demain - Epita - 2014-06-06Frédéric Harper
 
Performance et optimisation de PrestaShop
Performance et optimisation de PrestaShopPerformance et optimisation de PrestaShop
Performance et optimisation de PrestaShopPrestaShop
 
Gitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueGitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueVincent Composieux
 
Tableau objetjava
Tableau objetjavaTableau objetjava
Tableau objetjavaMoez Moezm
 
Nouveautés JavaScript dans le monde Microsoft
Nouveautés JavaScript dans le monde MicrosoftNouveautés JavaScript dans le monde Microsoft
Nouveautés JavaScript dans le monde Microsoftdavrous
 

Similaire à Synchroniser ses applis simplement avec akeneo/batch (20)

Javascript - Fonctions : que fait ce code ?
Javascript - Fonctions : que fait ce code ?Javascript - Fonctions : que fait ce code ?
Javascript - Fonctions : que fait ce code ?
 
De legacy à symfony
De legacy à symfonyDe legacy à symfony
De legacy à symfony
 
Cours yeoman backbone box2d
Cours yeoman backbone box2dCours yeoman backbone box2d
Cours yeoman backbone box2d
 
iTunes Stats
iTunes StatsiTunes Stats
iTunes Stats
 
Javascript : fondamentaux et OOP
Javascript : fondamentaux et OOPJavascript : fondamentaux et OOP
Javascript : fondamentaux et OOP
 
Node.js, le pavé dans la mare
Node.js, le pavé dans la mareNode.js, le pavé dans la mare
Node.js, le pavé dans la mare
 
Spring Batch 17-05-2011
Spring Batch 17-05-2011Spring Batch 17-05-2011
Spring Batch 17-05-2011
 
Quoi de neuf dans Zend Framework 1.10 ?
Quoi de neuf dans Zend Framework 1.10 ?Quoi de neuf dans Zend Framework 1.10 ?
Quoi de neuf dans Zend Framework 1.10 ?
 
GWT : under the hood
GWT : under the hoodGWT : under the hood
GWT : under the hood
 
Open close principle, on a dit étendre, pas extends !
Open close principle, on a dit étendre, pas extends !Open close principle, on a dit étendre, pas extends !
Open close principle, on a dit étendre, pas extends !
 
Comprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractibleComprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractible
 
Les Activités.pdf
Les Activités.pdfLes Activités.pdf
Les Activités.pdf
 
Firefox OS, le web de demain - Epita - 2014-06-06
Firefox OS, le web de demain - Epita - 2014-06-06Firefox OS, le web de demain - Epita - 2014-06-06
Firefox OS, le web de demain - Epita - 2014-06-06
 
Performance et optimisation de PrestaShop
Performance et optimisation de PrestaShopPerformance et optimisation de PrestaShop
Performance et optimisation de PrestaShop
 
Gitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueGitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement Continue
 
Spring Batch
Spring BatchSpring Batch
Spring Batch
 
Tableau objetjava
Tableau objetjavaTableau objetjava
Tableau objetjava
 
Paris js
Paris jsParis js
Paris js
 
Paris RailsCamp 2009
Paris RailsCamp 2009Paris RailsCamp 2009
Paris RailsCamp 2009
 
Nouveautés JavaScript dans le monde Microsoft
Nouveautés JavaScript dans le monde MicrosoftNouveautés JavaScript dans le monde Microsoft
Nouveautés JavaScript dans le monde Microsoft
 

Synchroniser ses applis simplement avec akeneo/batch

  • 1. Synchroniser ses applis simplement, avec akeneo/batch Grégory Planchat @gplanchat
  • 3. L’intégration logicielle, c’est faire des imports et exports de fichiers Je suis pas venu ici pour souffrir, OK ?
  • 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
  • 6. Utilisation • Import de données • Export de données • Actions de masse • …
  • 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 ?
  • 9. Fonctionnement Run Tier Job Tier Step Tier Data Tier
  • 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
  • 21. Utilisation avec les itérateurs • simplifie la lecture • nombreuses classes dans la SPL • compatibilité avec de nombreux paquets
  • 22. class IteratorReader implements ItemReaderInterface, StepExecutionAwareInterface, InitializableInterface { public function setStepExecution(StepExecution $stepExecution) { $this->stepExecution = $stepExecution; } public function initialize() { $this->iterator = new FooIterator(...); $this->iterator->rewind(); } public function read() { ... } }
  • 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
  • 25. Défauts • Pas d’exécution conditionnelle • Pas de parallélisation native
  • 26. Comment conditionner l’exécution ? • utiliser une chaine de responsabilité • décorer une classe Step
  • 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)