Skip to content

Commit

Permalink
Ability to rebuild Silo from API Command
Browse files Browse the repository at this point in the history
  • Loading branch information
isontheline committed Jun 1, 2024
1 parent 98a8647 commit 7e7a905
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DatabaseCommandBackendApiController {
@Autowired
private DatabaseService databaseService;

@PostMapping("/database/export")
@PostMapping("/export")
@ApiResponse(responseCode = "200", description = "Database dump has been successfully created.")
@Operation(summary = "Create a database export", description = "Creates a JSON database dump.")
public void export(HttpServletResponse response) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ai.dragon.controller.api.backendapi.command;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ai.dragon.service.SiloService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/api/backendapi/command/silo")
@Tag(name = "Silo Command", description = "Silo Command API Endpoints")
public class SiloCommandBackendApiController {
@Autowired
private SiloService siloService;

@PostMapping("/rebuild/{uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}")
@ApiResponse(responseCode = "200", description = "Silo is being rebuilt.")
@Operation(summary = "Rebuild Silo", description = "This will recompute the embeddings of the Silo.")
public void export(@PathVariable("uuid") @Parameter(description = "Identifier of the Silo") UUID uuid) throws Exception {
siloService.rebuildSilo(uuid);
}
}
10 changes: 8 additions & 2 deletions backend/src/main/java/ai/dragon/service/SiloJobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onChangeEvent(CollectionEventInfo<?> collectionEventInfo, SiloEntity
removeSiloIngestorJob(entity);
break;
case Update:
restartSiloIngestorJob(entity);
recreateSiloIngestorJob(entity);
break;
default:
break;
Expand All @@ -67,7 +67,13 @@ private void destroy() {
jobService.removeAllRecurringJobs();
}

public void restartSiloIngestorJob(SiloEntity siloEntity) {
public void startSiloIngestorJobNow(SiloEntity siloEntity) {
logger.info(
String.format("Starting Silo Ingestor Job Now : %s -> %s", siloEntity.getUuid(), siloEntity.getName()));
jobService.triggerRecurringJob(siloEntity.getUuid().toString());
}

public void recreateSiloIngestorJob(SiloEntity siloEntity) {
logger.info(
String.format("Restarting Silo Ingestor Job : %s -> %s", siloEntity.getUuid(), siloEntity.getName()));
removeSiloIngestorJob(siloEntity);
Expand Down
10 changes: 10 additions & 0 deletions backend/src/main/java/ai/dragon/service/SiloService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ai.dragon.service;

import java.util.UUID;

import org.dizitart.no2.collection.events.CollectionEventInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -25,6 +27,9 @@ public class SiloService {
@Autowired
private FarmRepository farmRepository;

@Autowired
private SiloJobService siloJobService;

@PostConstruct
private void init() {
entityChangeListener = siloRepository.subscribe(new EntityChangeListener<SiloEntity>() {
Expand All @@ -47,6 +52,11 @@ private void destroy() {
siloRepository.unsubscribe(entityChangeListener);
}

public void rebuildSilo(UUID uuid) {
SiloEntity entity = siloRepository.getByUuid(uuid).orElseThrow();
siloJobService.startSiloIngestorJobNow(entity);
}

private void removeFarmLinks(SiloEntity entity) {
for (FarmEntity farm : farmRepository.find()) {
if (farm.getSilos().contains(entity.getUuid())) {
Expand Down

0 comments on commit 7e7a905

Please sign in to comment.