@@ -24,7 +24,6 @@ import {
2424 isEmptyField ,
2525 isInferredIndex ,
2626 isNotEmptyField ,
27- MAX_EVENT_LOOP_PROCESSING_TIME ,
2827 offsetToCursor ,
2928 pascalize ,
3029 READ_DATA_INDICES ,
@@ -222,6 +221,7 @@ import { getPirWithAccessCheck } from '../modules/pir/pir-checkPirAccess';
222221import { asyncFilter , asyncMap , uniqAsyncMap } from '../utils/data-processing' ;
223222import { ENTITY_TYPE_PIR } from '../modules/pir/pir-types' ;
224223import { isMetricsName } from '../modules/metrics/metrics-utils' ;
224+ import { doYield } from '../utils/eventloop-utils' ;
225225
226226const ELK_ENGINE = 'elk' ;
227227const OPENSEARCH_ENGINE = 'opensearch' ;
@@ -1575,8 +1575,8 @@ const elDataConverter = (esHit) => {
15751575export const elConvertHitsToMap = async ( elements , opts ) => {
15761576 const { mapWithAllIds = false } = opts ;
15771577 const convertedHitsMap = { } ;
1578- let startProcessingTime = new Date ( ) . getTime ( ) ;
15791578 for ( let n = 0 ; n < elements . length ; n += 1 ) {
1579+ await doYield ( ) ;
15801580 const element = elements [ n ] ;
15811581 convertedHitsMap [ element . internal_id ] = element ;
15821582 if ( mapWithAllIds ) {
@@ -1589,13 +1589,6 @@ export const elConvertHitsToMap = async (elements, opts) => {
15891589 convertedHitsMap [ id ] = element ;
15901590 } ) ;
15911591 }
1592- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
1593- if ( new Date ( ) . getTime ( ) - startProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
1594- startProcessingTime = new Date ( ) . getTime ( ) ;
1595- await new Promise ( ( resolve ) => {
1596- setImmediate ( resolve ) ;
1597- } ) ;
1598- }
15991592 }
16001593 return convertedHitsMap ;
16011594} ;
@@ -3841,19 +3834,12 @@ const buildRegardingOfFilter = async (context, user, elements, filters) => {
38413834 sideIdManualInferred . set ( sideId , toTypes ) ;
38423835 }
38433836 } ;
3844- let startProcessingTime = new Date ( ) . getTime ( ) ;
38453837 for ( let relIndex = 0 ; relIndex < relationships . length ; relIndex += 1 ) {
3838+ await doYield ( ) ;
38463839 const relation = relationships [ relIndex ] ;
38473840 const relType = isInferredIndex ( relation . _index ) ? 'inferred' : 'manual' ;
38483841 addTypeSide ( relation . fromId , relType ) ;
38493842 addTypeSide ( relation . toId , relType ) ;
3850- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
3851- if ( new Date ( ) . getTime ( ) - startProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
3852- startProcessingTime = new Date ( ) . getTime ( ) ;
3853- await new Promise ( ( resolve ) => {
3854- setImmediate ( resolve ) ;
3855- } ) ;
3856- }
38573843 }
38583844 }
38593845 return ( element ) => {
@@ -4182,24 +4168,17 @@ export const elRemoveRelationConnection = async (context, user, elementsImpact,
41824168 const elIdsCache = { } ;
41834169 const indexCache = { } ;
41844170 const pirInformationCache = { } ;
4185- let startProcessingTime = new Date ( ) . getTime ( ) ;
41864171 for ( let idIndex = 0 ; idIndex < dataIds . length ; idIndex += 1 ) {
4172+ await doYield ( ) ;
41874173 const element = dataIds [ idIndex ] ;
41884174 elIdsCache [ element . internal_id ] = element . _id ;
41894175 indexCache [ element . internal_id ] = element . _index ;
41904176 pirInformationCache [ element . internal_id ] = element . pir_information ;
4191- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
4192- if ( new Date ( ) . getTime ( ) - startProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
4193- startProcessingTime = new Date ( ) . getTime ( ) ;
4194- await new Promise ( ( resolve ) => {
4195- setImmediate ( resolve ) ;
4196- } ) ;
4197- }
41984177 }
41994178 // Split by max operations, create the bulk
42004179 const groupsOfImpacts = R . splitEvery ( MAX_BULK_OPERATIONS , impacts ) ;
4201- startProcessingTime = new Date ( ) . getTime ( ) ;
42024180 for ( let i = 0 ; i < groupsOfImpacts . length ; i += 1 ) {
4181+ await doYield ( ) ;
42034182 const impactsBulk = groupsOfImpacts [ i ] ;
42044183 const bodyUpdateRaw = impactsBulk . map ( ( [ impactId , elementMeta ] ) => {
42054184 return Object . entries ( elementMeta ) . map ( ( [ typeAndIndex , cleanupIds ] ) => {
@@ -4256,22 +4235,15 @@ export const elRemoveRelationConnection = async (context, user, elementsImpact,
42564235 if ( bodyUpdate . length > 0 ) {
42574236 await elBulk ( { refresh : forceRefresh , timeout : BULK_TIMEOUT , body : bodyUpdate } ) ;
42584237 }
4259- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
4260- if ( new Date ( ) . getTime ( ) - startProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
4261- startProcessingTime = new Date ( ) . getTime ( ) ;
4262- await new Promise ( ( resolve ) => {
4263- setImmediate ( resolve ) ;
4264- } ) ;
4265- }
42664238 }
42674239 }
42684240} ;
42694241
42704242export const computeDeleteElementsImpacts = async ( cleanupRelations , toBeRemovedIds , relationsToRemoveMap ) => {
42714243 // Update all rel connections that will remain
42724244 const elementsImpact = { } ;
4273- let startProcessingTime = new Date ( ) . getTime ( ) ;
42744245 for ( let i = 0 ; i < cleanupRelations . length ; i += 1 ) {
4246+ await doYield ( ) ;
42754247 const relation = cleanupRelations [ i ] ;
42764248 const fromWillNotBeRemoved = ! relationsToRemoveMap . has ( relation . fromId ) && ! toBeRemovedIds . includes ( relation . fromId ) ;
42774249 const isFromCleanup = fromWillNotBeRemoved && isImpactedTypeAndSide ( relation . entity_type , relation . fromType , relation . toType , ROLE_FROM ) ;
@@ -4303,13 +4275,6 @@ export const computeDeleteElementsImpacts = async (cleanupRelations, toBeRemoved
43034275 }
43044276 }
43054277 }
4306- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
4307- if ( new Date ( ) . getTime ( ) - startProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
4308- startProcessingTime = new Date ( ) . getTime ( ) ;
4309- await new Promise ( ( resolve ) => {
4310- setImmediate ( resolve ) ;
4311- } ) ;
4312- }
43134278 }
43144279 return elementsImpact ;
43154280} ;
@@ -4528,15 +4493,22 @@ const createDeleteOperationElement = async (context, user, mainElement, deletedE
45284493export const prepareElementForIndexing = async ( element ) => {
45294494 const thing = { } ;
45304495 const keyItems = Object . keys ( element ) ;
4531- let startProcessingTime = new Date ( ) . getTime ( ) ;
45324496 for ( let index = 0 ; index < keyItems . length ; index += 1 ) {
4497+ await doYield ( ) ;
45334498 const key = keyItems [ index ] ;
45344499 const value = element [ key ] ;
45354500 if ( Array . isArray ( value ) ) { // Array of Date, objects, string or number
45364501 const preparedArray = [ ] ;
4537- let innerProcessingTime = new Date ( ) . getTime ( ) ;
4538- let extendLoopSplit = 0 ;
4502+ let yieldCount = 0 ;
45394503 for ( let valueIndex = 0 ; valueIndex < value . length ; valueIndex += 1 ) {
4504+ if ( await doYield ( ) ) {
4505+ // If we extend the preparation 5 times, log a warn
4506+ // It will help to understand what kind of key have so many elements
4507+ if ( yieldCount === 5 ) {
4508+ logApp . warn ( '[ENGINE] Element preparation too many values' , { id : element . id , key, size : value . length } ) ;
4509+ }
4510+ yieldCount += 1 ;
4511+ }
45404512 const valueElement = value [ valueIndex ] ;
45414513 if ( valueElement ) {
45424514 if ( isDateAttribute ( key ) ) { // Date is an object but natively supported
@@ -4551,19 +4523,6 @@ export const prepareElementForIndexing = async (element) => {
45514523 preparedArray . push ( valueElement ) ;
45524524 }
45534525 }
4554- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
4555- if ( new Date ( ) . getTime ( ) - innerProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
4556- // If we extends the preparation 5 times, log a warn
4557- // It will help to understand what kind of key have so much elements
4558- if ( extendLoopSplit === 5 ) {
4559- logApp . warn ( '[ENGINE] Element preparation too many values' , { id : element . id , key, size : value . length } ) ;
4560- }
4561- extendLoopSplit += 1 ;
4562- innerProcessingTime = new Date ( ) . getTime ( ) ;
4563- await new Promise ( ( resolve ) => {
4564- setImmediate ( resolve ) ;
4565- } ) ;
4566- }
45674526 }
45684527 thing [ key ] = preparedArray ;
45694528 } else if ( isDateAttribute ( key ) ) { // Date is an object but natively supported
@@ -4579,13 +4538,6 @@ export const prepareElementForIndexing = async (element) => {
45794538 } else { // For all other types (numeric, ...), no transform
45804539 thing [ key ] = value ;
45814540 }
4582- // Prevent event loop locking more than MAX_EVENT_LOOP_PROCESSING_TIME
4583- if ( new Date ( ) . getTime ( ) - startProcessingTime > MAX_EVENT_LOOP_PROCESSING_TIME ) {
4584- startProcessingTime = new Date ( ) . getTime ( ) ;
4585- await new Promise ( ( resolve ) => {
4586- setImmediate ( resolve ) ;
4587- } ) ;
4588- }
45894541 }
45904542 return thing ;
45914543} ;
0 commit comments