Skip to content

Commit 378e407

Browse files
author
César Román
authored
feat(db): handle concurrent DisposableConnection connections (#142)
Closes: #12
1 parent f41eb31 commit 378e407

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

Diff for: .pre-commit-config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ repos:
1515
rev: 5.12.0
1616
hooks:
1717
- id: isort
18+
- repo: https://github.com/PyCQA/docformatter
19+
rev: v1.5.1
20+
hooks:
21+
- id: docformatter
22+
args: [--in-place, --wrap-summaries=72, --close-quotes-on-newline]
1823
- repo: https://github.com/PyCQA/flake8
1924
rev: 5.0.4
2025
hooks:

Diff for: setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ project_urls =
3838
[options]
3939
packages = find:
4040
install_requires =
41-
ignition-api
41+
ignition-api>=8.1.0
4242
python_requires = ==2.7.18
4343
package_dir =
4444
=src

Diff for: src/incendium/db.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Any, List, Optional, Tuple, Type, TypedDict, Union
1919

2020
import system.db
21+
import system.util
2122
from com.inductiveautomation.ignition.common import BasicDataset
2223
from java.lang import Thread
2324

@@ -42,9 +43,6 @@ class DisposableConnection(object):
4243
resources.
4344
"""
4445

45-
database = None # type: AnyStr
46-
retries = None # type: int
47-
4846
def __init__(self, database, retries=3):
4947
# type: (AnyStr, int) -> None
5048
"""Disposable Connection initializer.
@@ -56,14 +54,21 @@ def __init__(self, database, retries=3):
5654
enabling the connection. Optional.
5755
"""
5856
super(DisposableConnection, self).__init__()
59-
self.database = database
60-
self.retries = retries
57+
self._database = database
58+
self._retries = retries
59+
self._global_conn = "incendium_db_{}".format(database)
60+
61+
@property
62+
def database(self):
63+
# type: () -> AnyStr
64+
"""Get the name of the disposable connection."""
65+
return self._database
6166

6267
@property
6368
def status(self):
6469
# type: () -> AnyStr
6570
"""Get connection status."""
66-
connection_info = system.db.getConnectionInfo(self.database)
71+
connection_info = system.db.getConnectionInfo(self._database)
6772
return str(connection_info.getValueAt(0, "Status"))
6873

6974
def __enter__(self):
@@ -74,22 +79,25 @@ def __enter__(self):
7479
IOError: If the connection's status reports as Faulted, or
7580
ir cannot be enabled.
7681
"""
77-
system.db.setDatasourceEnabled(self.database, True)
82+
system.db.setDatasourceEnabled(self._database, True)
7883

79-
for _ in range(self.retries):
84+
for _ in range(self._retries):
8085
Thread.sleep(1000)
8186
if self.status == "Valid":
87+
if self._global_conn not in system.util.globals:
88+
system.util.globals[self._global_conn] = 0
89+
system.util.globals[self._global_conn] += 1
8290
break
8391
if self.status == "Faulted":
8492
raise IOError(
8593
"The database connection {!r} is {}.".format(
86-
self.database, self.status
94+
self._database, self.status
8795
)
8896
)
8997
else:
9098
raise IOError(
9199
"The database connection {!r} could not be enabled.".format(
92-
self.database
100+
self._database
93101
)
94102
)
95103
return self
@@ -102,7 +110,9 @@ def __exit__(
102110
):
103111
# type: (...) -> None
104112
"""Exit the runtime context related to this object."""
105-
system.db.setDatasourceEnabled(self.database, False)
113+
system.util.globals[self._global_conn] -= 1
114+
if system.util.globals[self._global_conn] == 0:
115+
system.db.setDatasourceEnabled(self._database, False)
106116

107117

108118
class Param(object):

Diff for: tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ deps =
4747
isort
4848
pydocstyle
4949
sort-all
50+
ssort
5051
commands =
5152
bash -c 'sort-all $(find src -name "*.py" -type f)'
53+
ssort src
5254
black --quiet src
5355
isort src
5456
flake8 src

0 commit comments

Comments
 (0)