Skip to content

Commit

Permalink
HTM-1295: add last result to api response at api/admin/tasks (#1018)
Browse files Browse the repository at this point in the history
* HTM-1295: add last result to api response at api/admin/tasks 

* Add LAST_RESULT_KEY to task interface and use the key
  • Loading branch information
steinkobben authored Nov 5, 2024
1 parent cde9ed1 commit 82557d9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public ResponseEntity<Object> list(@RequestParam(required = false) String type)
.put(
Task.DESCRIPTION_KEY,
jobDetail.getJobDataMap().getString(Task.DESCRIPTION_KEY))
.put(
Task.LAST_RESULT_KEY,
jobDetail.getJobDataMap().getString(Task.LAST_RESULT_KEY))
.putPOJO(Task.STATE_KEY, state));
});
} catch (SchedulerException e) {
Expand Down Expand Up @@ -246,7 +249,7 @@ public ResponseEntity<Object> details(@PathVariable TaskType type, @PathVariable
"nextFireTimes", TriggerUtils.computeFireTimes((OperableTrigger) cron, null, 5))
.putPOJO(Task.STATE_KEY, scheduler.getTriggerState(trigger.getKey()))
.putPOJO("progress", result[0])
.put("lastResult", jobDataMap.getString("lastResult"))
.put(Task.LAST_RESULT_KEY, jobDataMap.getString(Task.LAST_RESULT_KEY))
.putPOJO("jobData", jobDataMap));
} catch (SchedulerException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Error getting task", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void executeInternal(JobExecutionContext context) throws JobExecutionE
throw new UnsupportedOperationException("Failing POC task failed.");
} catch (Exception e) {
logger.error("Failing POC task failed.", e);
jobDataMap.put("lastResult", "POC task executed unsuccessfully");
jobDataMap.put(Task.LAST_RESULT_KEY, "POC task executed unsuccessfully");
context.setResult("POC task executed unsuccessfully");
throw new JobExecutionException(e);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/tailormap/api/scheduling/IndexTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ protected void executeInternal(@NonNull JobExecutionContext context)
persistedJobData.put(
"executions", (1 + (int) context.getMergedJobDataMap().getOrDefault("executions", 0)));
persistedJobData.put("lastExecutionFinished", Instant.now());
persistedJobData.put("lastResult", "Index task executed successfully");
persistedJobData.put(Task.LAST_RESULT_KEY, "Index task executed successfully");
context.setResult("Index task executed successfully");
} catch (UnsupportedOperationException | IOException | SolrServerException | SolrException e) {
logger.error("Error indexing", e);
searchIndex.setStatus(SearchIndex.Status.ERROR).setComment(e.getMessage());
persistedJobData.put("lastExecutionFinished", null);
persistedJobData.put(
"lastResult", "Index task failed with " + e.getMessage() + ". Check logs for details");
Task.LAST_RESULT_KEY,
"Index task failed with " + e.getMessage() + ". Check logs for details");
searchIndexRepository.save(searchIndex);
context.setResult("Error indexing. Check logs for details.");
throw new JobExecutionException("Error indexing", e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tailormap/api/scheduling/PocTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected void executeInternal(@NonNull JobExecutionContext context) {
int executions = (1 + (int) mergedJobDataMap.getOrDefault("executions", 0));
jobDataMap.put("executions", executions);
jobDataMap.put("lastExecutionFinished", Instant.now());
jobDataMap.put("lastResult", "POC task executed successfully");
jobDataMap.put(Task.LAST_RESULT_KEY, "POC task executed successfully");
context.setResult("POC task executed successfully");

setFoo("foo executed: " + executions);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/tailormap/api/scheduling/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface Task {
String CRON_EXPRESSION_KEY = "cronExpression";
String PRIORITY_KEY = "priority";
String STATE_KEY = "state";
String LAST_RESULT_KEY = "lastResult";

/**
* Get the type of the task. Implement this method to return the key for the type of task. This
Expand Down

0 comments on commit 82557d9

Please sign in to comment.