Skip to content

Commit

Permalink
make runs autoreload until done
Browse files Browse the repository at this point in the history
  • Loading branch information
stoerr committed Oct 25, 2024
1 parent 8c4dfcc commit 68cea10
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,10 +43,16 @@ public boolean isDisabled() {
|| autoTranslateConfigService == null || !autoTranslateConfigService.isPocUiEnabled();
}

@Nonnull
public List<AutoTranslateService.TranslationRun> getTranslationRuns() {
return autoTranslateService != null ? autoTranslateService.getTranslationRuns() : Collections.emptyList();
}

public boolean inProgress() {
List<AutoTranslateService.TranslationRun> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -188,7 +188,7 @@ public List<TranslationPage> getTranslatedPages() {
public void cancel() {
if (future != null) {
future.cancel(true);
status = "cancelling";
status = TranslationStatus.CANCELLING;
}
}

Expand All @@ -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());
Expand All @@ -222,7 +222,7 @@ public void execute(ResourceResolver callResourceResolver) {
interrupted = true;
}
if (interrupted) {
status = "cancelled";
status = TranslationStatus.CANCELLED;
page.status = "cancelled";
continue;
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</coral-alert>
<span class="foundation-field-readonly coral-Form-fieldwrapper">
<span class="coral-Form-field foundation-layout-util-breakword">
<strong>Status:</strong> ${model.status}
<strong>Status:</strong> ${model.statusString}
</span>
</span>
<span class="foundation-field-readonly coral-Form-fieldwrapper">
Expand Down Expand Up @@ -96,6 +96,7 @@
</tr>
</tbody>
</table>
<input type="hidden" id="pending" value="true" data-sly-test="${model.inProgress}">
</div>
<p>
<a href="../run.cancel.html/${model.id}"
Expand Down Expand Up @@ -126,5 +127,13 @@
</div>
</div>
<sly data-sly-call="${clientLib.js @ categories='coralui3,granite.ui.foundation.content'}"></sly>
<script>
// as long as #pending is set, refresh the page every 20 seconds
if (document.getElementById('pending')) {
setTimeout(function () {
location.reload();
}, 10000);
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</coral-alert>
<span class="foundation-field-readonly coral-Form-fieldwrapper">
<span class="coral-Form-field foundation-layout-util-breakword">
<strong>Status:</strong> ${model.status}
<strong>Status:</strong> ${model.statusString}
</span>
</span>
<span class="foundation-field-readonly coral-Form-fieldwrapper">
Expand Down Expand Up @@ -95,6 +95,7 @@
</tr>
</tbody>
</table>
<input type="hidden" id="pending" value="true" data-sly-test="${model.inProgress}">
</div>
<p>
<a href="../run.cancel.html/${model.id}"
Expand All @@ -112,7 +113,6 @@
<p>${model.messages.toString @ context='html'}</p>
</div>
</div>

</section>
</sly>
</div>
Expand All @@ -121,5 +121,13 @@
</div>
</div>
<sly data-sly-call="${clientLib.js @ categories='coralui3,granite.ui.foundation.content'}"></sly>
<script>
// as long as #pending is set, refresh the page every 20 seconds
if (document.getElementById('pending')) {
setTimeout(function () {
location.reload();
}, 10000);
}
</script>
</body>
</html>

0 comments on commit 68cea10

Please sign in to comment.