ldc-gen-game-web/app/Jobs/ProcessUniqueCode.php

136 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs;
use App\Mail\CodeGenerationEnd;
use App\Mail\JobFailed;
use App\Models\Code;
use App\Models\Settings;
use Illuminate\Bus\Queueable;
2023-11-23 17:31:28 +01:00
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
2023-11-23 17:31:28 +01:00
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
2023-11-23 17:31:28 +01:00
class ProcessUniqueCode implements ShouldQueue, ShouldBeUnique
{
2021-08-18 17:08:02 +02:00
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;
/**
* Create a new job instance.
*
2023-11-23 17:31:28 +01:00
* @param integer $amount Amount of code to generate.
* @param integer $length Length of each code to generate.
* @return void
*/
2023-11-23 17:31:28 +01:00
public function __construct(protected int $amount, protected int $length)
2021-08-18 17:08:02 +02:00
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2023-11-23 17:31:28 +01:00
DB::transaction(function () {
// ! Le premier code commence à 1
$last_id = (Code::query()->max('id') ?? 0) + 1;
/** @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() + 1;
/**
* Nombre d'insertions max sur un seule requète
*
* @var integer $perPage
*/
$perPage = \floor($maxSQLPlaceholders / $insertColumns);
/**
* Nombre de page totale à insérer.
*
* @var integer $totalPages
*/
$totalPages = \ceil($this->amount / $perPage);
$totalGeneratedInserts = 0;
for ($currentPage = 1; $currentPage <= $totalPages; $currentPage++) {
$inserts = [];
$insertToGenerate = min($perPage, $this->amount - $totalGeneratedInserts);
do {
$inserts[] = [
'id' => $last_id++,
'date_creation' => now()->format('Y-m-d'),
];
} while (($totalGeneratedInserts < $this->amount) && --$insertToGenerate);
// * Mass insert
Code::query()->insert($inserts);
$totalGeneratedInserts += count($inserts);
}
// * Fin de la génération.
Mail::to(config('mail.admin'))
->send(new CodeGenerationEnd($this->amount, $settings->name));
});
}
/**
* In case of job failure
*
2023-11-23 17:31:28 +01:00
* @param \Exception $exception
* @return void
*/
2023-11-23 17:31:28 +01:00
public function failed(\Exception $exception)
{
Mail::to(config('mail.admin'))->send(
new JobFailed('ProcessUniqueCode', [
'message' => $exception->getMessage(),
'trace' => $exception->getTrace()
])
);
}
/**
* Get the middleware the job should pass through.
*
* @return array<int, object>
*/
public function middleware(): array
2021-08-18 17:08:02 +02:00
{
2023-11-23 17:31:28 +01:00
return [
(new WithoutOverlapping('code_generation'))
->releaseAfter(60)
];
}
}