comparison OrthancServer/main.cpp @ 1615:c40fe92a68e7

Primitives to upgrade the database version in plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Sep 2015 15:18:59 +0200
parents d73124f6b439
children ffd23c0104af
comparison
equal deleted inserted replaced
1614:1c9e99d2bfd2 1615:c40fe92a68e7
313 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl 313 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl
314 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl 314 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl
315 << std::endl 315 << std::endl
316 << "If no configuration file is given on the command line, a set of default " << std::endl 316 << "If no configuration file is given on the command line, a set of default " << std::endl
317 << "parameters is used. Please refer to the Orthanc homepage for the full " << std::endl 317 << "parameters is used. Please refer to the Orthanc homepage for the full " << std::endl
318 << "instructions about how to use Orthanc " << std::endl 318 << "instructions about how to use Orthanc <http://www.orthanc-server.com/>." << std::endl
319 << "<https://code.google.com/p/orthanc/wiki/OrthancCookbook>." << std::endl
320 << std::endl 319 << std::endl
321 << "Command-line options:" << std::endl 320 << "Command-line options:" << std::endl
322 << " --help\t\tdisplay this help and exit" << std::endl 321 << " --help\t\tdisplay this help and exit" << std::endl
323 << " --logdir=[dir]\tdirectory where to store the log files" << std::endl 322 << " --logdir=[dir]\tdirectory where to store the log files" << std::endl
324 << "\t\t\t(if not used, the logs are dumped to stderr)" << std::endl 323 << "\t\t\t(if not used, the logs are dumped to stderr)" << std::endl
325 << " --config=[file]\tcreate a sample configuration file and exit" << std::endl 324 << " --config=[file]\tcreate a sample configuration file and exit" << std::endl
325 << " --verbose\t\tbe verbose in logs" << std::endl
326 << " --trace\t\thighest verbosity in logs (for debug)" << std::endl 326 << " --trace\t\thighest verbosity in logs (for debug)" << std::endl
327 << " --verbose\t\tbe verbose in logs" << std::endl 327 << " --upgrade\t\tallow Orthanc to upgrade the version of the" << std::endl
328 << "\t\t\tdatabase (beware that the database will become" << std::endl
329 << "\t\t\tincompatible with former versions of Orthanc)" << std::endl
328 << " --version\t\toutput version information and exit" << std::endl 330 << " --version\t\toutput version information and exit" << std::endl
329 << std::endl 331 << std::endl
330 << "Exit status:" << std::endl 332 << "Exit status:" << std::endl
331 << " 0 if OK," << std::endl 333 << " 0 if success," << std::endl
332 << " -1 if error (have a look at the logs)." << std::endl 334 << " -1 if error (have a look at the logs)." << std::endl
333 << std::endl; 335 << std::endl;
334 } 336 }
335 337
336 338
337 static void PrintVersion(char* path) 339 static void PrintVersion(char* path)
517 519
518 return StartDicomServer(context, restApi); 520 return StartDicomServer(context, restApi);
519 } 521 }
520 522
521 523
524 static bool UpgradeDatabase(IDatabaseWrapper& database,
525 IStorageArea& storageArea,
526 bool allowDatabaseUpgrade)
527 {
528 // Upgrade the database, if needed
529 unsigned int currentVersion = database.GetDatabaseVersion();
530 if (currentVersion == ORTHANC_DATABASE_VERSION)
531 {
532 return true;
533 }
534
535 if (!allowDatabaseUpgrade)
536 {
537 LOG(ERROR) << "The database must be upgraded from version "
538 << currentVersion << " to " << ORTHANC_DATABASE_VERSION
539 << ": Please run Orthanc with the \"--upgrade\" command-line option";
540 return false;
541 }
542
543 LOG(WARNING) << "Upgrading the database from version "
544 << currentVersion << " to " << ORTHANC_DATABASE_VERSION;
545 database.Upgrade(ORTHANC_DATABASE_VERSION, storageArea);
546
547 // Sanity check
548 currentVersion = database.GetDatabaseVersion();
549 if (ORTHANC_DATABASE_VERSION != currentVersion)
550 {
551 LOG(ERROR) << "The database was not properly updated, it is still at version " << currentVersion;
552 throw OrthancException(ErrorCode_InternalError);
553 }
554
555 return true;
556 }
557
558
522 static bool ConfigureServerContext(IDatabaseWrapper& database, 559 static bool ConfigureServerContext(IDatabaseWrapper& database,
523 IStorageArea& storageArea, 560 IStorageArea& storageArea,
524 OrthancPlugins *plugins) 561 OrthancPlugins *plugins,
525 { 562 bool allowDatabaseUpgrade)
563 {
564 if (!UpgradeDatabase(database, storageArea, allowDatabaseUpgrade))
565 {
566 return false;
567 }
568
526 ServerContext context(database, storageArea); 569 ServerContext context(database, storageArea);
527 570
528 HttpClient::SetDefaultTimeout(Configuration::GetGlobalIntegerParameter("HttpTimeout", 0)); 571 HttpClient::SetDefaultTimeout(Configuration::GetGlobalIntegerParameter("HttpTimeout", 0));
529 context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false)); 572 context.SetCompressionEnabled(Configuration::GetGlobalBoolParameter("StorageCompression", false));
530 context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true)); 573 context.SetStoreMD5ForAttachments(Configuration::GetGlobalBoolParameter("StoreMD5ForAttachments", true));
567 return restart; 610 return restart;
568 } 611 }
569 612
570 613
571 static bool ConfigurePlugins(int argc, 614 static bool ConfigurePlugins(int argc,
572 char* argv[]) 615 char* argv[],
616 bool allowDatabaseUpgrade)
573 { 617 {
574 std::auto_ptr<IDatabaseWrapper> databasePtr; 618 std::auto_ptr<IDatabaseWrapper> databasePtr;
575 std::auto_ptr<IStorageArea> storage; 619 std::auto_ptr<IStorageArea> storage;
576 620
577 #if ORTHANC_PLUGINS_ENABLED == 1 621 #if ORTHANC_PLUGINS_ENABLED == 1
602 } 646 }
603 647
604 assert(database != NULL); 648 assert(database != NULL);
605 assert(storage.get() != NULL); 649 assert(storage.get() != NULL);
606 650
607 return ConfigureServerContext(*database, *storage, &plugins); 651 return ConfigureServerContext(*database, *storage, &plugins, allowDatabaseUpgrade);
608 652
609 #elif ORTHANC_PLUGINS_ENABLED == 0 653 #elif ORTHANC_PLUGINS_ENABLED == 0
610 // The plugins are disabled 654 // The plugins are disabled
611 databasePtr.reset(Configuration::CreateDatabaseWrapper()); 655 databasePtr.reset(Configuration::CreateDatabaseWrapper());
612 storage.reset(Configuration::CreateStorageArea()); 656 storage.reset(Configuration::CreateStorageArea());
613 657
614 return ConfigureServerContext(*databasePtr, *storage, NULL); 658 return ConfigureServerContext(*databasePtr, *storage, NULL, allowDatabaseUpgrade);
615 659
616 #else 660 #else
617 # error The macro ORTHANC_PLUGINS_ENABLED must be set to 0 or 1 661 # error The macro ORTHANC_PLUGINS_ENABLED must be set to 0 or 1
618 #endif 662 #endif
619 } 663 }
620 664
621 665
622 static bool StartOrthanc(int argc, char* argv[]) 666 static bool StartOrthanc(int argc,
623 { 667 char* argv[],
624 return ConfigurePlugins(argc, argv); 668 bool allowDatabaseUpgrade)
669 {
670 return ConfigurePlugins(argc, argv, allowDatabaseUpgrade);
625 } 671 }
626 672
627 673
628 int main(int argc, char* argv[]) 674 int main(int argc, char* argv[])
629 { 675 {
630 Logging::Initialize(); 676 Logging::Initialize();
677
678 bool allowDatabaseUpgrade = false;
631 679
632 for (int i = 1; i < argc; i++) 680 for (int i = 1; i < argc; i++)
633 { 681 {
634 if (std::string(argv[i]) == "--help") 682 if (std::string(argv[i]) == "--help")
635 { 683 {
666 fprintf(stderr, "The directory where to store the log files (%s) is inexistent, aborting.\n", directory.c_str()); 714 fprintf(stderr, "The directory where to store the log files (%s) is inexistent, aborting.\n", directory.c_str());
667 return -1; 715 return -1;
668 } 716 }
669 } 717 }
670 718
719 if (std::string(argv[i]) == "--upgrade")
720 {
721 allowDatabaseUpgrade = true;
722 }
723
671 if (boost::starts_with(argv[i], "--config=")) 724 if (boost::starts_with(argv[i], "--config="))
672 { 725 {
673 std::string configurationSample; 726 std::string configurationSample;
674 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE); 727 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE);
675 728
704 { 757 {
705 for (;;) 758 for (;;)
706 { 759 {
707 OrthancInitialize(configurationFile); 760 OrthancInitialize(configurationFile);
708 761
709 bool restart = StartOrthanc(argc, argv); 762 bool restart = StartOrthanc(argc, argv, allowDatabaseUpgrade);
710 if (restart) 763 if (restart)
711 { 764 {
712 OrthancFinalize(); 765 OrthancFinalize();
713 } 766 }
714 else 767 else