From 742e19dca031dc15aa54ea9b0e64bf485478627d Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Tue, 16 Oct 2007 20:19:45 +0000
Subject: [PATCH] Issue 2368: tasks should be interruptable.  The four schedulable task (import-ldif, export-ldif, backup, and restore) can now be interrupted for purposes of cancellation.  The manage-tasks utility now allows the user to cancel any one of these tasks if they are currently running.  If interrupted while executing, the tasks try to break out of their work loop as soon as possible and return a 'stopped by administrator' status.  Both the backup and export-ldif tasks perform some cleanup (removing the abandoned backup or exported LDIF file) if they are cancelled.

---
 opendj-sdk/opends/src/server/org/opends/server/backends/task/Task.java |   71 +++++++++++++++++++++++++++++++++++
 1 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/task/Task.java b/opendj-sdk/opends/src/server/org/opends/server/backends/task/Task.java
index e26ec54..f4805bb 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/task/Task.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/task/Task.java
@@ -142,6 +142,9 @@
   // The current state of this task.
   private TaskState taskState;
 
+  // The task state that may be set when the task is interrupted.
+  private TaskState taskInterruptState;
+
   // The scheduler with which this task is associated.
   private TaskScheduler taskScheduler;
 
@@ -577,6 +580,18 @@
     return taskState;
   }
 
+  /**
+   * Indicates whether or not this task has been cancelled.
+   *
+   * @return boolean where true indicates that this task was
+   *         cancelled either before or during execution
+   */
+  public boolean isCancelled()
+  {
+    return taskInterruptState != null &&
+      TaskState.isCancelled(taskInterruptState);
+  }
+
 
 
   /**
@@ -626,6 +641,58 @@
     }
   }
 
+
+  /**
+   * Sets a state for this task that is the result of a call to
+   * {@link #interruptTask(TaskState, org.opends.messages.Message)}.
+   * It may take this task some time to actually cancel to that
+   * actual state may differ until quiescence.
+   *
+   * @param state for this task once it has canceled whatever it is doing
+   */
+  protected void setTaskInterruptState(TaskState state)
+  {
+    this.taskInterruptState = state;
+  }
+
+
+  /**
+   * Gets the interrupt state for this task that was set as a
+   * result of a call to {@link #interruptTask(TaskState,
+   * org.opends.messages.Message)}.
+   *
+   * @return interrupt state for this task
+   */
+  protected TaskState getTaskInterruptState()
+  {
+    return this.taskInterruptState;
+  }
+
+
+  /**
+   * Returns a state for this task after processing has completed.
+   * If the task was interrupted with a call to
+   * {@link #interruptTask(TaskState, org.opends.messages.Message)}
+   * then that method's interruptState is returned here.  Otherwse
+   * this method returns TaskState.COMPLETED_SUCCESSFULLY.  It is
+   * assumed that if there were errors during task processing that
+   * task state will have been derived in some other way.
+   *
+   * @return state for this task after processing has completed
+   */
+  protected TaskState getFinalTaskState()
+  {
+    if (this.taskInterruptState == null)
+    {
+      return TaskState.COMPLETED_SUCCESSFULLY;
+    }
+    else
+    {
+      return this.taskInterruptState;
+    }
+  }
+
+
   /**
    * Replaces an attribute values of the task entry.
    *
@@ -1241,6 +1308,10 @@
    * gracefully interrupt a task, then subclasses should override this method to
    * do so.
    *
+   * Implementations of this method are exprected to call
+   * {@link #setTaskInterruptState(TaskState)} if the interruption is accepted
+   * by this task.
+   *
    * @param  interruptState   The state to use for the task if it is
    *                          successfully interrupted.
    * @param  interruptReason  A human-readable explanation for the cancellation.

--
Gitblit v1.10.0