58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Imports\SongDataImport;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
//use Illuminate\Foundation\Queue\Queueable;
|
|
|
|
class ImportSongJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected string $filePath;
|
|
//public $timeout = 36000;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(string $filePath)
|
|
{
|
|
$this->filePath = $filePath;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
//ini_set('memory_limit', '-1'); // 無限記憶體,適合大量匯入
|
|
|
|
try {
|
|
Excel::import(new SongDataImport, $this->filePath);
|
|
|
|
// 匯入成功後再刪檔案
|
|
if (Storage::exists($this->filePath)) {
|
|
Storage::delete($this->filePath);
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
// 寫入錯誤日誌
|
|
\Log::error('ImportSongJob failed: ' . $e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
'file' => $this->filePath
|
|
]);
|
|
|
|
// ❗重要:不要刪除檔案,讓失敗時可以 retry 使用同一份檔案
|
|
throw $e; // 讓 Laravel Queue 系統可以 retry
|
|
}
|
|
}
|
|
}
|