Ticket 739 : Pagination sur ReindexElasticMedia command

This commit is contained in:
Jérémy Munsch 2022-03-23 16:04:24 +01:00
parent 99ae0ac96f
commit 53537ff61e
No known key found for this signature in database
GPG key ID: 55196BEE445F6049
2 changed files with 67 additions and 81 deletions

View file

@ -0,0 +1,67 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Media;
class ReindexElasticMedia extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'medias:reindex';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reindex la base médias dans Elasticsearch';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Reindexation de la base médias dans Elasticsearch...');
$this->info('Test Présence...');
if (Media::indexExists()) {
$this->info('Suppression index...');
Media::deleteIndex();
}
$this->info('Création...');
Media::createIndex();
$pageNumber = 0;
$perPage = 200;
/** @var \Illuminate\Pagination\LengthAwarePaginator $medias */
$medias = Media::paginate($perPage);
$bar = $this->output->createProgressBar(count($medias));
$bar->start($medias->total());
// * LOOP
do {
if ($pageNumber > 0) {
/** @var \Illuminate\Pagination\LengthAwarePaginator $medias */
$medias = Media::paginate($perPage, ['*'], 'page', $pageNumber);
}
foreach ($medias as $media) {
$media->addToIndex();
$bar->advance();
}
++$pageNumber;
} while ($medias->hasMorePages());
$bar->finish();
$this->line('');
$this->info('Fin du traitement');
}
}

View file

@ -1,81 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Media;
class reindexElasticMedia extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'medias:reindex {injectfrom?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reindex la base médias dans Elasticsearch';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Reindexation de la base médias dans Elasticsearch...');
$bReprise = false;
$startFrom = $this->argument('injectfrom');
if($startFrom != "") {
$bReprise = true;
} else {
$startFrom = 1;
}
$this->info('Test Présence...');
if (Media::indexExists() && !$bReprise) {
$this->info('Suppression index...');
Media::deleteIndex();
}
if(!$bReprise) {
$this->info('Création...');
Media::createIndex();
}
$medias = Media::all();
$bar = $this->output->createProgressBar(count($medias));
$bar->start();
if($bReprise)
$bar->advance($startFrom);
$i=1;
foreach ($medias as $media) {
if($i >= $startFrom) {
// ---- index le media dans Elasticsearch
$media->addToIndex();
$bar->advance();
}
unset($media);
$i++;
}
$bar->finish();
$this->line("");
$this->info('Fin du traitement');
}
}