Queue guest document extraction
All checks were successful
build-and-deploy / build-deploy (push) Successful in 3m36s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 3m36s
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package com.android.trisolarisserver.component
|
||||||
|
|
||||||
|
import jakarta.annotation.PreDestroy
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class ExtractionQueue {
|
||||||
|
private val executor = Executors.newSingleThreadExecutor { runnable ->
|
||||||
|
Thread(runnable, "doc-extraction-queue").apply { isDaemon = true }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun enqueue(task: () -> Unit) {
|
||||||
|
executor.submit {
|
||||||
|
try {
|
||||||
|
task()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
// Best-effort processing; failures should not crash the worker.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
fun shutdown() {
|
||||||
|
executor.shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.android.trisolarisserver.controller
|
|||||||
|
|
||||||
import com.android.trisolarisserver.component.DocumentStorage
|
import com.android.trisolarisserver.component.DocumentStorage
|
||||||
import com.android.trisolarisserver.component.DocumentTokenService
|
import com.android.trisolarisserver.component.DocumentTokenService
|
||||||
|
import com.android.trisolarisserver.component.ExtractionQueue
|
||||||
import com.android.trisolarisserver.component.LlamaClient
|
import com.android.trisolarisserver.component.LlamaClient
|
||||||
import com.android.trisolarisserver.component.PropertyAccess
|
import com.android.trisolarisserver.component.PropertyAccess
|
||||||
import com.android.trisolarisserver.db.repo.BookingRepo
|
import com.android.trisolarisserver.db.repo.BookingRepo
|
||||||
@@ -38,6 +39,7 @@ class GuestDocuments(
|
|||||||
private val appUserRepo: AppUserRepo,
|
private val appUserRepo: AppUserRepo,
|
||||||
private val storage: DocumentStorage,
|
private val storage: DocumentStorage,
|
||||||
private val tokenService: DocumentTokenService,
|
private val tokenService: DocumentTokenService,
|
||||||
|
private val extractionQueue: ExtractionQueue,
|
||||||
private val llamaClient: LlamaClient,
|
private val llamaClient: LlamaClient,
|
||||||
private val objectMapper: ObjectMapper,
|
private val objectMapper: ObjectMapper,
|
||||||
@org.springframework.beans.factory.annotation.Value("\${storage.documents.publicBaseUrl}")
|
@org.springframework.beans.factory.annotation.Value("\${storage.documents.publicBaseUrl}")
|
||||||
@@ -88,8 +90,8 @@ class GuestDocuments(
|
|||||||
storagePath = stored.storagePath
|
storagePath = stored.storagePath
|
||||||
)
|
)
|
||||||
val saved = guestDocumentRepo.save(document)
|
val saved = guestDocumentRepo.save(document)
|
||||||
runExtraction(saved, propertyId, guestId)
|
runExtraction(saved.id!!, propertyId, guestId)
|
||||||
return guestDocumentRepo.save(saved).toResponse(objectMapper)
|
return saved.toResponse(objectMapper)
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@@ -135,7 +137,9 @@ class GuestDocuments(
|
|||||||
.body(resource)
|
.body(resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun runExtraction(document: GuestDocument, propertyId: UUID, guestId: UUID) {
|
private fun runExtraction(documentId: UUID, propertyId: UUID, guestId: UUID) {
|
||||||
|
extractionQueue.enqueue {
|
||||||
|
val document = guestDocumentRepo.findById(documentId).orElse(null) ?: return@enqueue
|
||||||
try {
|
try {
|
||||||
val token = tokenService.createToken(document.id.toString())
|
val token = tokenService.createToken(document.id.toString())
|
||||||
val imageUrl =
|
val imageUrl =
|
||||||
@@ -190,10 +194,12 @@ class GuestDocuments(
|
|||||||
|
|
||||||
document.extractedData = objectMapper.writeValueAsString(results)
|
document.extractedData = objectMapper.writeValueAsString(results)
|
||||||
document.extractedAt = OffsetDateTime.now()
|
document.extractedAt = OffsetDateTime.now()
|
||||||
|
guestDocumentRepo.save(document)
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
// Keep upload successful even if AI extraction fails.
|
// Keep upload successful even if AI extraction fails.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user