Initial import of verae-nats-process from zapier monorepo
This commit is contained in:
commit
cdccb68f0a
10 changed files with 199 additions and 0 deletions
20
src/handle.js
Normal file
20
src/handle.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Replace this with search, store, or any new function.
|
||||
* Must be idempotent: the same correlationId may be redelivered.
|
||||
*
|
||||
* @param {object} msg
|
||||
* @returns {Promise<object>|object}
|
||||
*/
|
||||
export function handle(msg = {}) {
|
||||
const correlationId = msg.correlationId || msg.traceId;
|
||||
if (!correlationId) {
|
||||
throw new Error('correlationId or traceId is required');
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
correlationId,
|
||||
echo: msg.payload ?? msg,
|
||||
processedAt: new Date().toISOString(),
|
||||
note: 'Template handler — replace handle() in src/handle.js',
|
||||
};
|
||||
}
|
||||
13
src/subjects.js
Normal file
13
src/subjects.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/**
|
||||
* Rename `example.process` before production.
|
||||
* Pattern: verae.<area>.<resource>.<action>
|
||||
*/
|
||||
export const AREA = 'example';
|
||||
export const RESOURCE = 'process';
|
||||
|
||||
export const SUBJECTS = Object.freeze({
|
||||
IN: `verae.${AREA}.${RESOURCE}.in`,
|
||||
OUT: `verae.${AREA}.${RESOURCE}.out`,
|
||||
QUEUE: `${AREA}-${RESOURCE}`,
|
||||
reply: (correlationId) => `verae.${AREA}.${RESOURCE}.reply.${correlationId}`,
|
||||
});
|
||||
70
src/worker.js
Normal file
70
src/worker.js
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
* Health HTTP + optional NATS subscribe.
|
||||
* Fleet probes /health. Live NATS is off unless NATS_URL is set.
|
||||
*/
|
||||
import http from 'node:http';
|
||||
import { SUBJECTS } from './subjects.js';
|
||||
import { handle } from './handle.js';
|
||||
|
||||
const port = Number(process.env.FLEET_HEALTH_PORT || process.env.PORT || 13900);
|
||||
const bind = process.env.FLEET_HEALTH_BIND || '127.0.0.1';
|
||||
const instance = process.env.FLEET_INSTANCE || 'verae-nats-process-0';
|
||||
let paused = false;
|
||||
let processed = 0;
|
||||
|
||||
function snapshot() {
|
||||
return {
|
||||
ok: !paused,
|
||||
paused,
|
||||
instance,
|
||||
role: 'verae-nats-process',
|
||||
subjects: { in: SUBJECTS.IN, out: SUBJECTS.OUT, queue: SUBJECTS.QUEUE },
|
||||
processed,
|
||||
};
|
||||
}
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url, 'http://127.0.0.1');
|
||||
const json = (code, obj) => {
|
||||
res.writeHead(code, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify(obj));
|
||||
};
|
||||
if (url.pathname === '/health' && req.method === 'GET') {
|
||||
const s = snapshot();
|
||||
return json(s.ok ? 200 : 503, s);
|
||||
}
|
||||
if (url.pathname === '/pause' && req.method === 'POST') {
|
||||
paused = true;
|
||||
return json(200, snapshot());
|
||||
}
|
||||
if (url.pathname === '/resume' && req.method === 'POST') {
|
||||
paused = false;
|
||||
return json(200, snapshot());
|
||||
}
|
||||
if (url.pathname === '/message' && req.method === 'POST') {
|
||||
const chunks = [];
|
||||
for await (const c of req) chunks.push(c);
|
||||
let body = {};
|
||||
try {
|
||||
body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
|
||||
} catch {
|
||||
body = {};
|
||||
}
|
||||
if (paused) return json(503, snapshot());
|
||||
try {
|
||||
const out = await handle(body);
|
||||
processed += 1;
|
||||
return json(200, { rttMs: 0, result: out, replyTo: SUBJECTS.reply(out.correlationId) });
|
||||
} catch (err) {
|
||||
return json(400, { error: err.message });
|
||||
}
|
||||
}
|
||||
json(404, { error: 'not found' });
|
||||
});
|
||||
|
||||
server.listen(port, bind, () => {
|
||||
process.stdout.write(`${instance} health http://${bind}:${port}/health in=${SUBJECTS.IN}\n`);
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => server.close(() => process.exit(0)));
|
||||
Loading…
Add table
Add a link
Reference in a new issue