changeset 5364:b5f2122a1334

Added a route to delete completed jobs from history: DELETE /jobs/{id}
author Alain Mazy <am@osimis.io>
date Thu, 20 Jul 2023 10:51:34 +0200
parents 78aad3916da4
children 5653527a2b1d
files NEWS OrthancFramework/Sources/JobsEngine/IJob.h OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp OrthancFramework/Sources/JobsEngine/JobsRegistry.h OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.h
diffstat 7 files changed, 84 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Jul 13 19:33:10 2023 +0200
+++ b/NEWS	Thu Jul 20 10:51:34 2023 +0200
@@ -6,6 +6,12 @@
 
 * Prevent the leak of the full path of the source files in the binaries
 
+REST API
+--------
+
+* API version upgraded to 22
+* Added a route to delete completed jobs from history: DELETE /jobs/{id}
+
 
 Version 1.12.1 (2023-07-04)
 ===========================
--- a/OrthancFramework/Sources/JobsEngine/IJob.h	Thu Jul 13 19:33:10 2023 +0200
+++ b/OrthancFramework/Sources/JobsEngine/IJob.h	Thu Jul 20 10:51:34 2023 +0200
@@ -66,5 +66,9 @@
     // This function can only be called if the job has reached its
     // "success" state
     virtual bool DeleteOutput(const std::string& key) = 0;
+
+    // This function can only be called if the job has reached its
+    // "success" state
+    virtual void DeleteAllOutputs() {}
   };
 }
--- a/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Thu Jul 13 19:33:10 2023 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Thu Jul 20 10:51:34 2023 +0200
@@ -649,6 +649,42 @@
   }
 
 
+  bool JobsRegistry::DeleteJobInfo(const std::string& id)
+  {
+    LOG(INFO) << "Deleting job: " << id;
+
+    boost::mutex::scoped_lock lock(mutex_);
+    CheckInvariants();
+
+    JobsIndex::iterator found = jobsIndex_.find(id);
+
+    if (found == jobsIndex_.end())
+    {
+      LOG(WARNING) << "Unknown job to delete: " << id;
+      return false;
+    }
+    else
+    {
+      for (CompletedJobs::iterator it = completedJobs_.begin();
+           it != completedJobs_.end(); ++it)
+      {
+        if (*it == found->second)
+        {
+          found->second->GetJob().DeleteAllOutputs();
+          delete found->second;
+          
+          completedJobs_.erase(it);
+          jobsIndex_.erase(id);
+          return true;
+        }
+      }
+
+      LOG(WARNING) << "Can not delete a job that is not complete: " << id;
+      return false;
+    }
+  }
+
+
   bool JobsRegistry::GetJobOutput(std::string& output,
                                   MimeType& mime,
                                   std::string& filename,
--- a/OrthancFramework/Sources/JobsEngine/JobsRegistry.h	Thu Jul 13 19:33:10 2023 +0200
+++ b/OrthancFramework/Sources/JobsEngine/JobsRegistry.h	Thu Jul 20 10:51:34 2023 +0200
@@ -147,6 +147,8 @@
     bool GetJobInfo(JobInfo& target,
                     const std::string& id);
 
+    bool DeleteJobInfo(const std::string& id);
+
     bool GetJobOutput(std::string& output,
                       MimeType& mime,
                       std::string& filename,
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp	Thu Jul 13 19:33:10 2023 +0200
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp	Thu Jul 20 10:51:34 2023 +0200
@@ -729,6 +729,34 @@
     }
   }
 
+  static void DeleteJobInfo(RestApiDeleteCall& call)
+  {
+    if (call.IsDocumentation())
+    {
+      call.GetDocumentation()
+        .SetTag("Jobs")
+        .SetSummary("Delete a job from history")
+        .SetDescription("Delete the job from the jobs history.  Only a completed job can be deleted. "
+                        "If the job has not run or not completed yet, you must cancel it first. "
+                        "If the job has outputs, all outputs will be deleted as well. ")
+        .SetUriArgument("id", "Identifier of the job of interest");
+      return;
+    }
+
+    std::string job = call.GetUriComponent("id", "");
+
+    if (OrthancRestApi::GetContext(call).GetJobsEngine().
+        GetRegistry().DeleteJobInfo(job))
+    {
+      call.GetOutput().AnswerBuffer("", MimeType_PlainText);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InexistentItem,
+                             "No job found with this id: " + job);
+    }
+  }
+
 
   static void GetJobOutput(RestApiGetCall& call)
   {
@@ -1138,6 +1166,7 @@
 
     Register("/jobs", ListJobs);
     Register("/jobs/{id}", GetJobInfo);
+    Register("/jobs/{id}", DeleteJobInfo);
     Register("/jobs/{id}/cancel", ApplyJobAction<JobAction_Cancel>);
     Register("/jobs/{id}/pause", ApplyJobAction<JobAction_Pause>);
     Register("/jobs/{id}/resubmit", ApplyJobAction<JobAction_Resubmit>);
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Thu Jul 13 19:33:10 2023 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Thu Jul 20 10:51:34 2023 +0200
@@ -1482,4 +1482,9 @@
       return false;
     }
   }
+
+  void ArchiveJob::DeleteAllOutputs()
+  {
+    DeleteOutput("archive");
+  }
 }
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Thu Jul 13 19:33:10 2023 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Thu Jul 20 10:51:34 2023 +0200
@@ -122,5 +122,7 @@
                            const std::string& key) ORTHANC_OVERRIDE;
 
     virtual bool DeleteOutput(const std::string& key) ORTHANC_OVERRIDE;
+
+    virtual void DeleteAllOutputs() ORTHANC_OVERRIDE;
   };
 }