codescraftman / ethanicbot_updater / management / commands / runserver_with_celery.py
runserver_with_celery.py
Raw
# ethanicbot/management/commands/runserver_with_celery.py

import os
import sys
from django.core.management.commands.runserver import Command as RunserverCommand
from subprocess import Popen

class Command(RunserverCommand):
    def handle(self, *args, **options):
        # Start the Django server
        print("Starting Django development server...")
        runserver_process = Popen([sys.executable, 'manage.py', 'runserver'])

        # Start the Celery worker
        print("Starting Celery worker...")
        celery_process = Popen([sys.executable, 'manage.py', 'celery', 'worker', '--loglevel=info'])

        # Wait for the Django server to terminate (if it does)
        runserver_process.wait()

        # Ensure Celery stops when Django server is stopped
        celery_process.terminate()
        celery_process.wait()