refactor: rename email-server-worker to email-worker

Rename directory from email-server-worker to email-worker for clarity and brevity. Update all references in CLAUDE.md documentation.
This commit is contained in:
Knee Cola
2025-12-30 10:33:59 +01:00
parent 9d6ad17452
commit 3e4d8fb95c
37 changed files with 32 additions and 28 deletions

View File

@@ -0,0 +1,8 @@
/**
* Za neinicijaliziranu env varijablu vraća default vrijednost
* @param value vrijednost env varijable
* @param defaultValue default vrijednost
* @returns
*/
export const coalesce = (value:string|undefined, defaultValue:string):string => value===undefined ? defaultValue : (value==="" ? defaultValue : value);

View File

@@ -0,0 +1,21 @@
import debug from 'debug';
/**
* Logs to console / stdout
* @param namespace
* @returns instance of Debug
*/
export const createLogger = (namespace:string):debug.Debugger => {
const dbg = debug(namespace);
const rx = /nodemon/gi;
if(rx.test(process.env?.npm_lifecycle_script ?? "")) {
// When started via nodemon:
// forcing the use of console insted of stdout
// -> nodemon doesn't work with stdout
dbg.log = console.log.bind(console);
}
return(dbg);
};

View File

@@ -0,0 +1,50 @@
import { Counter, Histogram, register } from 'prom-client';
import { coalesce } from './initTools';
/** Histogram Buckets */
const PROMETHEUS_HISTOGRAM_BUCKETS = coalesce(process.env.PROMETHEUS_HISTOGRAM_BUCKETS, "0.1, 0.5, 1, 5, 10");
/** Labela kojom želimo da bude označena metrika prikupljena na ovom web servisu */
const PROMETHEUS_APP_LABEL = coalesce(process.env.PROMETHEUS_APP_LABEL, 'email-worker');
// na "app" labele ćemo razdvajanje rezultata u Grafani
register.setDefaultLabels({ app: PROMETHEUS_APP_LABEL });
/**
* Broji koliko je ukupno zahtjeva zaprimljeno za obradu
*/
export const totalRequestCounter = new Counter({
name: "request_operations_total",
help: "ukupan broj zaprimljenih zahtjeva",
/** countere razdvajamo po vrsti zahtjeva */
labelNames: ['path'],
});
/**
* Broji zahtjeve koji su uspješno obrađeni
*/
export const successfulRequestCounter = new Counter({
name: "request_operations_ok",
help: "broj zahtjeva koji su uspješno obrađeni",
/** countere razdvajamo po vrsti zahtjeva */
labelNames: ['path'],
});
/**
* Broji zahtjeve kod čije obrade je došlo do greške
*/
export const failedRequestCounter = new Counter({
name: "request_operations_failed",
help: "broj zahtjeva kod čije obrade je došlo do greške",
/** countere razdvajamo po vrsti zahtjeva i rezultatu izvođenja */
labelNames: ["path", "status"],
});
/** Histogram mjeri koliko traje obrada pristiglog zahtjeva */
export const requestDurationHistogram = new Histogram({
name: "request_duration_seconds",
help: "Trajanje request-a u sekundama",
/** countere razdvajamo po vrsti zahtjeva i rezultatu izvođenja */
labelNames: ["path", "status"],
buckets: PROMETHEUS_HISTOGRAM_BUCKETS?.split(',').map((el) => parseFloat(el))
});

View File

@@ -0,0 +1,19 @@
/**
* This function serializes an error object into a string that can be logged
* @param ex error object
* @returns string
* @description SQL Server may generate more than one error for one request so you can access preceding errors with `err.precedingErrors`, while the `ex` itself is a generic error without any useful information
*/
export const serializeError = (ex:Error | Error & { precedingErrors?:Error[] }):string => {
const { name, message, stack, precedingErrors } = (ex as Error & { precedingErrors?:Error[] });
// SQL Server may generate more than one error for one request so you can access preceding errors with `ex.precedingErrors`,
// while the `ex` itself is a generic error without any useful information
if(precedingErrors) {
return(serializeError(precedingErrors[0]));
}
return `${name}:${message}`;
}