171 lines
5.4 KiB
PHP
171 lines
5.4 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
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\Storage;
|
||
use Illuminate\Support\Facades\Artisan;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Str;
|
||
|
||
class CacheBladeViewJob implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
/**
|
||
* The number of seconds the job can run before timing out.
|
||
*
|
||
* @var int
|
||
*/
|
||
public $timeout = 60 * 60; // 1 hour
|
||
|
||
/**
|
||
* The number of times the job may be attempted.
|
||
*
|
||
* @var int
|
||
*/
|
||
public $tries = 3;
|
||
|
||
/**
|
||
* The maximum number of unhandled exceptions to allow before failing.
|
||
*
|
||
* @var int
|
||
*/
|
||
public $maxExceptions = 1;
|
||
|
||
protected $viewPath;
|
||
protected $cacheFileName;
|
||
public $uniqueId;
|
||
|
||
/**
|
||
* Create a new job instance.
|
||
*/
|
||
public function __construct(string $viewPath, string $cacheFileName)
|
||
{
|
||
$this->viewPath = $viewPath;
|
||
$this->cacheFileName = $cacheFileName;
|
||
|
||
// 1. Bu iş için benzersiz bir ID oluştur
|
||
$this->uniqueId = Str::uuid()->toString();
|
||
|
||
// 2. Bu viewPath ve cacheFileName için "geçerli en son iş" olarak bunu işaretle
|
||
// Cache süresi job timeout süresinden biraz fazla olsun
|
||
$lockKey = "job_lock_{$viewPath}_{$cacheFileName}";
|
||
Cache::put($lockKey, $this->uniqueId, 3660);
|
||
|
||
// 3. Öncelikli kuyruğa ata
|
||
$this->onQueue('cache');
|
||
}
|
||
|
||
/**
|
||
* Execute the job.
|
||
*/
|
||
public function handle(): void
|
||
{
|
||
ini_set('memory_limit', '-1');
|
||
set_time_limit(0);
|
||
|
||
$lockKey = "job_lock_{$this->viewPath}_{$this->cacheFileName}";
|
||
|
||
// 4. KONTROL: Cache'deki son ID benimki mi?
|
||
// Eğer benden sonra başka bir job geldiyse, cache'deki ID değişmiştir.
|
||
$latestJobId = Cache::get($lockKey);
|
||
|
||
if ($latestJobId !== $this->uniqueId) {
|
||
Log::info("CacheBladeViewJob CANCELLED: Newer job detected for {$this->viewPath} -> {$this->cacheFileName}", [
|
||
'my_id' => $this->uniqueId,
|
||
'latest_id' => $latestJobId, // null ise cache silinmiş veya süresi dolmuş demektir
|
||
'lock_key' => $lockKey
|
||
]);
|
||
return; // İşlemi sessizce bitir
|
||
}
|
||
|
||
$startTime = microtime(true);
|
||
|
||
Log::info("=== CacheBladeViewJob STARTED ===", [
|
||
'view_path' => $this->viewPath,
|
||
'cache_file_name' => $this->cacheFileName,
|
||
'timestamp' => now(),
|
||
'memory_start' => memory_get_usage(true) / 1024 / 1024 . ' MB'
|
||
]);
|
||
|
||
try {
|
||
// Cache klasörünü oluştur (yoksa)
|
||
if (!Storage::exists('cache')) {
|
||
Storage::makeDirectory('cache');
|
||
}
|
||
|
||
// Render öncesi tekrar kontrol (Uzun sürebileceği için)
|
||
if (Cache::get($lockKey) !== $this->uniqueId) {
|
||
Log::info("CacheBladeViewJob CANCELLED (Pre-render): Newer job detected", [
|
||
'view_path' => $this->viewPath,
|
||
'cache_file_name' => $this->cacheFileName
|
||
]);
|
||
return;
|
||
}
|
||
|
||
// View içeriğini oluştur
|
||
$viewContent = view($this->viewPath)->render();
|
||
|
||
// Cache dosyasını kaydet
|
||
Storage::put("cache/{$this->cacheFileName}.blade.php", $viewContent);
|
||
|
||
// Son güncelleme zamanını kaydet
|
||
Storage::put("cache/{$this->cacheFileName}_last_updated.txt", date('Y-m-d H:i:s'));
|
||
|
||
// Cache'leri temizle - DİKKAT: cache:clear komutu çalışan diğer jobların lock bilgilerini sildiği için kaldırıldı.
|
||
// Artisan::call('cache:clear');
|
||
// Artisan::call('config:clear');
|
||
Artisan::call('view:clear');
|
||
|
||
$endTime = microtime(true);
|
||
$executionTime = $endTime - $startTime;
|
||
$memoryUsage = memory_get_peak_usage(true) / 1024 / 1024;
|
||
|
||
Log::info("=== CacheBladeViewJob COMPLETED ===", [
|
||
'view_path' => $this->viewPath,
|
||
'cache_file_name' => $this->cacheFileName,
|
||
'execution_time_seconds' => round($executionTime, 2),
|
||
'memory_peak_mb' => round($memoryUsage, 2),
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
} catch (\Exception $e) {
|
||
Log::error("CacheBladeViewJob FAILED", [
|
||
'view_path' => $this->viewPath,
|
||
'cache_file_name' => $this->cacheFileName,
|
||
'error_message' => $e->getMessage(),
|
||
'error_trace' => $e->getTraceAsString(),
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
throw $e;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Get the job's description for Telescope.
|
||
*/
|
||
public function getDescription(): string
|
||
{
|
||
return "Cache Blade View: {$this->viewPath} -> {$this->cacheFileName}";
|
||
}
|
||
|
||
/**
|
||
* Get the tags that should be assigned to the job.
|
||
*/
|
||
public function tags(): array
|
||
{
|
||
return [
|
||
"CacheBladeView",
|
||
"View:{$this->viewPath}",
|
||
"Cache:{$this->cacheFileName}"
|
||
];
|
||
}
|
||
}
|