FEAT: Implemented auto-updating thumbnails (includes the addition of Redis and Celery)

This commit is contained in:
2025-01-31 00:12:28 +00:00
parent 21a45e5538
commit 4396d71c2d
8 changed files with 134 additions and 12 deletions

View File

@@ -0,0 +1,29 @@
from celery import Celery, shared_task, Task
from utils.stream_utils import generate_thumbnail, streamer_live_status
from time import sleep
def celery_init_app(app) -> Celery:
class FlaskTask(Task):
def __call__(self, *args: object, **kwargs: object) -> object:
with app.app_context():
return self.run(*args, **kwargs)
celery_app = Celery(app.name, task_cls=FlaskTask)
celery_app.config_from_object(app.config["CELERY"])
celery_app.set_default()
app.extensions["celery"] = celery_app
return celery_app
@shared_task
def update_thumbnail(user_id, sleep_time=10) -> None:
"""
Updates the thumbnail of a stream periodically
"""
ffmpeg_wait_time = 5
# check if user is streaming
while streamer_live_status(user_id)['isLive']:
sleep(ffmpeg_wait_time)
generate_thumbnail(user_id)
sleep(sleep_time - ffmpeg_wait_time)
return

View File

@@ -0,0 +1,4 @@
from blueprints import create_app
flask_app = create_app()
celery_app = flask_app.extensions["celery"]