Actualizar worker-homeserve.js
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
// worker-homeserve.js (Versión PostgreSQL - MULTI-EMPRESA SAAS)
|
// worker-homeserve.js (Versión PostgreSQL - MULTI-EMPRESA SAAS - ES Modules)
|
||||||
'use strict';
|
import { chromium } from 'playwright';
|
||||||
|
import pg from 'pg';
|
||||||
|
|
||||||
const { chromium } = require('playwright');
|
const { Pool } = pg;
|
||||||
const { Pool } = require('pg');
|
|
||||||
|
|
||||||
// --- CONFIGURACIÓN ---
|
// --- CONFIGURACIÓN ---
|
||||||
const CONFIG = {
|
const CONFIG = {
|
||||||
@@ -51,7 +51,7 @@ async function getHomeServeCreds(ownerId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = q.rows[0].username;
|
const user = q.rows[0].username;
|
||||||
// Convierte el Base64 (ej: UGFqYXJpdG8xNCQ=) a texto normal
|
// Convierte el Base64 a texto normal
|
||||||
const pass = Buffer.from(q.rows[0].password_hash, 'base64').toString('utf-8');
|
const pass = Buffer.from(q.rows[0].password_hash, 'base64').toString('utf-8');
|
||||||
|
|
||||||
return { user, pass };
|
return { user, pass };
|
||||||
@@ -177,7 +177,6 @@ async function loginAndProcess(page, creds, jobData) {
|
|||||||
// --- EL CEREBRO: LECTURA DE LA COLA EN POSTGRESQL ---
|
// --- EL CEREBRO: LECTURA DE LA COLA EN POSTGRESQL ---
|
||||||
async function pollQueue() {
|
async function pollQueue() {
|
||||||
try {
|
try {
|
||||||
// Buscamos 1 solo trabajo pendiente y lo bloqueamos para que no lo cojan otros robots si abres varios
|
|
||||||
const res = await pool.query(`
|
const res = await pool.query(`
|
||||||
UPDATE robot_queue
|
UPDATE robot_queue
|
||||||
SET status = 'RUNNING', updated_at = NOW()
|
SET status = 'RUNNING', updated_at = NOW()
|
||||||
@@ -199,27 +198,22 @@ async function pollQueue() {
|
|||||||
console.log(`========================================`);
|
console.log(`========================================`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 🔴 PASAMOS EL OWNER_ID PARA SACAR LAS CREDENCIALES CORRECTAS
|
|
||||||
const creds = await getHomeServeCreds(job.owner_id);
|
const creds = await getHomeServeCreds(job.owner_id);
|
||||||
|
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await loginAndProcess(page, creds, job);
|
await loginAndProcess(page, creds, job);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Si va bien, marcamos como DONE en la BD
|
|
||||||
await pool.query("UPDATE robot_queue SET status = 'DONE', updated_at = NOW() WHERE id = $1", [job.id]);
|
await pool.query("UPDATE robot_queue SET status = 'DONE', updated_at = NOW() WHERE id = $1", [job.id]);
|
||||||
console.log(`✅ TRABAJO #${job.id} COMPLETADO CON ÉXITO.\n`);
|
console.log(`✅ TRABAJO #${job.id} COMPLETADO CON ÉXITO.\n`);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Si falla, guardamos el error en la BD para que el HTML lo muestre
|
|
||||||
console.error(`❌ ERROR EN TRABAJO #${job.id}:`, err.message);
|
console.error(`❌ ERROR EN TRABAJO #${job.id}:`, err.message);
|
||||||
await pool.query("UPDATE robot_queue SET status = 'FAILED', error_msg = $1, updated_at = NOW() WHERE id = $2", [err.message, job.id]);
|
await pool.query("UPDATE robot_queue SET status = 'FAILED', error_msg = $1, updated_at = NOW() WHERE id = $2", [err.message, job.id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buscar el siguiente inmediatamente
|
|
||||||
setTimeout(pollQueue, 1000);
|
setTimeout(pollQueue, 1000);
|
||||||
} else {
|
} else {
|
||||||
// Dormimos y volvemos a mirar
|
|
||||||
setTimeout(pollQueue, CONFIG.POLL_INTERVAL_MS);
|
setTimeout(pollQueue, CONFIG.POLL_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user