Skip to content

Commit

Permalink
Test fix DB timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizcko committed Jul 29, 2024
1 parent 4dabc63 commit 27eae66
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 89 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
gammu-smsdrc-win
mysql.sql
syslog
Pipfile
Pipfile
__pycache__
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ services:
image: mariadb:11.4-ubi
container_name: sms-gateway_db
restart: always
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
command: --wait_timeout=28800 --interactive_timeout=28800
volumes:
- /opt/mariadb:/var/lib/mysql
environment:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
image: mariadb:11.4-ubi
container_name: sms-gateway_db
restart: always
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
command: --wait_timeout=28800 --interactive_timeout=28800
volumes:
- /opt/mariadb:/var/lib/mysql
environment:
Expand Down
15 changes: 14 additions & 1 deletion src/database/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@
engine = create_engine(mysql_uri, pool_recycle=3600, pool_pre_ping=True)
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))

from contextlib import contextmanager

@contextmanager
def get_session():
session = db_session()
try:
yield session
session.commit()
except Exception as e:
session.rollback()
raise
finally:
session.close()

Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
import database.models
Expand Down
59 changes: 31 additions & 28 deletions src/resources/v1/inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restx import Resource, reqparse
from flask_jwt_extended import ( jwt_required )
from database.models import inbox
from database.instance import db_session
from database.instance import get_session
from server.instance import server
from math import ceil
from models.inbox import inbox_item, inbox_items
Expand Down Expand Up @@ -36,26 +36,27 @@ def get(self):
per_page = int(request.args.get('per_page')) if request.args.get('per_page') else 50
page = int(request.args.get('page')) if request.args.get('page') else 1

query = db_session.query(inbox)
query = query.filter()
with get_session() as session:
query = session.query(inbox)
query = query.filter()

if(before):
query = query.filter(inbox.ReceivingDateTime < before)
if(after):
query = query.filter(inbox.ReceivingDateTime > after)
if(sender):
query = query.filter(inbox.SenderNumber == sender)
total_records = query.count()
total_pages = ceil(total_records/per_page)
if(per_page):
query = query.limit(per_page)
if(page):
query = query.offset((page-1)*per_page)
if(before):
query = query.filter(inbox.ReceivingDateTime < before)
if(after):
query = query.filter(inbox.ReceivingDateTime > after)
if(sender):
query = query.filter(inbox.SenderNumber == sender)
total_records = query.count()
total_pages = ceil(total_records/per_page)
if(per_page):
query = query.limit(per_page)
if(page):
query = query.offset((page-1)*per_page)

records = query.all()
results = []
for record in records:
results.append(record.as_json())
records = query.all()
results = []
for record in records:
results.append(record.as_json())

