Skip to content

Commit

Permalink
Too many connections - Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
bmadriz committed Aug 2, 2021
1 parent 1a000e1 commit 65c8701
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions climmob/config/celery_class.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from celery import Task
from sqlalchemy import create_engine
from climmob.config.celery_app import get_ini_value

from sqlalchemy.pool import NullPool
from celery.contrib.abortable import AbortableTask

class celeryTask(AbortableTask):
def on_success(self, retval, task_id, args, kwargs):
engine = create_engine(get_ini_value("sqlalchemy.url"))
engine.execute(
engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool)
connection = engine.connect()
connection.execute(
"INSERT INTO finishedtasks(taskid,taskerror) VALUES ('"
+ str(task_id)
+ "',0)"
)
connection.invalidate()
engine.dispose()

def on_failure(self, exc, task_id, args, kwargs, einfo):
engine = create_engine(get_ini_value("sqlalchemy.url"))
engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool)
engine.execute(
"INSERT INTO finishedtasks(taskid,taskerror) VALUES ('"
+ str(task_id)
Expand Down
8 changes: 4 additions & 4 deletions climmob/models/repository.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from sqlalchemy import create_engine
from climmob.config.celery_app import get_ini_value

from sqlalchemy.pool import NullPool

def sql_fetch_one(sql):
engine = create_engine(get_ini_value("sqlalchemy.url"))
engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool)
connection = engine.connect()
res = connection.execute(sql).fetchone()
connection.invalidate()
Expand All @@ -12,7 +12,7 @@ def sql_fetch_one(sql):


def sql_fetch_all(sql):
engine = create_engine(get_ini_value("sqlalchemy.url"))
engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool)
connection = engine.connect()
res = connection.execute(sql).fetchall()
connection.invalidate()
Expand All @@ -21,7 +21,7 @@ def sql_fetch_all(sql):


def sql_execute(sql):
engine = create_engine(get_ini_value("sqlalchemy.url"))
engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool)
connection = engine.connect()
res = connection.execute(sql)
connection.invalidate()
Expand Down

0 comments on commit 65c8701

Please sign in to comment.