2025-05-26 16:28:16 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Branch;
|
|
|
|
|
use App\Services\ApiClient;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Queue\{InteractsWithQueue, SerializesModels};
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class SendSqliteFileJob implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
|
|
protected string $filename;
|
|
|
|
|
protected ?int $branchId;
|
|
|
|
|
|
|
|
|
|
public function __construct(string $filename, ?int $branchId = null)
|
|
|
|
|
{
|
|
|
|
|
$this->filename = $filename;
|
|
|
|
|
$this->branchId = $branchId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
2025-05-27 11:56:04 +08:00
|
|
|
|
$path = $this->filename;
|
2025-05-26 16:28:16 +08:00
|
|
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
|
Log::error("❌ SQLite 檔案不存在: {$path}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$user = \App\Models\User::find(2);
|
|
|
|
|
$token = $user->api_plain_token;
|
|
|
|
|
|
|
|
|
|
$branches = $this->branchId
|
2025-05-27 11:56:04 +08:00
|
|
|
|
? Branch::where('id', $this->branchId)->get()
|
|
|
|
|
: Branch::where('enable', true)->cursor();
|
2025-05-26 16:28:16 +08:00
|
|
|
|
|
|
|
|
|
foreach ($branches as $branch) {
|
2025-06-04 09:43:24 +08:00
|
|
|
|
$client = new ApiClient($branch->external_ip , $token );
|
2025-05-26 16:28:16 +08:00
|
|
|
|
$response = $client->upload('/api/upload-sqlite', ['file' => $path]);
|
|
|
|
|
|
|
|
|
|
if ($response->successful()) {
|
2025-05-27 14:54:34 +08:00
|
|
|
|
Log::info("✅ $branch->name 檔案 {$path} 傳送成功");
|
2025-05-26 16:28:16 +08:00
|
|
|
|
} else {
|
2025-05-27 14:54:34 +08:00
|
|
|
|
Log::error("❌ $branch->name 傳送失敗:HTTP {$response->status()}");
|
2025-05-26 16:28:16 +08:00
|
|
|
|
Log::error($response->body());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-27 14:54:34 +08:00
|
|
|
|
if (file_exists($path)) {
|
|
|
|
|
sleep(1); // 小延遲避免未立即釋放
|
|
|
|
|
|
|
|
|
|
if (@unlink($path)) {
|
|
|
|
|
Log::info("🧹 Temp SQLite file deleted: {$path}");
|
|
|
|
|
} else {
|
|
|
|
|
Log::error("❌ 無法刪除 SQLite 檔案:{$path}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-26 16:28:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|