app.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. import signal
  3. import sys
  4. import threading
  5. import django
  6. import uvicorn
  7. from django.core.management import call_command
  8. from glitchtip.celery import app
  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "glitchtip.settings")
  10. django.setup()
  11. def run_celery_worker():
  12. app.worker_main(argv=["worker", "--loglevel=info", "--pool=threads"])
  13. def run_celery_beat():
  14. app.Beat().run()
  15. def run_django_server():
  16. uvicorn.run(
  17. "glitchtip.asgi:application",
  18. workers=1,
  19. host="0.0.0.0",
  20. port=8000,
  21. log_level="info",
  22. )
  23. def signal_handler(sig, frame):
  24. print("SIGTERM received, shutting down gracefully...")
  25. # Perform cleanup here
  26. sys.exit(0)
  27. def main():
  28. call_command("migrate", no_input=True)
  29. signal.signal(signal.SIGTERM, signal_handler)
  30. signal.signal(signal.SIGINT, signal_handler)
  31. worker_thread = threading.Thread(target=run_celery_worker)
  32. worker_thread.start()
  33. beat_thread = threading.Thread(target=run_celery_beat)
  34. beat_thread.start()
  35. django_thread = threading.Thread(target=run_django_server)
  36. django_thread.start()
  37. # run_django_server()
  38. django_thread.join()
  39. # worker_thread.join()
  40. # beat_thread.join()
  41. main()