Some checks are pending
offline / test (push) Waiting to run
Account-balance stores display names and looks up by name. Edge writes names on customer create/edit; staff UIs join from edge when needed. New verae-staff-session issues a host cookie; department HTML redirects when STAFF_AUTH=1. JOBS_EVENTS_EXCLUSIVE lets jobs-events own the durable consumer. Catalog index is cards; disabled fleet machines are grey.
75 lines
2.5 KiB
JavaScript
75 lines
2.5 KiB
JavaScript
#!/usr/bin/env node
|
|
/** Mailbox for verae.zapier.jobs.events. Passthrough: count + optional fan-out later. */
|
|
import http from 'node:http';
|
|
import { SUBJECTS } from './subjects.js';
|
|
|
|
const PORT = Number(process.env.PORT || process.env.FLEET_HEALTH_PORT || 3030);
|
|
const BIND = process.env.FLEET_HEALTH_BIND || '0.0.0.0';
|
|
let processed = 0;
|
|
let last = null;
|
|
|
|
async function startNats() {
|
|
const url = process.env.NATS_URL;
|
|
if (!url) return;
|
|
const { connect, StringCodec } = await import('nats');
|
|
const nc = await connect({ servers: url.split(','), name: 'verae-jobs-events' });
|
|
const sc = StringCodec();
|
|
const exclusive = process.env.JOBS_EVENTS_EXCLUSIVE === '1';
|
|
const deliver = async (m) => {
|
|
processed += 1;
|
|
try {
|
|
last = JSON.parse(sc.decode(m.data) || '{}');
|
|
} catch {
|
|
last = {};
|
|
}
|
|
if (exclusive) {
|
|
nc.publish(SUBJECTS.INTERNAL, m.data);
|
|
}
|
|
if (m.reply) m.respond(sc.encode(JSON.stringify({ ok: true, processed, exclusive })));
|
|
if (typeof m.ack === 'function') await m.ack();
|
|
};
|
|
if (exclusive) {
|
|
try {
|
|
const js = nc.jetstream();
|
|
const jsm = await nc.jetstreamManager();
|
|
await jsm.consumers.add('ZAPIER_EVENTS', {
|
|
durable_name: SUBJECTS.DURABLE,
|
|
ack_policy: 'explicit',
|
|
filter_subject: SUBJECTS.IN,
|
|
max_deliver: 10,
|
|
}).catch(() => {});
|
|
const consumer = await js.consumers.get('ZAPIER_EVENTS', SUBJECTS.DURABLE);
|
|
const messages = await consumer.consume({ max_messages: 10 });
|
|
for await (const m of messages) await deliver(m);
|
|
return;
|
|
} catch (err) {
|
|
process.stderr.write(`js exclusive fallback core sub: ${err.message}\n`);
|
|
}
|
|
}
|
|
for await (const m of nc.subscribe(SUBJECTS.IN, { queue: SUBJECTS.QUEUE })) {
|
|
await deliver(m);
|
|
}
|
|
}
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const url = new URL(req.url || '/', `http://127.0.0.1:${PORT}`);
|
|
if (url.pathname === '/health') {
|
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
res.end(JSON.stringify({
|
|
ok: true,
|
|
role: 'verae-jobs-events',
|
|
processed,
|
|
subject: SUBJECTS.IN,
|
|
exclusive: process.env.JOBS_EVENTS_EXCLUSIVE === '1',
|
|
lastEvent: last?.event || null,
|
|
}));
|
|
return;
|
|
}
|
|
res.writeHead(404, { 'content-type': 'application/json' });
|
|
res.end(JSON.stringify({ error: 'not found' }));
|
|
});
|
|
|
|
server.listen(PORT, BIND, () => {
|
|
process.stdout.write(`verae-jobs-events http://${BIND}:${PORT}/\n`);
|
|
});
|
|
startNats().catch((err) => process.stderr.write(`nats optional: ${err.message}\n`));
|