@@ -10,7 +10,13 @@ var _Errors = require("./error/Errors.js");
1010var _smqp = require ( "smqp" ) ;
1111const kOnMessage = Symbol . for ( 'onMessage' ) ;
1212const kExecution = Symbol . for ( 'execution' ) ;
13- function Formatter ( element , formatQ ) {
13+ const EXEC_ROUTING_KEY = 'run._formatting.exec' ;
14+
15+ /**
16+ * Message formatter used to enrich an element run message before continuing to the next run message
17+ * @param {import('types').ElementBase } element
18+ */
19+ function Formatter ( element ) {
1420 const {
1521 id,
1622 broker,
@@ -19,16 +25,19 @@ function Formatter(element, formatQ) {
1925 this . id = id ;
2026 this . broker = broker ;
2127 this . logger = logger ;
22- this . formatQ = formatQ ;
2328 this [ kOnMessage ] = this . _onMessage . bind ( this ) ;
2429}
30+
31+ /**
32+ * Format message
33+ * @param {import('types').MessageElement } message
34+ * @param {CallableFunction } callback
35+ */
2536Formatter . prototype . format = function format ( message , callback ) {
2637 const correlationId = this . _runId = ( 0 , _shared . getUniqueId ) ( message . fields . routingKey ) ;
2738 const consumerTag = '_formatter-' + correlationId ;
28- const formatQ = this . formatQ ;
29- formatQ . queueMessage ( {
30- routingKey : '_formatting.exec'
31- } , { } , {
39+ const broker = this . broker ;
40+ broker . publish ( 'format' , EXEC_ROUTING_KEY , { } , {
3241 correlationId,
3342 persistent : false
3443 } ) ;
@@ -37,11 +46,11 @@ Formatter.prototype.format = function format(message, callback) {
3746 formatKey : message . fields . routingKey ,
3847 runMessage : ( 0 , _messageHelper . cloneMessage ) ( message ) ,
3948 callback,
40- pending : [ ] ,
49+ pending : new Set ( ) ,
4150 formatted : false ,
4251 executeMessage : null
4352 } ;
44- formatQ . consume ( this [ kOnMessage ] , {
53+ broker . consume ( 'format-run-q' , this [ kOnMessage ] , {
4554 consumerTag,
4655 prefetch : 100
4756 } ) ;
@@ -53,41 +62,33 @@ Formatter.prototype._onMessage = function onMessage(routingKey, message) {
5362 pending,
5463 executeMessage
5564 } = this [ kExecution ] ;
56- const asyncFormatting = pending . length ;
57- switch ( routingKey ) {
58- case '_formatting.exec' :
59- if ( message . properties . correlationId !== correlationId ) return message . ack ( ) ;
60- if ( ! asyncFormatting ) {
61- message . ack ( ) ;
62- return this . _complete ( message ) ;
63- }
64- this [ kExecution ] . executeMessage = message ;
65- break ;
66- default :
67- {
68- message . ack ( ) ;
69- const endRoutingKey = message . content ?. endRoutingKey ;
70- if ( endRoutingKey ) {
71- this . _decorate ( message . content ) ;
72- pending . push ( message ) ;
73- return this . _debug ( `start formatting ${ formatKey } message content with formatter ${ routingKey } ` ) ;
74- }
75- if ( asyncFormatting ) {
76- const {
77- isError,
78- message : startMessage
79- } = this . _popFormatStart ( pending , routingKey ) ;
80- if ( startMessage ) startMessage . ack ( ) ;
81- if ( isError ) {
82- return this . _complete ( message , true ) ;
83- }
84- }
85- this . _decorate ( message . content ) ;
86- this . _debug ( `format ${ message . fields . routingKey } message content with formatter ${ routingKey } ` ) ;
87- if ( executeMessage && asyncFormatting && ! pending . length ) {
88- this . _complete ( message ) ;
89- }
65+ const asyncFormatting = pending . size ;
66+ if ( routingKey === EXEC_ROUTING_KEY ) {
67+ if ( message . properties . correlationId !== correlationId ) return message . ack ( ) ;
68+ message . ack ( ) ;
69+ if ( ! asyncFormatting ) {
70+ return this . _complete ( message ) ;
71+ }
72+ this [ kExecution ] . executeMessage = message ;
73+ } else {
74+ message . ack ( ) ;
75+ const endRoutingKey = message . content ?. endRoutingKey ;
76+ if ( endRoutingKey ) {
77+ this . _enrich ( message . content ) ;
78+ pending . add ( message ) ;
79+ return this . _debug ( `start formatting ${ formatKey } message content with formatter ${ routingKey } ` ) ;
80+ }
81+ if ( asyncFormatting ) {
82+ const isError = this . _popFormatStart ( pending , routingKey ) . isError ;
83+ if ( isError ) {
84+ return this . _complete ( message , true ) ;
9085 }
86+ }
87+ this . _enrich ( message . content ) ;
88+ this . _debug ( `format ${ message . fields . routingKey } message content with formatter ${ routingKey } ` ) ;
89+ if ( executeMessage && ! pending . size ) {
90+ this . _complete ( message ) ;
91+ }
9192 }
9293} ;
9394Formatter . prototype . _complete = function complete ( message , isError ) {
@@ -109,7 +110,7 @@ Formatter.prototype._complete = function complete(message, isError) {
109110 }
110111 return callback ( null , runMessage . content , formatted ) ;
111112} ;
112- Formatter . prototype . _decorate = function decorate ( withContent ) {
113+ Formatter . prototype . _enrich = function enrich ( withContent ) {
113114 const content = this [ kExecution ] . runMessage . content ;
114115 for ( const key in withContent ) {
115116 switch ( key ) {
@@ -134,20 +135,19 @@ Formatter.prototype._decorate = function decorate(withContent) {
134135 }
135136} ;
136137Formatter . prototype . _popFormatStart = function popFormattingStart ( pending , routingKey ) {
137- for ( let idx = 0 ; idx < pending . length ; idx ++ ) {
138- const msg = pending [ idx ] ;
138+ for ( const msg of pending ) {
139139 const {
140140 endRoutingKey,
141141 errorRoutingKey = '#.error'
142142 } = msg . content ;
143143 if ( endRoutingKey && ( 0 , _smqp . getRoutingKeyPattern ) ( endRoutingKey ) . test ( routingKey ) ) {
144144 this . _debug ( `completed formatting ${ msg . fields . routingKey } message content with formatter ${ routingKey } ` ) ;
145- pending . splice ( idx , 1 ) ;
145+ pending . delete ( msg ) ;
146146 return {
147147 message : msg
148148 } ;
149149 } else if ( ( 0 , _smqp . getRoutingKeyPattern ) ( errorRoutingKey ) . test ( routingKey ) ) {
150- pending . splice ( idx , 1 ) ;
150+ pending . delete ( msg ) ;
151151 return {
152152 isError : true ,
153153 message : msg
0 commit comments