Add some comments and remove debug code.

This commit is contained in:
Patrick Jentsch 2019-09-02 09:17:00 +02:00
parent 7d4a1e6d37
commit 6f4b9efa25

View File

@ -16,9 +16,12 @@ disconnected = []
@socketio.on('connect') @socketio.on('connect')
@login_required @login_required
def connect(): def connect():
# print('{} connected'.format(current_user.username)) '''
# send('You entered the room {}'.format(request.sid), ' The Socket.IO module creates a session id (sid) on each request. The
# room=request.sid) ' initiating client is automatically placed in a room with that sid, which
' will be used for further information exchange generated by a background
' task associated with the sid.
'''
socketio.start_background_task(background_task, socketio.start_background_task(background_task,
current_user.id, current_user.id,
request.sid) request.sid)
@ -27,8 +30,11 @@ def connect():
@socketio.on('disconnect') @socketio.on('disconnect')
@login_required @login_required
def disconnect(): def disconnect():
'''
' On disconnect the session id (sid) of the connection gets placed in the
' disconnected list (see above).
'''
disconnected.append(request.sid) disconnected.append(request.sid)
#print('{} disconnected'.format(current_user.username))
def background_task(user_id, session_id): def background_task(user_id, session_id):
@ -39,13 +45,19 @@ def background_task(user_id, session_id):
' '
' NOTE: The initial values are send as a init-* events. ' NOTE: The initial values are send as a init-* events.
' The JSON patches are send as update-* events. ' The JSON patches are send as update-* events.
'
' > where '*' is either 'corpora' or 'jobs'
'''
'''
' Create an app instance to get access to an app_context with which db
' operations are fulfilled.
''' '''
app = create_app(os.getenv('FLASK_CONFIG') or 'default', main=False) app = create_app(os.getenv('FLASK_CONFIG') or 'default', main=False)
with app.app_context(): with app.app_context():
user = db.session.query(User).filter_by(id=user_id).first() user = db.session.query(User).filter_by(id=user_id).first()
''' Get current values from the database. ''' ''' Get current values from the database. '''
corpora = user.corpora_as_dict() corpora = user.corpora_as_dict()
jobs = user.jobs_as_dict() jobs = user.jobs_as_dict()
''' Send initial values. ''' ''' Send initial values. '''
socketio.emit('init-corpora', socketio.emit('init-corpora',
@ -56,8 +68,6 @@ def background_task(user_id, session_id):
room=session_id) room=session_id)
''' TODO: Implement maximum runtime for this loop. ''' ''' TODO: Implement maximum runtime for this loop. '''
while session_id not in disconnected: while session_id not in disconnected:
# print(session_id + ' running')
# socketio.emit('message', 'heartbeat', room=session_id)
''' Get current values from the database ''' ''' Get current values from the database '''
new_corpora = user.corpora_as_dict() new_corpora = user.corpora_as_dict()
new_jobs = user.jobs_as_dict() new_jobs = user.jobs_as_dict()
@ -73,9 +83,8 @@ def background_task(user_id, session_id):
socketio.emit('update-jobs', socketio.emit('update-jobs',
jobs_patch.to_string(), jobs_patch.to_string(),
room=session_id) room=session_id)
''' Set new values as a reference for the next iteration. ''' ''' Set new values as references for the next iteration. '''
corpora = new_corpora corpora = new_corpora
jobs = new_jobs jobs = new_jobs
socketio.sleep(3) socketio.sleep(3)
disconnected.remove(session_id) disconnected.remove(session_id)
# print(session_id + ' stopped')