Add logic which delays initialization of the Orthanc plugin on first run of the server (Orthanc needs a chance to create tables if they don't exist)
The Sonador Resource cache (refer to !10 (merged)) introduced a SQLAlchemy database interface that allows the plugin to connect to and work with tables in the Orthanc database. This interface introspects the structure of the PostgreSQL tables at runtime and creates resource models that can be used for query.
On the first run of Orthanc, the C++ program introspects the structure of the database to determine if the schema matches the current version of Orthanc. If it does not, it will update the schema before starting the server. The current initialization of the SQLAlchemy interface does not allow for this process to complete. It runs before the C++ plugin completes its initialization and, if it can't find the resources and global properties table, will throw an error.
Logic needs to be added to the Orthanc Plugin so that it checks to see if autodetected tables exist, and if not, defers initialization of the resource cache until after they have been created. Subsequently, there needs to be a delay in the registration of all endpoints and events until after the database interface has been initialized.
Implementation notes:
- Refactor code and endpoint registration to occur within callbacks.
- Move init of the SQLAlchemy database interface to a callback invoked when the server has started.
- Introduce a new event "database:ready" (or similar)
- Initialize endpoints and other event within callbacks that trigger on "database:ready"