Skip to content

Commit

Permalink
Merge pull request #51 from camuschat/db-bugfix
Browse files Browse the repository at this point in the history
Fix database transaction bugs
  • Loading branch information
mrgnr authored May 16, 2021
2 parents 2675c90 + 0f623db commit 98766da
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 0.3.2
-------------

Released 2021-05-16

- Fix bugs around database transactions

Version 0.3.1
-------------

Expand Down
16 changes: 9 additions & 7 deletions camus/message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from camus import db
from camus.models import Client
from camus.util import get_ice_servers
from camus.util import commit_database, get_ice_servers


class MessageHandler:
Expand Down Expand Up @@ -56,7 +56,7 @@ async def _process_inbox(self):
# Update seen & active timestamps for client and room
client = Client.query.filter_by(uuid=client_uuid).first()
client.seen = client.room.active = datetime.datetime.utcnow()
db.session.commit()
commit_database()

# Message intended for the server
if message.receiver == self._address:
Expand All @@ -72,7 +72,7 @@ async def _process_inbox(self):
self.send(message)

except Exception as e:
print('Error processing inbox: {}'.format(e))
logging.exception('Error processing inbox')

def _handle_local_message(self, message):
"""Handle a message according to its type."""
Expand All @@ -97,7 +97,7 @@ def _handle_local_message(self, message):
username = message.data.get('username')
if username:
client.name = username
db.session.commit()
commit_database()

reply.type = 'room-info'
reply.data = self._room_info(client.room)
Expand All @@ -123,17 +123,19 @@ def _handle_local_message(self, message):
elif message.type == 'bye':
logging.info('got bye')

# Delete client object from db
client = Client.query.filter_by(uuid=message.sender).first()
room = client.room

# Delete client object from db
db.session.delete(client)
db.session.commit()
commit_database()

# Remove message queues associated with the client
self.outbox.pop(client.uuid, None)

# Broadcast updated room info to remaining clients
reply.type = 'room-info'
reply.data = self._room_info(client.room)
reply.data = self._room_info(room)
self.broadcast(client.room, reply)
reply = None

Expand Down
7 changes: 4 additions & 3 deletions camus/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from camus import db, message_handler
from camus.forms import CreateRoomForm, JoinRoomForm
from camus.models import Client, Room
from camus.util import commit_database

bp = Blueprint('main', __name__)

Expand All @@ -33,7 +34,7 @@ async def index():
if password:
room.set_password(password)
db.session.add(room)
db.session.commit()
commit_database(reraise=True)

return redirect('/room/{}'.format(room.slug), code=307)
except sqlalchemy.exc.IntegrityError:
Expand Down Expand Up @@ -62,7 +63,7 @@ async def room(room_id):
client = room.authenticate()
if client:
db.session.add(client)
db.session.commit()
commit_database(reraise=True)
session['id'] = client.uuid

return await render_template(
Expand All @@ -77,7 +78,7 @@ async def room(room_id):
client = room.authenticate(password)
if client:
db.session.add(client)
db.session.commit()
commit_database(reraise=True)
session['id'] = client.uuid

return await render_template(
Expand Down
2 changes: 1 addition & 1 deletion camus/static/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion camus/static/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "camus-chat",
"version": "0.3.1",
"version": "0.3.2",
"description": "Peer-to-peer video chat using WebRTC",
"main": "index.js",
"directories": {
Expand Down
26 changes: 23 additions & 3 deletions camus/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from twilio.base.exceptions import TwilioException, TwilioRestException

from quart import current_app
from sqlalchemy.exc import SQLAlchemyError

from camus import db
from camus.models import Client, Room
Expand All @@ -34,13 +35,32 @@ async def _run(self):
try:
await self._callback(**self._kwargs)
except Exception as e:
logging.error(traceback.format_exc())
logging.exception(
"Exception during excecution of LoopTimer callback function"
)

def cancel(self):
"""Cancel the running task."""
self._task.cancel()


def commit_database(log_error=True, rollback=True, reraise=False):
try:
db.session.commit()
return True
except SQLAlchemyError:
if log_error:
logging.exception('Exception during database commit.')

if rollback:
db.session.rollback()

if reraise:
raise

return False


async def ping_clients(message_handler):
"""Send a ping message to all clients which have not been seen recently
enough.
Expand All @@ -65,7 +85,7 @@ async def reap_clients(message_handler):
room = client.room
message_handler.send_bye(client.uuid)
db.session.delete(client)
db.session.commit()
commit_database()
message_handler.broadcast_room_info(room)


Expand All @@ -78,7 +98,7 @@ async def reap_rooms():

for room in rooms:
db.session.delete(room)
db.session.commit()
commit_database()


def get_ice_servers(username):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='camus-chat',
version='0.3.1',
version='0.3.2',
description='Peer-to-peer video chat using WebRTC',
long_description=open('README.rst').read(),
long_description_content_type="text/x-rst; charset=UTF-8",
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: camus
base: core20
version: '0.3.1'
version: '0.3.2'
summary: Peer-to-peer group video chat using WebRTC.
description: |
Camus is a group video chat server that uses WebRTC for direct peer-to-peer
Expand Down

0 comments on commit 98766da

Please sign in to comment.