KTV/app/Jobs/SendSqliteFileJob.php

55 lines
1.6 KiB
PHP
Raw Normal View History

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
{
$path = storage_path($this->filename);
if (!file_exists($path)) {
Log::error("❌ SQLite 檔案不存在: {$path}");
return;
}
$user = \App\Models\User::find(2);
$token = $user->api_plain_token;
$branches = $this->branchId
? Branch::where('id', $this->branchId)->where('enabled', true)->get()
: Branch::where('enabled', true)->cursor();
foreach ($branches as $branch) {
$client = new ApiClient($token, $branch->external_ip);
$response = $client->upload('/api/upload-sqlite', ['file' => $path]);
if ($response->successful()) {
Log::info("✅ 檔案 {$this->filename} 傳送成功");
} else {
Log::error("❌ 傳送失敗HTTP {$response->status()}");
Log::error($response->body());
}
}
}
}