Review funkt
This commit is contained in:
@@ -1,36 +1,67 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { query } from '@/lib/db';
|
||||
import axios from 'axios';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
// 1. Alle aktiven Hotels aus der Datenbank holen
|
||||
const result = await query(
|
||||
'SELECT id, booking_url FROM hotels WHERE is_active = true'
|
||||
);
|
||||
const activeHotels = result.rows;
|
||||
|
||||
const scraperUrl = process.env.SCRAPER_API_URL || 'http://localhost:3000/scrape';
|
||||
const webhookUrl = process.env.APP_WEBHOOK_URL || 'http://localhost:3001/api/webhooks/scraper';
|
||||
|
||||
let triggeredCount = 0;
|
||||
|
||||
// 2. Pro Hotel einen Scrape-Job an den Scraper-Microservice feuern
|
||||
for (const hotel of activeHotels) {
|
||||
await axios.post(scraperUrl, {
|
||||
url: hotel.booking_url,
|
||||
webhookUrl: `${webhookUrl}?hotelId=${hotel.id}` // Hotel-ID als URL-Param übergeben
|
||||
});
|
||||
triggeredCount++;
|
||||
const bullmq = typeof window === 'undefined' ? eval('require')('bullmq') : null;
|
||||
if (!bullmq || !bullmq.Queue) {
|
||||
throw new Error('BullMQ Queue Klasse konnte nicht geladen werden.');
|
||||
}
|
||||
|
||||
const { Queue } = bullmq;
|
||||
|
||||
const connection = {
|
||||
host: process.env.REDIS_HOST || '172.17.0.1',
|
||||
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
||||
connectTimeout: 5000,
|
||||
maxRetriesPerRequest: 3,
|
||||
};
|
||||
|
||||
const scrapeQueue = new Queue('scrape-jobs', { connection });
|
||||
|
||||
const res = await query('SELECT id, booking_url FROM hotels WHERE is_active = true');
|
||||
const activeHotels = res.rows;
|
||||
|
||||
if (activeHotels.length === 0) {
|
||||
await scrapeQueue.close();
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Keine aktiven Hotels zum Scrapen gefunden.',
|
||||
});
|
||||
}
|
||||
|
||||
const n8nBaseUrl = process.env.APP_WEBHOOK_URL || 'https://n8n.prinz-pilaw.de/webhook-test/scrape-reviews';
|
||||
|
||||
for (const hotel of activeHotels) {
|
||||
const n8nWebhookUrl = n8nBaseUrl.includes('?')
|
||||
? `${n8nBaseUrl}&hotelId=${hotel.id}`
|
||||
: `${n8nBaseUrl}?hotelId=${hotel.id}`;
|
||||
|
||||
// Direkt-URL zur Next.js App für Preishistorie
|
||||
const appWebhookUrl = `http://172.17.0.1:3002/api/webhooks/scraper?hotelId=${hotel.id}`;
|
||||
|
||||
await scrapeQueue.add('scrape-hotel', {
|
||||
hotelId: hotel.id,
|
||||
url: hotel.booking_url,
|
||||
appWebhookUrl, // Für Preishistorie in Postgres
|
||||
webhookUrl: n8nWebhookUrl, // Für n8n KI-Reviews
|
||||
});
|
||||
}
|
||||
|
||||
await scrapeQueue.close();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `${triggeredCount} Scrape-Jobs an den Scraper übergeben.`
|
||||
message: `${activeHotels.length} Scrape-Jobs erfolgreich gestartet.`,
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('Trigger Job Error:', error);
|
||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||
console.error('Fehler beim Starten der Jobs:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user