annotate Framework/Common/RetryDatabaseFactory.cpp @ 297:86265ef5f3e3

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Jul 2021 11:31:16 +0200
parents 39cb02035f54
children 16aac0287485
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
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * 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
9 * as published by the Free Software Foundation, either version 3 of
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * 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
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * 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
18 * 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
19 **/
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 #include "RetryDatabaseFactory.h"
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <Logging.h>
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <OrthancException.h>
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <boost/thread.hpp>
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
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 namespace OrthancDatabases
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 IDatabase* RetryDatabaseFactory::Open()
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 unsigned int count = 0;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 for (;;)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 try
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 return TryOpen();
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 catch (Orthanc::OrthancException& e)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 count ++;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 if (count <= maxConnectionRetries_)
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 LOG(WARNING) << "Database is currently unavailable, retrying...";
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 boost::this_thread::sleep(boost::posix_time::seconds(connectionRetryInterval_));
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 continue;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 }
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 else
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 LOG(ERROR) << "Timeout when connecting to the database, giving up";
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
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 throw;
39cb02035f54 added missing files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
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 }