File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/app/Jobs/Clients/GenerateClientExportJob.php
<?php
// app/Jobs/Clients/GenerateClientExportJob.php
namespace App\Jobs\Clients;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Exports\ClientsExport;
use App\Core\Customer\CustomerTableService;
use Illuminate\Http\Request;
class GenerateClientExportJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 3600;
public $tries = 3;
protected $requestData;
protected $filename;
protected $userId;
public function __construct($requestData, $filename, $userId)
{
$this->requestData = $requestData;
$this->filename = $filename;
$this->userId = $userId;
}
public function handle(CustomerTableService $customerTableService)
{
try {
$request = new Request($this->requestData);
$report = (new ClientsExport())->setQuery(
$customerTableService->getTableQuery($request)
);
Excel::store($report, $this->filename, 'public');
Cache::put("export_{$this->userId}_{$this->filename}", [
'status' => 'completed',
'filename' => $this->filename,
'url' => asset('storage/' . $this->filename),
], now()->addHours(24));
} catch (\Exception $e) {
logger()->error('Error generando export de clientes', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'filename' => $this->filename,
'user_id' => $this->userId
]);
Cache::put("export_{$this->userId}_{$this->filename}", [
'status' => 'failed',
'error' => 'Error al generar el archivo. Por favor intente nuevamente.'
], now()->addHours(1));
throw $e;
}
}
}