changeset 827:558342e93c5e

Wait until Orthanc has finalized its startup before executing the DB HOUSEKEEPING task
author Alain Mazy <am@orthanc.team>
date Mon, 10 Aug 2026 16:56:56 +0200
parents baf483a8adea
children 80d9c8c77a91 70bd49304929
files Framework/Plugins/BaseIndexConnectionsPool.cpp Framework/Plugins/DatabaseBackendAdapterV4.cpp Framework/Plugins/DatabaseBackendAdapterV4.h Framework/Plugins/IndexBackend.h PostgreSQL/NEWS PostgreSQL/Plugins/PostgreSQLIndex.cpp PostgreSQL/Plugins/PostgreSQLIndex.h
diffstat 7 files changed, 65 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/BaseIndexConnectionsPool.cpp	Mon Aug 10 10:20:35 2026 +0200
+++ b/Framework/Plugins/BaseIndexConnectionsPool.cpp	Mon Aug 10 16:56:56 2026 +0200
@@ -30,7 +30,10 @@
 {
   void BaseIndexConnectionsPool::HousekeepingThread(BaseIndexConnectionsPool* that)
   {
-#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 2)
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 13, 0)
+    // Orthanc::Logging::ScopedCurrentThreadNameSetter setter("DB HOUSEKEEPING");
+    OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "DB HOUSEKEEPING");    
+#elif ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 2)
     OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "DB HOUSEKEEPING");    
 #endif
 
@@ -44,7 +47,7 @@
         {
           {
             Accessor accessor(*that);
-            accessor.GetBackend().PerformDbHousekeeping(accessor.GetManager());
+            accessor.GetBackend().PerformDbHousekeeping(accessor.GetManager());  //
           }
 
           that->PerformPoolHousekeeping();
@@ -63,6 +66,8 @@
 
       boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
     }
+
+    LOG(INFO) << "database housekeeping has stopped";
   }
 
 
--- a/Framework/Plugins/DatabaseBackendAdapterV4.cpp	Mon Aug 10 10:20:35 2026 +0200
+++ b/Framework/Plugins/DatabaseBackendAdapterV4.cpp	Mon Aug 10 16:56:56 2026 +0200
@@ -1795,6 +1795,23 @@
     OrthancPlugins::AnswerJson(jsonLogs, output);
   }
 
+
+  OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
+                                          OrthancPluginResourceType resourceType,
+                                          const char* resourceId)
+  {
+    switch (changeType)
+    {
+      case OrthancPluginChangeType_OrthancStarted:
+        DatabaseBackendAdapterV4::SetOrthancStarted();
+        break;
+      default:
+        break;
+    }
+
+    return OrthancPluginErrorCode_Success;
+  }
+
   void DatabaseBackendAdapterV4::Register(IndexBackend* backend,
                                           size_t countConnections,
                                           bool useDynamicConnectionPool,
@@ -1832,6 +1849,8 @@
     OrthancPluginRegisterAuditLogHandler(context, AuditLogHandler);
     OrthancPlugins::RegisterRestCallback<GetAuditLogs>("/plugins/postgresql/audit-logs", true);
 #endif
+
+    OrthancPluginRegisterOnChangeCallback(context, OnChangeCallback);
   }
 
 
@@ -1842,6 +1861,18 @@
       LOG(ERROR) << "The Orthanc core has not destructed the index backend, internal error";
     }
   }
+
+  void DatabaseBackendAdapterV4::SetOrthancStarted()
+  {
+    if (!isBackendInUse_)
+    {
+      LOG(ERROR) << "The Orthanc core has not created the index backend, internal error";
+      return;
+    }
+    
+    BaseIndexConnectionsPool::Accessor accessor(*connectionPool_);
+    accessor.GetBackend().SetOrthancStarted();
+  }
 }
 
 #  endif
--- a/Framework/Plugins/DatabaseBackendAdapterV4.h	Mon Aug 10 10:20:35 2026 +0200
+++ b/Framework/Plugins/DatabaseBackendAdapterV4.h	Mon Aug 10 16:56:56 2026 +0200
@@ -55,6 +55,8 @@
                          unsigned int housekeepingDelaySeconds);
 
     static void Finalize();
+
+    static void SetOrthancStarted();
   };
 }
 
--- a/Framework/Plugins/IndexBackend.h	Mon Aug 10 10:20:35 2026 +0200
+++ b/Framework/Plugins/IndexBackend.h	Mon Aug 10 16:56:56 2026 +0200
@@ -558,6 +558,10 @@
       throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, "PerformDbHousekeeping should be overloaded");
     }
 
+    virtual void SetOrthancStarted()
+    {
+    }
+
     /**
      * "maxDatabaseRetries" is to handle
      * "OrthancPluginErrorCode_DatabaseCannotSerialize" if there is a
--- a/PostgreSQL/NEWS	Mon Aug 10 10:20:35 2026 +0200
+++ b/PostgreSQL/NEWS	Mon Aug 10 16:56:56 2026 +0200
@@ -1,3 +1,10 @@
+Pending changes in the mainline
+===============================
+
+Changes:
+* Wait until Orthanc has finalized its startup before executing the DB HOUSEKEEPING task to avoid 
+  "Plugin trying to call the database during its initialization" errors.
+
 Release 10.2 (2026-06-23)
 =========================
 
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Mon Aug 10 10:20:35 2026 +0200
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Mon Aug 10 16:56:56 2026 +0200
@@ -59,7 +59,8 @@
     IndexBackend(context, readOnly, parameters.GetAllowInconsistentChildCounts()),
     parameters_(parameters),
     clearAll_(false),
-    hkHasComputedAllMissingChildCount_(false)
+    hkHasComputedAllMissingChildCount_(false),
+    orthancHasStarted_(false)
   {
   }
 
@@ -961,6 +962,12 @@
 
   void PostgreSQLIndex::PerformDbHousekeeping(DatabaseManager& manager)
   {
+    if (!orthancHasStarted_)
+    {
+      LOG(INFO) << "Waiting for Orthanc to finalize its initialization";
+      return;
+    }
+
     // Compute the missing child count (table introduced in rev3)
     if (!hkHasComputedAllMissingChildCount_)
     {
--- a/PostgreSQL/Plugins/PostgreSQLIndex.h	Mon Aug 10 10:20:35 2026 +0200
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.h	Mon Aug 10 16:56:56 2026 +0200
@@ -36,6 +36,7 @@
     PostgreSQLParameters   parameters_;
     bool                   clearAll_;
     bool                   hkHasComputedAllMissingChildCount_;
+    bool                   orthancHasStarted_;
 
   protected:
     virtual void ClearDeletedFiles(DatabaseManager& manager) ORTHANC_OVERRIDE;
@@ -194,5 +195,10 @@
 
     virtual void PerformDbHousekeeping(DatabaseManager& manager) ORTHANC_OVERRIDE;
 
+    virtual void SetOrthancStarted() ORTHANC_OVERRIDE
+    {
+      orthancHasStarted_ = true;
+    }
+
   };
 }