Files
investbrain/app/Jobs/BackupImportJob.php
T

76 lines
1.7 KiB
PHP
Raw Normal View History

2024-10-24 14:48:24 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-10-24 14:48:24 -05:00
namespace App\Jobs;
2025-01-28 17:14:49 -06:00
use App\Imports\BackupImport as BackupImportExcel;
2024-10-24 14:48:24 -05:00
use App\Models\BackupImport;
2025-01-28 17:14:49 -06:00
use App\Models\User;
2024-10-24 16:36:54 -05:00
use App\Notifications\ImportFailedNotification;
2025-01-28 17:14:49 -06:00
use App\Notifications\ImportSucceededNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Maatwebsite\Excel\Facades\Excel;
use Throwable;
2024-10-24 14:48:24 -05:00
class BackupImportJob implements ShouldQueue
{
use Queueable;
/**
* The number of times the job may be attempted.
*/
2025-01-28 17:14:49 -06:00
public $tries = 1;
2024-10-24 14:48:24 -05:00
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 300;
/**
* Indicate if the job should be marked as failed on timeout.
*
* @var bool
*/
public $failOnTimeout = true;
2024-10-24 16:36:54 -05:00
public User $user;
2024-10-24 14:48:24 -05:00
/**
* Create a new job instance.
*/
public function __construct(
public BackupImport $backupImport
2025-01-28 17:14:49 -06:00
) {
2024-10-24 16:36:54 -05:00
$this->user = User::find($this->backupImport->user_id);
}
2024-10-24 14:48:24 -05:00
/**
* Execute the job.
*/
public function handle(): void
2025-01-28 17:14:49 -06:00
{
2024-10-24 14:48:24 -05:00
Excel::import(new BackupImportExcel($this->backupImport), $this->backupImport->path, config('livewire.temporary_file_upload.disk', null));
2024-10-24 16:36:54 -05:00
$this->user->notify(new ImportSucceededNotification);
2024-10-24 14:48:24 -05:00
}
/**
* Handle a job failure.
*/
public function failed(?Throwable $e): void
{
$this->backupImport->update([
'status' => 'failed',
2025-01-28 17:14:49 -06:00
'message' => 'Error: '.substr($e->getMessage(), 0, 220),
2024-10-24 14:48:24 -05:00
'has_errors' => true,
2025-01-28 17:14:49 -06:00
'completed_at' => now(),
2024-10-24 14:48:24 -05:00
]);
2024-10-24 16:36:54 -05:00
$this->user->notify(new ImportFailedNotification($e->getMessage()));
2024-10-24 14:48:24 -05:00
}
}