annotate Framework/Common/RetryDatabaseFactory.cpp @ 415:7e123f047771

LookupResources optimization continued
author Alain Mazy <am@osimis.io>
date Thu, 22 Jun 2023 18:07:41 +0200
parents 3d6886f3e5b3
children ecd0b719cff5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
227
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
389
3d6886f3e5b3 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 359
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
3d6886f3e5b3 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 359
diff changeset
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
227
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU Affero General Public License
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * Affero General Public License for more details.
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU Affero General Public License
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "RetryDatabaseFactory.h"
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <Logging.h>
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <OrthancException.h>
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <boost/thread.hpp>
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace OrthancDatabases
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 IDatabase* RetryDatabaseFactory::Open()
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 unsigned int count = 0;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 for (;;)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 try
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 return TryOpen();
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 catch (Orthanc::OrthancException& e)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 count ++;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 if (count <= maxConnectionRetries_)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 LOG(WARNING) << "Database is currently unavailable, retrying...";
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 boost::this_thread::sleep(boost::posix_time::seconds(connectionRetryInterval_));
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 continue;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 else
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 LOG(ERROR) << "Timeout when connecting to the database, giving up";
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 throw;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }