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/tasks/BackupTask.java |   40 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 39 insertions(+), 1 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tasks/BackupTask.java b/opendj-sdk/opends/src/server/org/opends/server/tasks/BackupTask.java
index f9aba64..bfdce39 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tasks/BackupTask.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tasks/BackupTask.java
@@ -26,6 +26,7 @@
  */
 package org.opends.server.tasks;
 import org.opends.messages.Message;
+import org.opends.messages.TaskMessages;
 
 import static org.opends.server.config.ConfigConstants.*;
 import static org.opends.server.core.DirectoryServer.getAttributeType;
@@ -133,6 +134,8 @@
   private File    backupDirectory;
   private String  incrementalBase;
 
+  private BackupConfig backupConfig;
+
   /**
    * All the backend configuration entries defined in the server mapped
    * by their backend ID.
@@ -460,7 +463,7 @@
 
 
     // Create a backup configuration.
-    BackupConfig backupConfig = new BackupConfig(backupDir, backupID,
+    backupConfig = new BackupConfig(backupDir, backupID,
                                                  incremental);
     backupConfig.setCompressData(compress);
     backupConfig.setEncryptData(encrypt);
@@ -560,6 +563,30 @@
   /**
    * {@inheritDoc}
    */
+  public void interruptTask(TaskState interruptState, Message interruptReason)
+  {
+    if (TaskState.STOPPED_BY_ADMINISTRATOR.equals(interruptState) &&
+            backupConfig != null)
+    {
+      addLogMessage(TaskMessages.INFO_TASK_STOPPED_BY_ADMIN.get(
+              interruptReason));
+      setTaskInterruptState(interruptState);
+      backupConfig.cancel();
+    }
+  }
+
+
+  /**
+   * {@inheritDoc}
+   */
+  public boolean isInterruptable() {
+    return true;
+  }
+
+
+  /**
+   * {@inheritDoc}
+   */
   protected TaskState runTask()
   {
     if (!argumentsAreValid())
@@ -585,6 +612,11 @@
     boolean errorsEncountered = false;
     for (Backend b : backendsToArchive)
     {
+      if (isCancelled())
+      {
+        break;
+      }
+
       // Acquire a shared lock for this backend.
       if (!lockBackend(b))
       {
@@ -639,6 +671,12 @@
       logError(message);
       return TaskState.COMPLETED_WITH_ERRORS;
     }
+    else if (isCancelled())
+    {
+      Message message = NOTE_BACKUPDB_CANCELLED.get();
+      logError(message);
+      return getTaskInterruptState();
+    }
     else
     {
       Message message = NOTE_BACKUPDB_COMPLETED_SUCCESSFULLY.get();

--
Gitblit v1.10.0