From fdb6134bdd03bbe68aba1402f6db066477533d33 Mon Sep 17 00:00:00 2001 From: Jonathan Gamba Date: Tue, 3 Dec 2024 20:59:01 -0600 Subject: [PATCH] #30367 Improvements on the cancel process --- .../business/api/JobQueueManagerAPIImpl.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/dotCMS/src/main/java/com/dotcms/jobs/business/api/JobQueueManagerAPIImpl.java b/dotCMS/src/main/java/com/dotcms/jobs/business/api/JobQueueManagerAPIImpl.java index 7f191b17d333..4518d25ac972 100644 --- a/dotCMS/src/main/java/com/dotcms/jobs/business/api/JobQueueManagerAPIImpl.java +++ b/dotCMS/src/main/java/com/dotcms/jobs/business/api/JobQueueManagerAPIImpl.java @@ -404,7 +404,7 @@ public void cancelJob(final String jobId) throws DotDataException { final Job job = getJob(jobId); - if (job.state() == JobState.PENDING || job.state() == JobState.RUNNING) { + if (isInCancelableState(job)) { handleJobCancelRequest(job); } else { Logger.warn(this, "Job " + job.id() + " is not in a cancellable state. " @@ -427,9 +427,7 @@ void onCancelRequestJob(final JobCancelRequestEvent event) { try { final var job = getJob(event.getJob().id()); - if (job.state() == JobState.PENDING - || job.state() == JobState.RUNNING - || job.state() == JobState.CANCEL_REQUESTED) { + if (isInCancelableState(job) || job.state() == JobState.CANCEL_REQUESTED) { final Optional instance = getInstance(job.id()); if (instance.isPresent()) { @@ -1136,6 +1134,18 @@ private int incrementAndResetEmptyQueueCount( return emptyQueueCount; } + /** + * Verifies if a job state is in a cancellable state. + * + * @param job The job to check. + * @return {@code true} if the job is in a cancellable state, {@code false} otherwise. + */ + private boolean isInCancelableState(final Job job) { + return job.state() == JobState.PENDING || job.state() == JobState.RUNNING + || job.state() == JobState.FAILED || job.state() == JobState.ABANDONED + || job.state() == JobState.ABANDONED_PERMANENTLY; + } + /** * A wrapper class that makes ScheduledExecutorService auto-closeable. This class is designed to * be used with try-with-resources to ensure that the ScheduledExecutorService is properly shut