Skip to content

Commit

Permalink
Merge pull request #142 from guzman-raphael/memory-leak
Browse files Browse the repository at this point in the history
Ensure connections are thread-safe
  • Loading branch information
jverswijver committed Nov 3, 2022
2 parents 4f745c1 + 1970c44 commit a9b2d8f
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 159 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.

## [0.6.0] - 2022-11-03

### Added

- Allow requests to be thread-safe so that database connections don't cross-pollinate [#142](https://github.com/datajoint/pharus/pull/142)
- Add `basicquery` and `external` as options to extend with dynamic spec [#142](https://github.com/datajoint/pharus/pull/142)

## [0.5.6] - 2022-10-29

### Added
Expand Down Expand Up @@ -200,6 +207,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
- Support for DataJoint attribute types: `varchar`, `int`, `float`, `datetime`, `date`, `time`, `decimal`, `uuid`.
- Check dependency utility to determine child table references.

[0.6.0]: https://github.com/datajoint/pharus/compare/0.5.6...0.6.0
[0.5.6]: https://github.com/datajoint/pharus/compare/0.5.5...0.5.6
[0.5.5]: https://github.com/datajoint/pharus/compare/0.5.4...0.5.5
[0.5.4]: https://github.com/datajoint/pharus/compare/0.5.3...0.5.4
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ To start the API server, use the command:

.. code-block:: bash
PHARUS_VERSION=0.5.6 docker-compose -f docker-compose-deploy.yaml up -d
PHARUS_VERSION=0.6.0 docker-compose -f docker-compose-deploy.yaml up -d
To stop the API server, use the command:

.. code-block:: bash
PHARUS_VERSION=0.5.6 docker-compose -f docker-compose-deploy.yaml down
PHARUS_VERSION=0.6.0 docker-compose -f docker-compose-deploy.yaml down
References
----------
Expand Down
9 changes: 4 additions & 5 deletions docker-compose-deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# PHARUS_VERSION=0.5.6 docker-compose -f docker-compose-deploy.yaml pull
# PHARUS_VERSION=0.5.6 docker-compose -f docker-compose-deploy.yaml up -d
# PHARUS_VERSION=0.6.0 docker-compose -f docker-compose-deploy.yaml pull
# PHARUS_VERSION=0.6.0 docker-compose -f docker-compose-deploy.yaml up -d
#
# Intended for production deployment.
# Note: You must run both commands above for minimal outage
Expand All @@ -8,8 +8,7 @@
# With this config and the configuration below in NGINX, you should be able to verify it is
# running properly with a `curl https://fakeservices.datajoint.io/api/version`.
version: "2.4"
x-net:
&net
x-net: &net
networks:
- main
services:
Expand All @@ -22,7 +21,7 @@ services:
# - PHARUS_SPEC_PATH=tests/init/test_dynamic_api_spec.yaml # for dynamic api spec
fakeservices.datajoint.io:
<<: *net
image: datajoint/nginx:v0.0.16
image: datajoint/nginx:v0.2.3
environment:
- ADD_pharus_TYPE=REST
- ADD_pharus_ENDPOINT=pharus:5000
Expand Down
10 changes: 3 additions & 7 deletions pharus/component_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
name,
component_config,
static_config,
connect_creds: dict,
connection: dj.Connection,
payload=None,
):
self.name = name
Expand All @@ -66,12 +66,7 @@ def __init__(
self.width = component_config["width"]
if static_config:
self.static_variables = types.MappingProxyType(static_config)
self.connection = dj.conn(
host=connect_creds["databaseAddress"],
user=connect_creds["username"],
password=connect_creds["password"],
reset=True,
)
self.connection = connection
self.payload = payload


Expand Down Expand Up @@ -406,6 +401,7 @@ def dj_query_route(self):


type_map = {
"external": Component,
"basicquery": FetchComponent,
"plot:plotly:stored_json": PlotPlotlyStoredjsonComponent,
"table": TableComponent,
Expand Down
16 changes: 10 additions & 6 deletions pharus/dynamic_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
def populate_api():
header_template = """# Auto-generated rest api
from .server import app, protected_route
from .interface import _DJConnector, dj
from .interface import _DJConnector
from flask import request
import datajoint as dj
from json import loads
from base64 import b64decode
from datetime import datetime
Expand All @@ -28,14 +29,14 @@ def populate_api():
@app.route('{route}', methods=['{rest_verb}'])
@protected_route
def {method_name}(connect_creds: dict) -> dict:
def {method_name}(connection: dj.Connection) -> dict:
if request.method in ['{rest_verb}']:
try:
component_instance = type_map['{component_type}'](name='{component_name}',
component_config={component},
static_config={static_config},
connect_creds=connect_creds,
connection=connection,
{payload})
return component_instance.{method_name_type}()
except Exception as e:
Expand All @@ -46,7 +47,7 @@ def {method_name}(connect_creds: dict) -> dict:
@app.route('{route}', methods=['{rest_verb}'])
def {method_name}() -> dict:
if request.method in ['{rest_verb}']:
connect_creds = dict(
connection = dj.Connection(
databaseAddress=os.environ["PHARUS_HOST"],
username=os.environ["PHARUS_USER"],
password=os.environ["PHARUS_PASSWORD"],
Expand All @@ -55,7 +56,7 @@ def {method_name}() -> dict:
component_instance = type_map['{component_type}'](name='{component_name}',
component_config={component},
static_config={static_config},
connect_creds=connect_creds,
connection=connection,
{payload})
return component_instance.{method_name_type}()
except Exception as e:
Expand Down Expand Up @@ -120,8 +121,11 @@ def {method_name}() -> dict:
else grid["components"]
).items():
if re.match(
r"^(table|metadata|plot|file|slider|dropdown-query|form).*$",
r"""
^(table|metadata|plot|file|slider|
dropdown-query|form|basicquery|external).*$""",
comp["type"],
flags=re.VERBOSE,
):
f.write(
(active_route_template).format(
Expand Down
Loading

0 comments on commit a9b2d8f

Please sign in to comment.