Refactor info update background task

We now time the interval between consecutive tasks. This is more precise
than using the constant interval the task is supposed to run at since
there is some drift on each run (~0.004s).
このコミットが含まれているのは:
n9k 2022-06-15 20:50:43 +00:00
コミット f3e58fd3fa
2個のファイルの変更24行の追加21行の削除

ファイルの表示

@ -47,6 +47,7 @@ def create_app(config_file):
app.stream_title = None app.stream_title = None
app.stream_uptime = None app.stream_uptime = None
app.stream_viewership = None app.stream_viewership = None
app.last_info_task = None
# Background tasks' asyncio.sleep tasks, cancelled on shutdown # Background tasks' asyncio.sleep tasks, cancelled on shutdown
app.background_sleep = set() app.background_sleep = set()

ファイルの表示

@ -130,56 +130,58 @@ async def t_broadcast_users_update(iteration):
broadcast_users_update() broadcast_users_update()
@with_period(CONFIG['TASK_BROADCAST_STREAM_INFO_UPDATE']) @with_period(CONFIG['TASK_BROADCAST_STREAM_INFO_UPDATE'])
async def t_broadcast_stream_info_update(iteration): @with_timestamp(precise=True)
async def t_broadcast_stream_info_update(timestamp, iteration):
if iteration == 0: if iteration == 0:
title = await get_stream_title() title = await get_stream_title()
uptime, viewership = get_stream_uptime_and_viewership() uptime, viewership = get_stream_uptime_and_viewership(rounded=False)
current_app.stream_title = title current_app.stream_title = title
current_app.stream_uptime = uptime current_app.stream_uptime = uptime
current_app.stream_viewership = viewership current_app.stream_viewership = viewership
else: else:
payload = {} info = {}
title = await get_stream_title() title = await get_stream_title()
uptime, viewership = get_stream_uptime_and_viewership() uptime, viewership = get_stream_uptime_and_viewership(rounded=False)
# Check if the stream title has changed # Check if the stream title has changed
if current_app.stream_title != title: if current_app.stream_title != title:
current_app.stream_title = title current_app.stream_title = title
payload['title'] = title info['title'] = title
# Check if the stream uptime has changed unexpectedly # Check if the stream uptime has changed unexpectedly
if current_app.stream_uptime is None: last_uptime, current_app.stream_uptime = (
expected_uptime = None current_app.stream_uptime, uptime
)
if last_uptime is None:
projected_uptime = None
else: else:
expected_uptime = ( last_info_task_ago = timestamp - current_app.last_info_task
current_app.stream_uptime projected_uptime = last_uptime + last_info_task_ago
+ CONFIG['TASK_BROADCAST_STREAM_INFO_UPDATE'] if uptime is None and projected_uptime is None:
)
current_app.stream_uptime = uptime
if uptime is None and expected_uptime is None:
stats_changed = False stats_changed = False
elif uptime is None or expected_uptime is None: elif uptime is None or projected_uptime is None:
stats_changed = True stats_changed = True
else: else:
stats_changed = abs(uptime - expected_uptime) >= 0.5 stats_changed = abs(uptime - projected_uptime) >= 0.5
# Check if viewership has changed # Check if viewership has changed
if current_app.stream_viewership != viewership: if current_app.stream_viewership != viewership:
current_app.stream_viewership = viewership current_app.stream_viewership = viewership
stats_changed = True stats_changed = True
# Broadcast iff anything has changed
if stats_changed: if stats_changed:
if uptime is None: if uptime is None:
payload['stats'] = None info['stats'] = None
else: else:
payload['stats'] = { info['stats'] = {
'uptime': uptime, 'uptime': round(uptime, 2),
'viewership': viewership, 'viewership': viewership,
} }
if info:
broadcast(USERS, payload={'type': 'info', **info})
if payload: current_app.last_info_task = timestamp
broadcast(USERS, payload={'type': 'info', **payload})
current_app.add_background_task(t_delete_eyes) current_app.add_background_task(t_delete_eyes)
current_app.add_background_task(t_sunset_users) current_app.add_background_task(t_sunset_users)