return {
'page': page,
Expand All @@ -73,11 +74,12 @@ class InboxID(Resource):
@api.response(200, 'Success', inbox_item)
def get(self, id: int):
''' Get a SMS located in the inbox by his ID'''
sms = inbox.query.filter_by(ID=id).first()
if(sms):
return sms.as_json(), 200
else:
return {'message': 'SMS not found'}, 404
with get_session() as session:
sms = session.get(inbox, {"ID": id})
if(sms):
return sms.as_json(), 200
else:
return {'message': 'SMS not found'}, 404

@ns.doc(params={'id': 'ID of a SMS to delete'})
@ns.doc(description='Delete a SMS located in the inbox')
Expand All @@ -86,8 +88,9 @@ def get(self, id: int):
@required_basicAuth(environment_config["require_basic"])
def delete(self, id: int):
''' Delete a SMS located in the inbox'''
sms = inbox.query.filter_by(ID=id).first()
if(sms):
db_session.delete(sms)
db_session.commit()
with get_session() as session:
sms = session.get(inbox, {"ID": id})
if(sms):
session.delete(sms)

return {'results': 'ok'}, 204
61 changes: 32 additions & 29 deletions src/resources/v1/outbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restx import Resource, reqparse
from flask_jwt_extended import ( jwt_required )
from database.models import outbox
from database.instance import db_session
from database.instance import get_session
from server.instance import server
from math import ceil
from models.outbox import outbox_item, outbox_items
Expand Down Expand Up @@ -36,26 +36,27 @@ def get(self):
per_page = int(request.args.get('per_page')) if request.args.get('per_page') else 50
page = int(request.args.get('page')) if request.args.get('page') else 1

query = db_session.query(outbox)
query = query.filter()
with get_session() as session:
query = session.query(outbox)
query = query.filter()

if(destination):
query = query.filter(outbox.SendingDateTime < before)
if(destination):
query = query.filter(outbox.SendingDateTime > after)
if(destination):
query = query.filter(outbox.DestinationNumber == destination)
total_records = query.count()
total_pages = ceil(total_records/per_page)
if(per_page):
query = query.limit(per_page)
if(page):
query = query.offset((page-1)*per_page)

records = query.all()
results = []
for record in records:
results.append(record.as_json())
if(destination):
query = query.filter(outbox.SendingDateTime < before)
if(destination):
query = query.filter(outbox.SendingDateTime > after)
if(destination):
query = query.filter(outbox.DestinationNumber == destination)
total_records = query.count()
total_pages = ceil(total_records/per_page)
if(per_page):
query = query.limit(per_page)
if(page):
query = query.offset((page-1)*per_page)
records = query.all()
results = []
for record in records:
results.append(record.as_json())

return {
'page': page,
Expand All @@ -73,11 +74,12 @@ class OutboxID(Resource):
@api.response(200, 'Success', outbox_item)
def get(self, id: int):
''' Get a SMS located in the outbox by his ID'''
sms = outbox.query.filter_by(ID=id).first()
if(sms):
return sms.as_json(), 200
else:
return {'message': 'SMS not found'}, 404
with get_session() as session:
sms = session.get(outbox, {"ID": id})
if(sms):
return sms.as_json(), 200
else:
return {'message': 'SMS not found'}, 404

@ns.doc(params={'id': 'ID of a SMS to delete'})
@ns.doc(description='Delete a SMS located in the outbox')
Expand All @@ -86,8 +88,9 @@ def get(self, id: int):
@required_basicAuth(environment_config["require_basic"])
def delete(self, id: int):
''' Delete a SMS located in the outbox'''
sms = outbox.query.filter_by(ID=id).first()
if(sms):
db_session.delete(sms)
db_session.commit()
with get_session() as session:
sms = session.get(outbox, {"ID": id})
if(sms):
session.delete(sms)

return {'results': 'ok'}, 204
59 changes: 31 additions & 28 deletions src/resources/v1/sentitems.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restx import Resource, reqparse
from flask_jwt_extended import ( jwt_required )
from database.models import sentitems
from database.instance import db_session
from database.instance import get_session
from server.instance import server
from math import ceil
from models.senditems import senditems_item, senditems_items
Expand Down Expand Up @@ -36,26 +36,27 @@ def get(self):
per_page = int(request.args.get('per_page')) if request.args.get('per_page') else 50
page = int(request.args.get('page')) if request.args.get('page') else 1

query = db_session.query(sentitems)
query = query.filter()
with get_session() as session:
query = session.query(sentitems)
query = query.filter()

if(before):
query = query.filter(sentitems.ReceivingDateTime < before)
if(after):
query = query.filter(sentitems.ReceivingDateTime > after)
if(destination):
query = query.filter(sentitems.DestinationNumber == destination)
total_records = query.count()
total_pages = ceil(total_records/per_page)
if(per_page):
query = query.limit(per_page)
if(page):
query = query.offset((page-1)*per_page)
if(before):
query = query.filter(sentitems.ReceivingDateTime < before)
if(after):
query = query.filter(sentitems.ReceivingDateTime > after)
if(destination):
query = query.filter(sentitems.DestinationNumber == destination)
total_records = query.count()
total_pages = ceil(total_records/per_page)
if(per_page):
query = query.limit(per_page)
if(page):
query = query.offset((page-1)*per_page)

records = query.all()
results = []
for record in records:
results.append(record.as_json())
records = query.all()
results = []
for record in records:
results.append(record.as_json())

return {
'page': page,
Expand All @@ -73,11 +74,12 @@ class SendItemsID(Resource):
@api.response(200, 'Success', senditems_item)
def get(self, id: int):
''' Get a SMS located in the send items by his ID'''
sms = sentitems.query.filter_by(ID=id).first()
if(sms):
return sms.as_json(), 200
else:
return {'message': 'SMS not found'}, 404
with get_session() as session:
sms = session.query(sentitems).filter_by(ID=id).first()
if(sms):
return sms.as_json(), 200
else:
return {'message': 'SMS not found'}, 404

@ns.doc(params={'id': 'ID of a SMS to delete'})
@ns.doc(description='Delete a SMS located in the send items')
Expand All @@ -86,8 +88,9 @@ def get(self, id: int):
@required_basicAuth(environment_config["require_basic"])
def delete(self, id: int):
''' Delete a SMS located in the send items'''
sms = sentitems.query.filter_by(ID=id).first()
if(sms):
db_session.delete(sms)
db_session.commit()
with get_session() as session:
sms = session.query(sentitems).filter_by(ID=id).first()
if(sms):
session.delete(sms)

return {'results': 'ok'}, 204

0 comments on commit 27eae66

Please sign in to comment.