This commit is contained in:
144
api/app/Jobs/AtencaoBasica/ExportacaoJob.php
Normal file
144
api/app/Jobs/AtencaoBasica/ExportacaoJob.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\AtencaoBasica;
|
||||
|
||||
use App\Enums\AtencaoBasica\Exportacao\Status;
|
||||
use App\Events\AtencaoBasica\ExportacaoFinalizadaEvent;
|
||||
use App\Models\AtencaoBasica\Exportacao\ExportacaoArquivo;
|
||||
use App\Services\AtencaoBasica\Exportacao\ExportacaoService;
|
||||
use Carbon\Carbon;
|
||||
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\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Throwable;
|
||||
|
||||
class ExportacaoJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
const BASE_PATH = 'atencao-basica' . DIRECTORY_SEPARATOR . 'exportacao' . DIRECTORY_SEPARATOR;
|
||||
|
||||
public $timeout = 0;
|
||||
public $tries = 1;
|
||||
|
||||
protected $cliente;
|
||||
protected $exportacaoArquivoId;
|
||||
|
||||
public function __construct($cliente, $exportacaoArquivoId)
|
||||
{
|
||||
$this->cliente = $cliente;
|
||||
$this->exportacaoArquivoId = $exportacaoArquivoId;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
set_time_limit(0);
|
||||
|
||||
$currentConnection = config('database.default');
|
||||
|
||||
try {
|
||||
$this->setConnection();
|
||||
|
||||
$exportacaoArquivo = ExportacaoArquivo::find($this->exportacaoArquivoId);
|
||||
|
||||
if (is_null($exportacaoArquivo)) {
|
||||
Log::channel('atencao-basica-exportacao')->error('Registro de exportação não encontrado', [
|
||||
'cliente' => $this->cliente,
|
||||
'origem' => 'ExportacaoJob',
|
||||
'exportacao_arquivo_id' => $this->exportacaoArquivoId,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$exportacaoArquivo->status_id = Status::EM_PROCESSAMENTO;
|
||||
$exportacaoArquivo->data_hora_inicio = Carbon::now();
|
||||
$exportacaoArquivo->save();
|
||||
|
||||
DB::transaction(function () use ($exportacaoArquivo) {
|
||||
$path = self::BASE_PATH . $this->cliente . DIRECTORY_SEPARATOR;
|
||||
$fileName = ExportacaoService::generate($this->cliente, $exportacaoArquivo->competencia, $exportacaoArquivo->id, $exportacaoArquivo->createdLotacao->usuario_id);
|
||||
|
||||
$exportacaoArquivo->status_id = Status::PROCESSADO;
|
||||
|
||||
if (Storage::exists($path . $fileName)) {
|
||||
$exportacaoArquivo->arquivo = $fileName;
|
||||
chmod(Storage::path($path . $fileName), 0775);
|
||||
}
|
||||
|
||||
$exportacaoArquivo->data_hora_fim = Carbon::now();
|
||||
$exportacaoArquivo->save();
|
||||
|
||||
ExportacaoFinalizadaEvent::dispatch($exportacaoArquivo->createdLotacao->usuario_id, $exportacaoArquivo->id, $exportacaoArquivo->arquivo);
|
||||
});
|
||||
} finally {
|
||||
$this->clearConnection();
|
||||
|
||||
Config::set('database.default', $currentConnection);
|
||||
}
|
||||
}
|
||||
|
||||
public function failed(Throwable $exception)
|
||||
{
|
||||
$currentConnection = config('database.default');
|
||||
|
||||
try {
|
||||
$this->setConnection();
|
||||
|
||||
$exportacaoArquivo = ExportacaoArquivo::find($this->exportacaoArquivoId);
|
||||
|
||||
if ($exportacaoArquivo) {
|
||||
$exportacaoArquivo->status_id = Status::ERRO_NO_PROCESSAMENTO;
|
||||
$exportacaoArquivo->data_hora_fim = Carbon::now();
|
||||
$exportacaoArquivo->save();
|
||||
|
||||
$path = self::BASE_PATH . $this->cliente . DIRECTORY_SEPARATOR;
|
||||
$fileName = $exportacaoArquivo->arquivo;
|
||||
|
||||
if (!is_null($fileName) && Storage::exists($path . $fileName)) {
|
||||
Storage::delete($path . $fileName);
|
||||
}
|
||||
}
|
||||
|
||||
Log::channel('atencao-basica-exportacao')->error('Erro ao executar a exportação', [
|
||||
'cliente' => $this->cliente,
|
||||
'origem' => 'ExportacaoJob',
|
||||
'erro' => $exception->getMessage(),
|
||||
]);
|
||||
} finally {
|
||||
$this->clearConnection();
|
||||
|
||||
Config::set('database.default', $currentConnection);
|
||||
}
|
||||
}
|
||||
|
||||
private function setConnection()
|
||||
{
|
||||
Config::set("database.connections.job-{$this->cliente}", [
|
||||
'driver' => 'mysql',
|
||||
'host' => env('QUEUE_DB_HOST'),
|
||||
'port' => env('QUEUE_DB_PORT'),
|
||||
'database' => "conectasus_{$this->cliente}",
|
||||
'username' => env('QUEUE_DB_USERNAME'),
|
||||
'password' => env('QUEUE_DB_PASSWORD'),
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
]);
|
||||
|
||||
Config::set('database.default', "job-{$this->cliente}");
|
||||
|
||||
$this->clearConnection();
|
||||
}
|
||||
|
||||
private function clearConnection()
|
||||
{
|
||||
DB::disconnect("job-{$this->cliente}");
|
||||
DB::purge("job-{$this->cliente}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user