Files
crawlshot/app/Jobs/ProcessCrawlShotJob.php
2025-08-10 21:10:33 +08:00

88 lines
2.5 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\CrawlShotJob;
use App\Services\BrowsershotService;
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\Log;
use Illuminate\Support\Facades\Storage;
class ProcessCrawlShotJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected string $jobUuid;
public function __construct(string $jobUuid)
{
$this->jobUuid = $jobUuid;
}
public function handle(): void
{
$job = CrawlShotJob::where('uuid', $this->jobUuid)->first();
if (!$job) {
Log::error("CrawlShotJob not found: {$this->jobUuid}");
return;
}
try {
$job->update([
'status' => 'processing',
'started_at' => now()
]);
$browsershot = new BrowsershotService();
if ($job->type === 'crawl') {
$result = $browsershot->crawlHtml($job->url, $job->parameters ?? []);
$this->saveCrawlResult($job, $result);
} elseif ($job->type === 'shot') {
$result = $browsershot->takeScreenshot($job->url, $job->parameters ?? []);
$this->saveScreenshotResult($job, $result);
}
$job->update([
'status' => 'completed',
'completed_at' => now()
]);
} catch (\Exception $e) {
Log::error("Job {$this->jobUuid} failed: " . $e->getMessage());
$job->update([
'status' => 'failed',
'error_message' => $e->getMessage(),
'completed_at' => now()
]);
}
}
private function saveCrawlResult(CrawlShotJob $job, string $html): void
{
$filename = "{$job->uuid}.html";
$path = "crawlshot/html/{$filename}";
Storage::put($path, $html);
$job->update(['file_path' => $path]);
}
private function saveScreenshotResult(CrawlShotJob $job, array $result): void
{
$parameters = $job->parameters ?? [];
$format = $parameters['format'] ?? 'jpg';
$filename = "{$job->uuid}.{$format}";
$path = "crawlshot/images/{$filename}";
Storage::put($path, $result['data']);
$job->update(['file_path' => $path]);
}
}