Skip to content

Commit ed28a00

Browse files
committed
setup django
1 parent cf248cc commit ed28a00

File tree

2 files changed

+78
-24
lines changed

2 files changed

+78
-24
lines changed

codeforlife/apps/celery.py

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,74 @@ def stop_background_workers(
164164
print(f"Error stopping all Celery workers: {error}")
165165
print(error.stderr)
166166

167-
@classmethod
168-
def handle_startup(cls, name: str):
167+
def start_background_beat(self, log_level: LogLevel = "INFO"):
168+
"""Start Celery beat using the 'celery --app=app beat' command.
169+
170+
Args:
171+
log_level: The log level.
172+
173+
Returns:
174+
The background process running Celery beat.
175+
"""
176+
177+
print("Starting Celery beat.")
178+
179+
try:
180+
process = subprocess.Popen( # pylint: disable=consider-using-with
181+
[
182+
"celery",
183+
f"--app={self.app}",
184+
"beat",
185+
f"--loglevel={log_level}",
186+
],
187+
stdout=subprocess.DEVNULL,
188+
stderr=subprocess.DEVNULL,
189+
)
190+
print("Successfully started Celery beat.")
191+
192+
atexit.register(self.stop_background_beat, process)
193+
194+
return process
195+
196+
except subprocess.CalledProcessError as error:
197+
print(f"Error starting Celery beat: {error}")
198+
199+
return None
200+
201+
def stop_background_beat(self, process: subprocess.Popen):
202+
"""Stop a Celery beat process.
203+
204+
Args:
205+
process: The process to stop.
206+
"""
207+
208+
print("Stopping Celery beat.")
209+
210+
try:
211+
process.terminate()
212+
print("Successfully stopped Celery beat.")
213+
214+
except Exception as ex: # pylint: disable=broad-exception-caught
215+
print(f"Error stopping Celery beat: {ex}")
216+
217+
def handle_startup(self):
169218
"""Handle the startup procedure of a Celery app.
170219
171220
Examples:
172221
```
173-
import os
174-
from codeforlife.apps import CeleryApplication
222+
from codeforlife.apps import CeleryApplication, DjangoApplication
175223
176-
# Make sure to set this before starting the Celery app!
177-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
224+
# Make sure to set up Django before starting!
225+
DjangoApplication.setup()
178226
179-
celery_app = CeleryApplication.handle_startup(__name__)
227+
celery_app = CeleryApplication().handle_startup()
180228
```
181229
182-
Args:
183-
name: The name of the file calling this function.
184-
185230
Returns:
186-
An instance of a Celery app.
231+
The Celery app instance for convenience.
187232
"""
188233

189-
app = cls()
190-
191-
if name == "__main__":
192-
app.start_background_workers()
234+
self.start_background_workers()
235+
self.start_background_beat()
193236

194-
return app
237+
return self

codeforlife/apps/django.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import multiprocessing
77
import os
88

9+
from django import setup
910
from django.core.asgi import get_asgi_application
1011
from django.core.management import call_command
1112
from django.core.wsgi import get_wsgi_application
@@ -55,30 +56,40 @@ def load_config(self):
5556
def load(self):
5657
return self.application
5758

58-
@classmethod
59-
def handle_startup(cls, name: str):
59+
@staticmethod
60+
def setup(settings_module: str = "settings"):
61+
"""Set up the Django app.
62+
63+
Args:
64+
settings_module: The dot-path to the settings module.
65+
"""
66+
67+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
68+
69+
setup()
70+
71+
def handle_startup(self, name: str):
6072
"""Handle the startup procedure of a Django app.
6173
6274
Examples:
6375
```
64-
import os
6576
from codeforlife.apps import DjangoApplication
6677
67-
# Make sure to set this before starting the Django app!
68-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
78+
# Make sure to set up Django before starting!
79+
DjangoApplication.setup()
6980
70-
django_app = DjangoApplication.handle_startup(__name__)
81+
django_app = DjangoApplication().handle_startup(__name__)
7182
```
7283
7384
Args:
7485
name: The name of the file calling this function.
7586
7687
Returns:
77-
An instance of a Django app.
88+
An instance of a WSGI app.
7889
"""
7990

8091
if name == "__main__":
81-
cls().run()
92+
self.run()
8293
else:
8394
return get_wsgi_application()
8495

0 commit comments

Comments
 (0)