@@ -2,39 +2,67 @@ import type { ConsumeMessage } from "amqplib";
22import { sendToBackend } from "./backend-client" ;
33import { config } from "./config" ;
44import { createSchemaValidators } from "./contracts/schema-validator" ;
5+ import { logger } from "./logger" ;
56import { QueueClient } from "./messaging/queue-client" ;
67import { normalizeRawEvent } from "./normalize" ;
78import type { RawSourceEvent } from "./types" ;
89
910async function bootstrap ( ) : Promise < void > {
10- const queue = new QueueClient ( config . rabbitmqUrl ) ;
11- const validators = createSchemaValidators ( config . sharedContractsDir ) ;
11+ const queue = new QueueClient ( config . RABBITMQ_URL ) ;
12+ const validators = createSchemaValidators ( config . SHARED_CONTRACTS_DIR ) ;
1213
13- await queue . init ( ) ;
14- await queue . assertQueue ( config . queueRaw ) ;
15- await queue . assertQueue ( config . queueNormalized ) ;
14+ await queue . init ( config . PREFETCH ) ;
15+ await queue . assertRetryTopology (
16+ config . QUEUE_RAW_EVENT ,
17+ config . QUEUE_RETRY_EVENT ,
18+ config . QUEUE_DEAD_LETTER_EVENT
19+ ) ;
20+ await queue . assertQueue ( config . QUEUE_NORMALIZED_EVENT ) ;
1621
17- console . log ( "[processing-worker] запущен" , {
18- queueRaw : config . queueRaw ,
19- queueNormalized : config . queueNormalized ,
20- apiGraphqlUrl : config . apiGraphqlUrl
21- } ) ;
22+ logger . info (
23+ {
24+ queueRaw : config . QUEUE_RAW_EVENT ,
25+ queueRetry : config . QUEUE_RETRY_EVENT ,
26+ queueDeadLetter : config . QUEUE_DEAD_LETTER_EVENT ,
27+ queueNormalized : config . QUEUE_NORMALIZED_EVENT ,
28+ apiGraphqlUrl : config . API_GRAPHQL_URL
29+ } ,
30+ "processing-worker started"
31+ ) ;
32+
33+ queue . consume ( config . QUEUE_RAW_EVENT , async ( message : ConsumeMessage ) => {
34+ const attempt = queue . getRetryCount ( message ) ;
35+
36+ try {
37+ const raw = queue . parseMessage < RawSourceEvent > ( message ) ;
38+ validators . validateRaw ( raw ) ;
2239
23- queue . consume ( config . queueRaw , async ( message : ConsumeMessage ) => {
24- const raw = queue . parseMessage < RawSourceEvent > ( message ) ;
25- validators . validateRaw ( raw ) ;
40+ const normalized = normalizeRawEvent ( raw ) ;
41+ validators . validateNormalized ( normalized ) ;
2642
27- const normalized = normalizeRawEvent ( raw ) ;
28- validators . validateNormalized ( normalized ) ;
43+ await queue . publish ( config . QUEUE_NORMALIZED_EVENT , normalized ) ;
44+ await sendToBackend ( config . API_GRAPHQL_URL , config . API_INGEST_TOKEN , normalized ) ;
45+ queue . ack ( message ) ;
2946
30- await queue . publish ( config . queueNormalized , normalized ) ;
31- await sendToBackend ( config . apiGraphqlUrl , normalized ) ;
47+ logger . info (
48+ { eventId : raw . eventId , source : raw . source , externalId : normalized . externalId } ,
49+ "raw event normalized and ingested"
50+ ) ;
51+ } catch ( error ) {
52+ const reason = error instanceof Error ? error . message : "Unknown worker error" ;
53+ if ( attempt < config . RETRY_ATTEMPTS ) {
54+ await queue . retry ( message , config . QUEUE_RETRY_EVENT , attempt + 1 , config . RETRY_BASE_DELAY_MS ) ;
55+ logger . warn ( { err : error , attempt : attempt + 1 } , "message scheduled for retry" ) ;
56+ return ;
57+ }
3258
33- console . log ( `[processing-worker] обработано событие ${ raw . eventId } ` ) ;
59+ await queue . deadLetter ( message , config . QUEUE_DEAD_LETTER_EVENT , reason ) ;
60+ logger . error ( { err : error , attempt } , "message moved to dead-letter queue" ) ;
61+ }
3462 } ) ;
3563}
3664
3765void bootstrap ( ) . catch ( ( error ) => {
38- console . error ( "[ processing-worker] фатальная ошибка" , error ) ;
66+ logger . error ( { err : error } , " processing-worker crashed" ) ;
3967 process . exit ( 1 ) ;
4068} ) ;
0 commit comments