Skip to content

Commit

Permalink
Add 400 if job already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrojo77 committed Dec 13, 2024
1 parent e7dedf9 commit 2337237
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.ndla.audioapi.controller

import no.ndla.audioapi.Props
import no.ndla.audioapi.model.api.JobAlreadyFoundException
import no.ndla.audioapi.service.{ReadService, TranscriptionService}
import no.ndla.network.tapir.TapirController
import no.ndla.network.tapir.TapirUtil.errorOutputsFor
Expand Down Expand Up @@ -68,7 +69,9 @@ trait TranscriptionController {
.serverLogicPure { _ =>
{ case (videoId, language, maxSpeakerOpt) =>
transcriptionService.transcribeVideo(videoId, language, maxSpeakerOpt) match {
case Success(_) => Right(())
case Success(_) => Right(())
case Failure(ex: JobAlreadyFoundException) =>
returnLeftError(ex)
case Failure(ex) => returnLeftError(ex)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ trait ErrorHandling extends TapirErrorHandling {
if rf.error.rootCause
.exists(x => x.`type` == "search_context_missing_exception" || x.reason == "Cannot parse scroll id") =>
invalidSearchContext
case jafe: JobAlreadyFoundException => ErrorBody(JOB_ALREADY_FOUND, jafe.getMessage, clock.now(), 400)
}

}
Expand All @@ -64,3 +65,4 @@ case class CouldNotFindLanguageException(message: String) extends RuntimeExcepti
class AudioStorageException(message: String) extends RuntimeException(message)
class LanguageMappingException(message: String) extends RuntimeException(message)
class ImportException(message: String) extends RuntimeException(message)
case class JobAlreadyFoundException(message: String) extends RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.ndla.audioapi.service

import com.typesafe.scalalogging.StrictLogging
import no.ndla.audioapi.Props
import no.ndla.audioapi.model.api.JobAlreadyFoundException
import no.ndla.common.aws.{NdlaAWSTranscribeClient, NdlaS3Client}
import no.ndla.common.brightcove.NdlaBrightcoveClient
import sttp.client3.{HttpURLConnectionBackend, UriContext, asFile, basicRequest}
Expand All @@ -19,6 +20,17 @@ trait TranscriptionService {
class TranscriptionService extends StrictLogging {

def transcribeVideo(videoId: String, language: String, maxSpeakers: Int): Try[Unit] = {
getTranscription(videoId, language) match {
case Success(status) if status == "COMPLETED" =>
logger.info(s"Transcription already completed for videoId: $videoId")
return Failure(new JobAlreadyFoundException(s"Transcription already completed for videoId: $videoId"))
case Success(status) if status == "IN_PROGRESS" =>
logger.info(s"Transcription already in progress for videoId: $videoId")
return Failure(new JobAlreadyFoundException(s"Transcription already in progress for videoId: $videoId"))
case _ =>
logger.info(s"No existing transcription job for videoId: $videoId")
}

getAudioExtractionStatus(videoId, language) match {
case Success(_) =>
logger.info(s"Audio already extracted for videoId: $videoId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ trait TapirErrorHandling extends StrictLogging {
val VALIDATION = "VALIDATION_ERROR"
val METHOD_NOT_ALLOWED = "METHOD_NOT_ALLOWED"
val CONFLICT = "CONFLICT"
val JOB_ALREADY_FOUND = "JOB_ALREADY_FOUND"

val PARAMETER_MISSING = "PARAMETER MISSING"
val PROVIDER_NOT_SUPPORTED = "PROVIDER NOT SUPPORTED"
Expand Down

0 comments on commit 2337237

Please sign in to comment.