131 lines
3.3 KiB
PHP
131 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Mail\CodeGenerationEnd;
|
|
use App\Mail\JobFailed;
|
|
use App\Models\Code;
|
|
use App\Models\Settings;
|
|
use Exception;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class ProcessUniqueCode implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* The number of seconds the job can run before timing out.
|
|
*
|
|
* 7200 seconds (2 hours)
|
|
*
|
|
* @var integer
|
|
*/
|
|
public $timeout = 7200;
|
|
|
|
/**
|
|
* Quantity of code to generate
|
|
*
|
|
* @var integer
|
|
*/
|
|
private $quantity;
|
|
|
|
/**
|
|
* Length of code to generate
|
|
*
|
|
* @var integer
|
|
*/
|
|
private $length;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param integer $quantity
|
|
* @param integer $length
|
|
* @return void
|
|
*/
|
|
public function __construct(int $quantity, int $length)
|
|
{
|
|
$this->quantity = $quantity;
|
|
$this->length = $length;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
* TODO: Mettre tout ce bordel dans une transaction quand les serveur de leurs morts auront été mis à jour Uu.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
/** @var \App\Models\Settings $settings */
|
|
$settings = app(Settings::class);
|
|
|
|
$maxSQLPlaceholders = 65535;
|
|
|
|
$insertColumns = collect(DB::getSchemaBuilder()
|
|
->getColumnListing((new Code())->getTable()))
|
|
->filter(function ($name) {
|
|
// * Ignorer les colonnes avec valeurs par défaut sur la table.
|
|
return !in_array($name, ['id', 'code_used', 'code_exported']);
|
|
})->count();
|
|
|
|
/**
|
|
* Nombre d'insertions max sur un seule requète
|
|
*
|
|
* @var integer $perPage
|
|
*/
|
|
$perPage = $maxSQLPlaceholders / $insertColumns;
|
|
|
|
/**
|
|
* Nombre de page totale à insérer.
|
|
*
|
|
* @var integer $totalPages
|
|
*/
|
|
$totalPages = \ceil($this->quantity / $perPage);
|
|
|
|
$totalGeneratedInserts = 0;
|
|
|
|
for ($currentPage = 1; $currentPage <= $totalPages; $currentPage++) {
|
|
$inserts = [];
|
|
$insertToGenerate = min($perPage, $this->quantity - $totalGeneratedInserts);
|
|
do {
|
|
$inserts[] = [
|
|
'date_creation' => now()->format('Y-m-d'),
|
|
];
|
|
} while ($totalGeneratedInserts < $this->quantity and --$insertToGenerate);
|
|
|
|
// * Mass insert
|
|
Code::insert($inserts);
|
|
|
|
$totalGeneratedInserts += count($inserts);
|
|
}
|
|
|
|
// * Fin de la génération.
|
|
Mail::to(config('mail.MAIL_ADMIN'))
|
|
->send(new CodeGenerationEnd($this->quantity, $settings->name));
|
|
}
|
|
|
|
/**
|
|
* In case of job failure
|
|
*
|
|
* @param Exception $exception
|
|
* @return void
|
|
*/
|
|
public function failed(Exception $exception)
|
|
{
|
|
Mail::to(config('mail.MAIL_ADMIN'))
|
|
->send(new JobFailed("ProcessUniqueCode", [
|
|
'message' => $exception->getMessage(),
|
|
'trace' => $exception->getTraceAsString()
|
|
]));
|
|
}
|
|
}
|