-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODLD-503: Reindex LD instance if Suppress Flags changed (#5)
MODLD-503: Reindex LD instance if Suppress Flags changed
- Loading branch information
1 parent
aa97f9c
commit 4f209e0
Showing
18 changed files
with
596 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...java/org/folio/linked/data/integration/kafka/listener/InventoryInstanceEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.folio.linked.data.integration.kafka.listener; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static org.folio.linked.data.domain.dto.ResourceIndexEventType.UPDATE; | ||
import static org.folio.linked.data.util.Constants.FOLIO_PROFILE; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.logging.log4j.Level; | ||
import org.folio.linked.data.domain.dto.InventoryInstanceEvent; | ||
import org.folio.linked.data.integration.kafka.listener.handler.InventoryInstanceEventHandler; | ||
import org.folio.linked.data.service.tenant.TenantScopedExecutionService; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.retry.RetryContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Log4j2 | ||
@Profile(FOLIO_PROFILE) | ||
@RequiredArgsConstructor | ||
public class InventoryInstanceEventListener { | ||
|
||
private static final String INVENTORY_INSTANCE_EVENT_LISTENER = "mod-linked-data-inventory-instance-event-listener"; | ||
private static final String INVENTORY_EVENT_LISTENER_CONTAINER_FACTORY = "inventoryEventListenerContainerFactory"; | ||
private final TenantScopedExecutionService tenantScopedExecutionService; | ||
private final InventoryInstanceEventHandler inventoryInstanceEventHandler; | ||
|
||
@KafkaListener( | ||
id = INVENTORY_INSTANCE_EVENT_LISTENER, | ||
containerFactory = INVENTORY_EVENT_LISTENER_CONTAINER_FACTORY, | ||
groupId = "#{folioKafkaProperties.listener['inventory-instance-event'].groupId}", | ||
concurrency = "#{folioKafkaProperties.listener['inventory-instance-event'].concurrency}", | ||
topicPattern = "#{folioKafkaProperties.listener['inventory-instance-event'].topicPattern}") | ||
public void handleInventoryInstanceEvent(List<ConsumerRecord<String, InventoryInstanceEvent>> consumerRecords) { | ||
consumerRecords.forEach(this::handleRecord); | ||
} | ||
|
||
private void handleRecord(ConsumerRecord<String, InventoryInstanceEvent> consumerRecord) { | ||
var event = consumerRecord.value(); | ||
if (event.getType() == UPDATE) { | ||
tenantScopedExecutionService.executeAsyncWithRetry( | ||
consumerRecord.headers(), | ||
retryContext -> runRetryableJob(event, retryContext), | ||
ex -> logFailedEvent(event, ex, false) | ||
); | ||
} | ||
} | ||
|
||
private void runRetryableJob(InventoryInstanceEvent event, RetryContext retryContext) { | ||
ofNullable(retryContext.getLastThrowable()) | ||
.ifPresent(ex -> logFailedEvent(event, ex, true)); | ||
inventoryInstanceEventHandler.handle(event); | ||
} | ||
|
||
private void logFailedEvent(InventoryInstanceEvent event, Throwable ex, boolean isRetrying) { | ||
var logLevel = isRetrying ? Level.INFO : Level.ERROR; | ||
log.log(logLevel, "Failed to reindex inventory instance with id {}. Retrying: {}", | ||
event.getNew().getId(), isRetrying, ex); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...g/folio/linked/data/integration/kafka/listener/handler/InventoryInstanceEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.folio.linked.data.integration.kafka.listener.handler; | ||
|
||
import static org.apache.commons.lang3.ObjectUtils.anyNull; | ||
import static org.folio.linked.data.domain.dto.ResourceIndexEventType.UPDATE; | ||
import static org.folio.linked.data.util.Constants.FOLIO_PROFILE; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.linked.data.domain.dto.InventoryInstanceEvent; | ||
import org.folio.linked.data.integration.kafka.sender.search.WorkUpdateMessageSender; | ||
import org.folio.linked.data.model.entity.FolioMetadata; | ||
import org.folio.linked.data.model.entity.Resource; | ||
import org.folio.linked.data.repo.FolioMetadataRepository; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Log4j2 | ||
@Component | ||
@Profile(FOLIO_PROFILE) | ||
@RequiredArgsConstructor | ||
public class InventoryInstanceEventHandler { | ||
|
||
public static final String INSTANCE_REINDEX_NOT_REQUIRED = "Ignoring InventoryInstanceEvent '{}'," | ||
+ " reindexing not required."; | ||
public static final String SENT_FOR_REINDEXING = "Instance '{}' has some fields updated, sent for reindexing."; | ||
public static final String SUPPRESS_FLAGS_CHANGED = "InventoryInstanceEvent:{} - changes in suppress flags: {}."; | ||
|
||
private final FolioMetadataRepository folioMetadataRepository; | ||
private final WorkUpdateMessageSender workUpdateMessageSender; | ||
|
||
@Transactional | ||
public void handle(InventoryInstanceEvent event) { | ||
getOptionalReindexResource(event) | ||
.ifPresentOrElse(this::reindexResource, | ||
() -> log.debug(INSTANCE_REINDEX_NOT_REQUIRED, event.getId())); | ||
} | ||
|
||
private Optional<Resource> getOptionalReindexResource(InventoryInstanceEvent event) { | ||
if (reindexNotRequired(event)) { | ||
return Optional.empty(); | ||
} | ||
return folioMetadataRepository.findByInventoryId(event.getNew().getId()) | ||
.map(metadata -> updateMetadataAndGetResource(metadata, event)); | ||
} | ||
|
||
private void reindexResource(Resource resource) { | ||
workUpdateMessageSender.produce(resource); | ||
log.info(SENT_FOR_REINDEXING, resource.getId()); | ||
} | ||
|
||
private Resource updateMetadataAndGetResource(FolioMetadata folioMetadata, InventoryInstanceEvent event) { | ||
folioMetadata.setStaffSuppress(event.getNew().getStaffSuppress()); | ||
folioMetadata.setSuppressFromDiscovery(event.getNew().getDiscoverySuppress()); | ||
return folioMetadataRepository.save(folioMetadata).getResource(); | ||
} | ||
|
||
private boolean reindexNotRequired(InventoryInstanceEvent event) { | ||
if (notEqual(event.getType(), UPDATE) || anyNull(event.getOld(), event.getNew())) { | ||
return true; | ||
} | ||
return !suppressFlagsChanged(event); | ||
} | ||
|
||
private boolean suppressFlagsChanged(InventoryInstanceEvent event) { | ||
var newObj = event.getNew(); | ||
var oldObj = event.getOld(); | ||
var result = notEqual(newObj.getStaffSuppress(), oldObj.getStaffSuppress()) | ||
|| notEqual(newObj.getDiscoverySuppress(), oldObj.getDiscoverySuppress()); | ||
log.debug(SUPPRESS_FLAGS_CHANGED, event.getId(), result); | ||
return result; | ||
} | ||
|
||
private boolean notEqual(Object first, Object second) { | ||
return !Objects.equals(first, second); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/resources/swagger.api/folio-modules/inventory/inventoryInstance.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Inventory instance event data model", | ||
"javaType": "org.folio.rest.jaxrs.model.InventoryInstance", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"id": { | ||
"description": "UUID", | ||
"$ref": "../common/uuid.json" | ||
}, | ||
"staffSuppress": { | ||
"description": "Suppress flags - Staff", | ||
"type": "boolean" | ||
}, | ||
"discoverySuppress": { | ||
"description": "Suppress flags - From Discovery", | ||
"type": "boolean" | ||
} | ||
}, | ||
"required": [ | ||
"id" | ||
] | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/resources/swagger.api/folio-modules/inventory/inventoryInstanceEvent.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Inventory instance event data model", | ||
"javaType": "org.folio.rest.jaxrs.model.InventoryInstanceEvent", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"id": { | ||
"description": "Id of the event", | ||
"$ref": "../common/uuid.json" | ||
}, | ||
"new": { | ||
"description": "New state of the instance", | ||
"type": "object", | ||
"$ref": "inventoryInstance.json" | ||
}, | ||
"old": { | ||
"description": "Old state of the instance", | ||
"type": "object", | ||
"$ref": "inventoryInstance.json" | ||
}, | ||
"type": { | ||
"description": "Event type", | ||
"$ref": "../search/resourceIndexEventType.json" | ||
}, | ||
"tenant": { | ||
"description": "Tenant id", | ||
"type": "string" | ||
}, | ||
"ts": { | ||
"description": "Timestamp", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"id" | ||
], | ||
"excludedFromEqualsAndHashCode": [ | ||
"tenant", | ||
"ts" | ||
], | ||
"x-implements": "org.folio.spring.tools.kafka.BaseKafkaMessage" | ||
} |
Oops, something went wrong.