Skip to content

Commit

Permalink
feat: Cleanup successfully finished jobs by timeout like failed jobs (#…
Browse files Browse the repository at this point in the history
…176)

Refs: #175
  • Loading branch information
nirikash authored Sep 2, 2024
1 parent 9a89ff6 commit a26d5d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ To change WeasyPrint Service URL, adjust the following property in the `polarion
ch.sbb.polarion.extension.pdf-exporter.weasyprint.service=http://localhost:9080
```

### Asynchronous PDF Export: export jobs timeout
This extension provides REST API to export PDF asynchronously. Using this API, it is possible to start export job, observe their status and get result.
Finished (succeed or failed) and in-progress export jobs will be preserved in memory until configured timeout. To change this timeout, adjust the following property in the local `pdf-converter-jobs.properties` file:
```properties
# Timeout in minutes to keep finished async conversion jobs results in memory
jobs.timeout.finished.minutes=30
# Timeout in minutes to wait until async conversion jobs is finished
jobs.timeout.in-progress.minutes=60
```

### PDF exporter extension to appear on a Document's properties pane

1. Open a project where you wish PDF Exporter to be available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Optional<byte[]> getJobResult(String jobId) {
return Optional.empty();
}
if (future.isCancelled() || future.isCompletedExceptionally()) {
throw new IllegalStateException("Job was cancelled or failed: " + jobId);
throw new IllegalStateException("Job was cancelled or failed: " + failedJobsReasons.get(jobId));
}
try {
return Optional.of(future.get());
Expand All @@ -109,8 +109,6 @@ public Optional<byte[]> getJobResult(String jobId) {
throw new IllegalStateException("Cannot extract result for job " + jobId + " :" + e.getMessage(), e);
} catch (Exception e) {
throw new IllegalStateException("Cannot extract result for job " + jobId + " :" + e.getMessage(), e);
} finally {
jobs.remove(jobId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.security.auth.Subject;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -78,9 +77,14 @@ void shouldStartJobAndGetStatus() {
assertThat(jobResult).isNotEmpty();
assertThat(new String(jobResult.get())).isEqualTo("test pdf");

assertThatThrownBy(() -> pdfConverterJobsService.getJobState(jobId))
.isInstanceOf(NoSuchElementException.class)
.hasMessageContaining(jobId);
// Second attempt to ensure that job is not removed
jobState = pdfConverterJobsService.getJobState(jobId);
assertThat(jobState.isDone()).isTrue();
assertThat(jobState.isCompletedExceptionally()).isFalse();
assertThat(jobState.isCancelled()).isFalse();
jobResult = pdfConverterJobsService.getJobResult(jobId);
assertThat(jobResult).isNotEmpty();

verify(securityService).logout(subject);
}

Expand All @@ -100,6 +104,10 @@ void shouldReturnFailInExceptionalCase() {
assertThat(jobState.isCompletedExceptionally()).isTrue();
assertThat(jobState.isCancelled()).isFalse();
assertThat(jobState.errorMessage()).contains("test error");

assertThatThrownBy(() -> pdfConverterJobsService.getJobResult(jobId))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("test error");
verify(securityService).logout(subject);
}

Expand Down Expand Up @@ -182,7 +190,7 @@ void shouldRespectInProgressTimeout(int timeout, boolean isTimeoutExpected) {

@ParameterizedTest
@CsvSource({"0,0", "1,1"})
void shouldCleanupTimedOutFinishedJobs(int timeout, int expectedJobsCount) {
void shouldCleanupSuccessfullyFinishedJobs(int timeout, int expectedJobsCount) {
ExportParams exportParams = ExportParams.builder().build();
String finishedJobId = pdfConverterJobsService.startJob(exportParams, 1);
waitToFinishJob(finishedJobId);
Expand All @@ -192,6 +200,24 @@ void shouldCleanupTimedOutFinishedJobs(int timeout, int expectedJobsCount) {
assertThat(pdfConverterJobsService.getAllJobsStates()).hasSize(expectedJobsCount);
}

@ParameterizedTest
@CsvSource({"0,0", "1,1"})
void shouldCleanupFailedJobs(int timeout, int expectedJobsCount) {
lenient().when(securityService.doAsUser(any(), any(PrivilegedAction.class))).thenThrow(new RuntimeException("test error"));
ExportParams exportParams = ExportParams.builder().build();
String failedJobId = pdfConverterJobsService.startJob(exportParams, 1);
waitToFinishJob(failedJobId);

JobState jobState = pdfConverterJobsService.getJobState(failedJobId);
assertThat(jobState.isDone()).isTrue();
assertThat(jobState.isCompletedExceptionally()).isTrue();
assertThat(jobState.errorMessage()).contains("test error");

PdfConverterJobsService.cleanupExpiredJobs(timeout);

assertThat(pdfConverterJobsService.getAllJobsStates()).hasSize(expectedJobsCount);
}

@Test
@SuppressWarnings({"unchecked", "java:S2925"})
void shouldCleanupTimedOutInProgressJobs() {
Expand Down

0 comments on commit a26d5d6

Please sign in to comment.