Añade un heartbeat de Verifagent a un script Node.js
Fetch nativo basta. Haz ping al final de una ejecución correcta para que caídas y errores lanzados nunca envíen heartbeat.
Antes de empezar
Crea un monitor con la frecuencia real de tu workflow y copia su URL desde el panel. Su token es secreto: no lo publiques.
Configuración
Copia la URL del monitor
Crea un monitor para la cadencia de este job. Guarda la URL en VERIFAGENT_URL (o pásala como argumento).
https://verifagent.com/api/ping/your-ping-tokenPing solo tras el éxito
Requiere Node 18+. Espera el trabajo real, luego fetch. Las respuestas HTTP no OK lanzan error para que un ping roto sea visible en los logs.
// Node.js 18+
const PING_URL = process.env.VERIFAGENT_URL;
if (!PING_URL) {
throw new Error("Missing VERIFAGENT_URL");
}
async function doWork() {
// Sustituye por tu job
}
async function ping() {
const response = await fetch(PING_URL, { method: "GET" });
if (!response.ok) {
throw new Error(`Ping failed: HTTP ${response.status}`);
}
const body = await response.json();
console.log(body); // { ok: true }
}
async function main() {
await doWork();
await ping();
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
// Sin ping en caso de fallo
});Variante POST opcional
GET basta para un heartbeat. POST funciona igual si lo prefieres.
const response = await fetch(PING_URL, { method: "POST" });
if (!response.ok) throw new Error(`Ping failed: HTTP ${response.status}`);Ejecuta una vez
VERIFAGENT_URL='…' node job.mjs — espera { ok: true } y Up en el panel.
{"ok":true}Rutas disponibles
La URL base basta para un heartbeat. Las variantes enriquecen el historial si tu plataforma tiene varias ramas.
GET https://YOUR_PING_URLPOST https://YOUR_PING_URL/successPOST https://YOUR_PING_URL/fail?msg=ErrorVerificar
Ejecución correcta → Up. Lanza error antes de ping() → sin señal; Verifagent abre un incidente tras periodo + margen.
200 {"ok":true}. El monitor pasa de Pending a Up.