File: /var/www/limestate-admin/app/Jobs/ResetCacheJob.php
<?php
namespace App\Jobs;
use App\Client\LimeWpClient;
use App\Services\ElasticIndexService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Throwable;
class ResetCacheJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
const PHP_PATH = '/usr/bin/php';
const CRONS_PATH = '/var/www/api/crons';
const CACHE_KEY_ADDITIONAL = 'ResetCache_additional';
const CACHE_KEY_LAST_EXECUTION_TIME = 'ResetCache_lastExecutionTime';
public static function resetAdditional(): void
{
Cache::delete(self::CACHE_KEY_ADDITIONAL);
}
private static function setAdditional(string $msg): void
{
Cache::set(self::CACHE_KEY_ADDITIONAL, $msg);
}
public static function getAdditional()
{
return Cache::get(self::CACHE_KEY_ADDITIONAL);
}
private static function updateExecutionTime(): void
{
Cache::set(self::CACHE_KEY_LAST_EXECUTION_TIME, date('Y-m-d H:i:s'));
}
public static function getLastExecutionTime()
{
return Cache::get(self::CACHE_KEY_LAST_EXECUTION_TIME);
}
/**
* Execute the job.
*/
public function handle(ElasticIndexService $elasticIndexService): void
{
self::resetAdditional();
$logger = Log::channel('resetCache');
$log = ['startTime' => date('Y-m-d H:i:s')];
try {
$time = microtime(true);
$elasticIndexService->complexes();
$log['complexTime'] = round(microtime(true) - $time, 2);
$time = microtime(true);
$elasticIndexService->second();
$log['secondsTime'] = round(microtime(true) - $time, 2);
$time = microtime(true);
$elasticIndexService->flats();
$log['flatsTime'] = round(microtime(true) - $time, 2);
$limeWpClient = new LimeWpClient();
if (!$limeWpClient->check()) {
$error = 'Необходимо настроить переменные окружения LIME_WP_HOST и LIME_WP_TOKEN';
$logger->error($error, $log);
self::setAdditional($error);
return;
}
$response = $limeWpClient->clearCache();
if (!$response->successful()) {
$error = 'Код ответа при сбросе кеша: ' . $response->status();
$logger->error($error, $log);
self::setAdditional($error);
return;
}
} catch (Throwable $exception) {
$log['messages'] = $elasticIndexService->messages;
$logger->error($exception->getMessage(), $log);
}
$log['endTime'] = date('Y-m-d H:i:s');
$logger->info('Сброс кеша', $log);
self::updateExecutionTime();
}
}