// amqp-ping — a tiny demo consumer of the mesh `amqp` interface, run as a long-lived container by // `mesh-tools run` (it never returns, so the container stays up). It exists to PROVE the grant end to // end: the mesh gave it a scoped login and a vhost of that name on the lavinmq provider, and this // connects with exactly those and round-trips a message. // // The connection facts arrive the way every consumer's do — the mesh writes them into an env-file the // container reads (novox/hq ADR 0048): MESH_AMQP_HOST/PORT from the binding, MESH_AMQP_USER and // MESH_AMQP_VHOST both from `${bound:amqp:as}` (the provider named the vhost after the login, so the // consumer uses the login for both — the db-name lesson applied to AMQP), and MESH_AMQP_PASSWORD from // `${secret:amqp}`. // // It retries: on first boot the provider may not have provisioned this consumer yet (the reconcile is // asynchronous and cross-container), so a refused or unreachable connection is a "not yet", not a // failure — it waits and tries again until the round-trip succeeds, then holds the connection idle // and re-pings on a slow cadence so the container is a stable, running proof. import { connFromEnv, roundTrip } from "./client.js"; async function sleep(ms: number): Promise { await new Promise((r) => setTimeout(r, ms)); } async function pingOnce(): Promise { try { const conn = connFromEnv(); const sent = `ping-${Date.now()}`; const got = await roundTrip(conn, "amqp-ping", sent); if (got === sent) { console.log(`[amqp-ping] round-trip ok as ${conn.user} on vhost ${conn.vhost} (${conn.host}:${conn.port})`); return true; } console.error(`[amqp-ping] round-trip mismatch: sent ${sent}, got ${got}`); return false; } catch (err) { console.error(`[amqp-ping] not ready yet: ${err instanceof Error ? err.message : err}`); return false; } } // Wait for the first successful round-trip — the proof this consumer's grant works — then stay up. let first = false; for (let i = 0; !first; i++) { first = await pingOnce(); if (!first) await sleep(3000); } console.log("[amqp-ping] connected and round-tripped; holding steady"); for (;;) { await sleep(30000); await pingOnce(); }