"""Utility for spawning async tasks with the uWSGI spooler.""" import json from multiprocessing import Process from time import time import uuid import sentry_sdk from collaborator.api import uwsgiRunning def uwsgi_spool_task(func, *args): """Add a background task to the uWSGI spooler queue.""" if uwsgiRunning: import uwsgi # noqa try: uwsgi.spool( { b"task_id": str(uuid.uuid4()).encode("utf-8"), b"start": str(time()).encode("utf-8"), b"module": func.__module__.encode("utf-8"), b"class": func.__self__.__name__.encode("utf-8"), b"func": func.__name__.encode("utf-8"), b"args": json.dumps(args).encode("utf-8"), } ) except Exception as e: sentry_sdk.capture_exception(e) # In dev, just spawn a child process instead else: Process( target=func, args=args, daemon=True, ).start()