From e98d8f3ed95a1c931a7fb992db88c2cf53fa3900 Mon Sep 17 00:00:00 2001 From: tc-wleite Date: Wed, 11 Dec 2024 12:15:23 -0300 Subject: [PATCH] '#2381: Disable audio transcription if init fails when creating reports. --- .../transcript/RemoteTranscriptionTask.java | 23 +++++++++++--- .../transcript/Wav2Vec2TranscriptTask.java | 30 +++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/iped-engine/src/main/java/iped/engine/task/transcript/RemoteTranscriptionTask.java b/iped-engine/src/main/java/iped/engine/task/transcript/RemoteTranscriptionTask.java index 8c321db98f..678f835b37 100644 --- a/iped-engine/src/main/java/iped/engine/task/transcript/RemoteTranscriptionTask.java +++ b/iped-engine/src/main/java/iped/engine/task/transcript/RemoteTranscriptionTask.java @@ -56,6 +56,8 @@ public class RemoteTranscriptionTask extends AbstractTranscriptTask { private static AtomicBoolean statsPrinted = new AtomicBoolean(); + private static volatile AtomicBoolean init = new AtomicBoolean(); + private static long lastUpdateServersTime = 0; private static class Server { @@ -81,7 +83,7 @@ public void init(ConfigurationManager configurationManager) throws Exception { super.init(configurationManager); - if (!this.isEnabled()) { + if (!isEnabled()) { return; } @@ -115,8 +117,21 @@ public void init(ConfigurationManager configurationManager) throws Exception { return; } - requestServers(true); - + synchronized (init) { + if (!init.get()) { + try { + requestServers(true); + } catch (Exception e) { + if (hasIpedDatasource()) { + transcriptConfig.setEnabled(false); + logger.warn("Could not initialize remote transcription. Task disabled."); + } else { + throw e; + } + } + init.set(true); + } + } } private static synchronized void requestServers(RemoteTranscriptionTask task, boolean now) throws IOException { @@ -163,7 +178,7 @@ private void requestServers(boolean now) throws IOException { @Override public void finish() throws Exception { super.finish(); - if (!statsPrinted.getAndSet(true)) { + if (isEnabled() && !statsPrinted.getAndSet(true)) { int numWorkers = this.worker.manager.getNumWorkers(); DecimalFormat df = new DecimalFormat(); logger.info("Time spent to send audios: {}s", df.format(audioSendingTime.get() / (1000 * numWorkers))); diff --git a/iped-engine/src/main/java/iped/engine/task/transcript/Wav2Vec2TranscriptTask.java b/iped-engine/src/main/java/iped/engine/task/transcript/Wav2Vec2TranscriptTask.java index 803fee7b12..1e88610239 100644 --- a/iped-engine/src/main/java/iped/engine/task/transcript/Wav2Vec2TranscriptTask.java +++ b/iped-engine/src/main/java/iped/engine/task/transcript/Wav2Vec2TranscriptTask.java @@ -7,6 +7,7 @@ import java.io.InputStreamReader; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.lang3.SystemUtils; import org.apache.logging.log4j.Level; @@ -42,6 +43,8 @@ public class Wav2Vec2TranscriptTask extends AbstractTranscriptTask { protected static volatile Level logLevel = Level.forName("MSG", 250); + private static volatile AtomicBoolean init = new AtomicBoolean(); + static class Server { Process process; BufferedReader reader; @@ -68,7 +71,7 @@ public void init(ConfigurationManager configurationManager) throws Exception { super.init(configurationManager); - if (!this.isEnabled()) { + if (!isEnabled()) { return; } @@ -85,11 +88,26 @@ public void init(ConfigurationManager configurationManager) throws Exception { if (!deque.isEmpty()) return; - - Server server; - int device = 0; - while ((server = startServer(device++)) != null) { - deque.add(server); + + synchronized (init) { + if (!init.get()) { + try { + Server server; + int device = 0; + while ((server = startServer(device++)) != null) { + deque.add(server); + } + + } catch (Exception e) { + if (hasIpedDatasource()) { + transcriptConfig.setEnabled(false); + logger.warn("Could not initialize audio transcription. Task disabled."); + } else { + throw e; + } + } + init.set(true); + } } logLevel = Level.DEBUG;