Skip to content

Commit

Permalink
Merge pull request #8081 from rtibbles/sharing_is_not_caring
Browse files Browse the repository at this point in the history
Revert SharingPool usage
  • Loading branch information
rtibbles authored May 12, 2021
2 parents 8f24d41 + aebe197 commit 9136c7f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
13 changes: 13 additions & 0 deletions kolibri/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
TEMP_KOLIBRI_HOME = "./.pytest_kolibri_home"


@pytest.fixture(scope="session")
def django_db_setup(
request,
django_db_setup,
):
def dispose_sqlalchemy():
from kolibri.core.tasks.main import connection

connection.dispose()

request.addfinalizer(dispose_sqlalchemy)


@pytest.fixture(scope="session", autouse=True)
def global_fixture():
if not os.path.exists(TEMP_KOLIBRI_HOME):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
)
from kolibri.core.content.utils.sqlalchemybridge import get_default_db_string
from kolibri.core.content.utils.sqlalchemybridge import prepare_base
from kolibri.core.content.utils.sqlalchemybridge import SharingPool
from kolibri.core.content.utils.sqlalchemybridge import SQLALCHEMY_CLASSES_PATH_TEMPLATE
from kolibri.core.utils.sqlalchemy_utils import SharingPool

DATA_PATH_TEMPLATE = os.path.join(
os.path.dirname(__file__), "../../fixtures/{name}_content_data.json"
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/content/test/sqlalchemytesting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlalchemy import create_engine

from kolibri.core.content.utils.sqlalchemybridge import get_default_db_string
from kolibri.core.utils.sqlalchemy_utils import SharingPool
from kolibri.core.content.utils.sqlalchemybridge import SharingPool


def django_connection_engine():
Expand Down
40 changes: 40 additions & 0 deletions kolibri/core/content/utils/sqlalchemybridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ class ClassNotFoundError(Exception):
pass


# get_conn and SharingPool code modified from:
# http://nathansnoggin.blogspot.com/2013/11/integrating-sqlalchemy-into-django.html


def get_conn(self):
"""
custom connection factory, so we can share with django
"""
from django.db import connections

conn = connections["default"]
return conn.connection


class SharingPool(NullPool):
"""
custom connection pool that doesn't close connections, and uses our
custom connection factory
"""

def __init__(self, get_connection, **kwargs):
kwargs["reset_on_return"] = False
super(SharingPool, self).__init__(get_conn, **kwargs)

def status(self):
return "Sharing Pool"

def _do_return_conn(self, conn):
pass

def _do_get(self):
return self._create_connection()

def _close_connection(self, connection):
pass

def dispose(self):
pass


def sqlite_connection_string(db_path):
# Call normpath to ensure that Windows paths are properly formatted
return "sqlite:///{db_path}".format(db_path=os.path.normpath(db_path))
Expand Down
7 changes: 0 additions & 7 deletions kolibri/core/tasks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from kolibri.core.tasks.queue import Queue
from kolibri.core.tasks.scheduler import Scheduler
from kolibri.core.tasks.worker import Worker
from kolibri.core.utils.sqlalchemy_utils import SharingPool
from kolibri.utils import conf


Expand All @@ -28,8 +27,6 @@ def __create_engine():
path=os.path.join(conf.KOLIBRI_HOME, "job_storage.sqlite3")
),
connect_args={"check_same_thread": False},
# Use NullPool for SQLite as we use a completely separate database
# file, so no need to share anything with Django.
poolclass=NullPool,
)

Expand All @@ -49,10 +46,6 @@ def __create_engine():
),
pool_pre_ping=True,
client_encoding="utf8",
# Use our SharingPool for Postgres, so as to ensure that
# we share underlying database connections with Django
# this results in cleaner shutdown and clean up of connections
poolclass=SharingPool,
)


Expand Down
8 changes: 8 additions & 0 deletions kolibri/core/tasks/test/test_no_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest


@pytest.mark.django_db
def test_importing_queue_no_open_connection():
from kolibri.core.tasks.main import queue

queue.empty()
39 changes: 0 additions & 39 deletions kolibri/core/utils/sqlalchemy_utils.py

This file was deleted.

0 comments on commit 9136c7f

Please sign in to comment.