From 68cea10e2e1bba61be7721e5c3b78aa29604f889 Mon Sep 17 00:00:00 2001 From: "Dr. Hans-Peter Stoerr" Date: Fri, 25 Oct 2024 16:48:58 +0200 Subject: [PATCH] make runs autoreload until done --- .../autotranslate/AutoTranslateListModel.java | 8 ++++++++ .../impl/autotranslate/AutoTranslateService.java | 15 ++++++++++++++- .../autotranslate/AutoTranslateServiceImpl.java | 16 ++++++++-------- .../autotranslate-experiments/run/run.html | 11 ++++++++++- .../components/autotranslate/run/run.html | 12 ++++++++++-- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java index 66a824b0d..0df8b5718 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateListModel.java @@ -3,6 +3,8 @@ import java.util.Collections; import java.util.List; +import javax.annotation.Nonnull; + import org.apache.commons.lang3.StringUtils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.LoginException; @@ -41,10 +43,16 @@ public boolean isDisabled() { || autoTranslateConfigService == null || !autoTranslateConfigService.isPocUiEnabled(); } + @Nonnull public List getTranslationRuns() { return autoTranslateService != null ? autoTranslateService.getTranslationRuns() : Collections.emptyList(); } + public boolean inProgress() { + List runs = getTranslationRuns(); + return runs.stream().filter(run -> run.isInProgress()).findAny().isPresent(); + } + public AutoTranslateService.TranslationRun createRun() throws LoginException, PersistenceException { if (run == null) { String path = request.getParameter("path"); diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateService.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateService.java index 38ef0fac7..fe08813ef 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateService.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateService.java @@ -112,9 +112,13 @@ public TranslationParameters clone() { } } + enum TranslationStatus { + QUEUED, CANCELLING, RUNNING, CANCELLED, DONE_WITH_ERRORS, INTERRUPTED, ERROR, FINISHED; + } + abstract class TranslationRun { public String id; - public String status; + public TranslationStatus status; public String startTime; public String stopTime; public String user; @@ -127,6 +131,15 @@ abstract class TranslationRun { public abstract void rollback(@Nonnull ResourceResolver resourceResolver) throws PersistenceException, WCMException; + @Nonnull + public String statusString() { + return status != null ? status.name().toLowerCase() : ""; + } + + public boolean isInProgress() { + return status == TranslationStatus.QUEUED || status == TranslationStatus.RUNNING || status == TranslationStatus.CANCELLING; + } + @Override public String toString() { diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java index 13b155388..8be8daed6 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoTranslateServiceImpl.java @@ -135,7 +135,7 @@ public TranslationRun startTranslation( .map(r -> new TranslationPageImpl(r.getPath())) .collect(Collectors.toList()); run.waituntil = System.currentTimeMillis() + 1000; // when triggered during live copy creation. - run.status = "queued"; + run.status = TranslationStatus.QUEUED; run.user = resourceResolver.getUserID(); stateService.getTranslationRuns().add(run); run.future = getThreadPool().submit(() -> run.execute(processResolver)); @@ -188,7 +188,7 @@ public List getTranslatedPages() { public void cancel() { if (future != null) { future.cancel(true); - status = "cancelling"; + status = TranslationStatus.CANCELLING; } } @@ -207,7 +207,7 @@ public synchronized void rollback(@Nonnull ResourceResolver resourceResolver) th */ public void execute(ResourceResolver callResourceResolver) { try { - status = "running"; + status = TranslationStatus.RUNNING; if (System.currentTimeMillis() < waituntil) { // delay a little since that is used during creating a livecopy, and that should be finished. Thread.sleep(waituntil - System.currentTimeMillis()); @@ -222,7 +222,7 @@ public void execute(ResourceResolver callResourceResolver) { interrupted = true; } if (interrupted) { - status = "cancelled"; + status = TranslationStatus.CANCELLED; page.status = "cancelled"; continue; } @@ -244,19 +244,19 @@ public void execute(ResourceResolver callResourceResolver) { hasErrors = true; } } - status = hasErrors ? "doneWithErrors" : interrupted ? "cancelled" : "done"; + status = hasErrors ? TranslationStatus.DONE_WITH_ERRORS : interrupted ? TranslationStatus.CANCELLED : TranslationStatus.FINISHED; } catch (InterruptedException e) { LOG.error("Interruption during " + this, e); Thread.currentThread().interrupt(); - status = "interrupted"; + status = TranslationStatus.INTERRUPTED; } catch (Exception e) { LOG.error("Error during " + this, e); - status = "error"; + status = TranslationStatus.ERROR; messages.append("Error: ").append(e).append("\n"); } finally { stopTime = new Date().toString(); if (status == null) { - status = "finished"; + status = TranslationStatus.FINISHED; } future = null; callResourceResolver.close(); diff --git a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate-experiments/run/run.html b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate-experiments/run/run.html index 947412373..37052ac7d 100644 --- a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate-experiments/run/run.html +++ b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate-experiments/run/run.html @@ -27,7 +27,7 @@ - Status: ${model.status} + Status: ${model.statusString} @@ -96,6 +96,7 @@ +

+ diff --git a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/run/run.html b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/run/run.html index 34b19fad9..3006bc65b 100644 --- a/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/run/run.html +++ b/aem/ui.apps/src/main/content/jcr_root/apps/composum-ai/components/autotranslate/run/run.html @@ -26,7 +26,7 @@ - Status: ${model.status} + Status: ${model.statusString} @@ -95,6 +95,7 @@ +

${model.messages.toString @ context='html'}

- @@ -121,5 +121,13 @@ +