henrik242 commented on code in PR #26112:
URL: https://github.com/apache/camel/pull/26112#discussion_r3950111421


##########
core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java:
##########
@@ -163,22 +171,57 @@ public Future<?> schedule(CamelContext camelContext, 
BooleanSupplier supplier) {
         running.set(true);
         Future<?> future = service.scheduleWithFixedDelay(() -> 
runTaskWrapper(camelContext, supplier),
                 budget.initialDelay(), budget.interval(), 
TimeUnit.MILLISECONDS);
+        scheduledContext.set(camelContext);
         scheduledFuture.set(future);
         if (latch.getCount() == 0) {
             // the task already finished before the future was published, so 
it could not unschedule itself
-            unschedule();
+            unschedule(false);
         }
         return future;
     }
 
+    /**
+     * Cancels a task scheduled with {@link #schedule(CamelContext, 
BooleanSupplier)} that is no longer needed, and
+     * removes it from the {@link TaskManagerRegistry}. A scheduled task 
deregisters itself from one of its runs, which
+     * is not going to happen once the schedule is cancelled, so cancelling 
the returned {@link Future} directly leaves
+     * the task behind in the registry.
+     * <p/>
+     * This does not wait for an attempt that is already running: with {@code 
mayInterruptIfRunning} false, a supplier
+     * call that is in progress runs to completion after this method returns, 
and {@link #isAttempting()} keeps
+     * reporting it until it does.
+     *
+     * @param mayInterruptIfRunning whether the thread of an attempt that is 
currently running should be interrupted
+     */
+    public void cancel(boolean mayInterruptIfRunning) {
+        // any run that has not started yet becomes a no-op
+        latch.countDown();
+        unschedule(mayInterruptIfRunning);
+        if (status == Status.Active) {
+            status = Status.Inactive;
+        }
+        completed.set(false);
+        deregister();
+        running.set(false);

Review Comment:
   Guarded: completed.set(false) moved under the existing status == 
Status.Active check, so a Completed, Failed or Exhausted task keeps its 
outcome, and run() cannot be made to return false for a task that succeeded. 
Note compareAndSet(true, false) would have performed the clobber rather than 
prevented it. Covered by testCancelKeepsTheOutcomeOfACompletedTask.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to