Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refacotr sync tool scheduler #331

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package gsrs.module.substance.tasks;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;

import gsrs.controller.AbstractLegacyTextSearchGsrsEntityController;
import gsrs.controller.hateoas.GsrsEntityToControllerMapper;
import gsrs.scheduledTasks.ScheduledTaskInitializer;
import gsrs.scheduledTasks.SchedulerPlugin;
import gsrs.springUtils.StaticContextAccessor;
import ix.utils.Util;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DatabaseIndexSyncTaskInitializer extends ScheduledTaskInitializer{

private String entityString;

@JsonProperty("entityString")
public void setEntityString(String entities) {
this.entityString = entities;
}


private final Set<String> supportedEntities = Util.toSet("Code","ControlledVocabulary","Name", "Reference","Substance");

@Override
public void run(SchedulerPlugin.JobStats stats, SchedulerPlugin.TaskListener l) {
if(entityString.length()==0) {
log.info("No entities defined for the database and index sync scheduler.");
return;
}

Set<String> syncEntities = Stream.of(entityString.split(",")).map(entity->entity.trim()).collect(Collectors.toSet());
syncEntities.retainAll(supportedEntities);
if(syncEntities.size()==0) {
log.info("No allowed entities defined for the database and index sync scheduler.");
return;
}

for(String entity: syncEntities) {

String entityClassName = generateEntityClassName(entity);
log.info("Entity class: " + entityClassName);

if(entityClassName.isEmpty()) {
log.warn("Illegal entity class: " + entity + " in database and index sync scheduler.");
continue;
}

Class<?> entityClass;
try {
entityClass = Class.forName(entityClassName);
} catch (ClassNotFoundException e) {
log.warn("Entity class is not found: " + entityClassName);
continue;
}

GsrsEntityToControllerMapper mapper = StaticContextAccessor.getBean(GsrsEntityToControllerMapper.class);
if(mapper ==null){
continue;
}
Optional<Class> controllerOpt = mapper.getControllerFor(entityClass);
if(!controllerOpt.isPresent()) {
continue;
}

AbstractLegacyTextSearchGsrsEntityController searchController = (AbstractLegacyTextSearchGsrsEntityController) StaticContextAccessor.getBean(controllerOpt.get());
try {
searchController.syncIndexesWithDatabase();
} catch (JsonProcessingException e) {
log.error("Error in database and index sync scheduler: " + entityClassName);
e.printStackTrace();
continue;
}
}
}

private String generateEntityClassName(String entity) {
return "ix.ginas.models.v1." + entity;
}

@Override
public String getDescription() {
return "Reindex entities in backup tables that are not in indexes";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ eureka.client.enabled= false
ix.index.deepfields = ["ix.ginas.models.v1.Substance"]
ix.index.deepfieldsraw = "ix.ginas.models.v1.Substance"


# When an unspecified search happens, promote all
# exact matches that match specific fields over
# the rest of the matches. In other words,
Expand Down Expand Up @@ -550,6 +549,13 @@ gsrs.entityProcessors=[


gsrs.scheduled-tasks.list=[
{
"scheduledTaskClass" : "gsrs.module.substance.tasks.DatabaseIndexSyncTaskInitializer",
"parameters" : {
"autorun": false,
"entityString" : "Code,ControlledVocabulary,Name,Reference,Substance"
}
},
{
"scheduledTaskClass" : "gsrs.module.substance.tasks.ReindexTaskInitializer",
"parameters" : {
Expand Down
Loading