Actualizar worker-homeserve.js

This commit is contained in:
2026-03-05 22:22:08 +00:00
parent 93efaa976a
commit 7e3393f5e4

View File

@@ -1,8 +1,8 @@
// worker-homeserve.js (Versión PostgreSQL - MULTI-EMPRESA SAAS)
'use strict';
// worker-homeserve.js (Versión PostgreSQL - MULTI-EMPRESA SAAS - ES Modules)
import { chromium } from 'playwright';
import pg from 'pg';
const { chromium } = require('playwright');
const { Pool } = require('pg');
const { Pool } = pg;
// --- CONFIGURACIÓN ---
const CONFIG = {
@@ -51,7 +51,7 @@ async function getHomeServeCreds(ownerId) {
}
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');
return { user, pass };
@@ -177,7 +177,6 @@ async function loginAndProcess(page, creds, jobData) {
// --- EL CEREBRO: LECTURA DE LA COLA EN POSTGRESQL ---
async function pollQueue() {
try {
// Buscamos 1 solo trabajo pendiente y lo bloqueamos para que no lo cojan otros robots si abres varios
const res = await pool.query(`
UPDATE robot_queue
SET status = 'RUNNING', updated_at = NOW()
@@ -199,27 +198,22 @@ async function pollQueue() {
console.log(`========================================`);
try {
// 🔴 PASAMOS EL OWNER_ID PARA SACAR LAS CREDENCIALES CORRECTAS
const creds = await getHomeServeCreds(job.owner_id);
await withBrowser(async (page) => {
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]);
console.log(`✅ TRABAJO #${job.id} COMPLETADO CON ÉXITO.\n`);
} 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);
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);
} else {
// Dormimos y volvemos a mirar
setTimeout(pollQueue, CONFIG.POLL_INTERVAL_MS);
}
} catch (e) {