Skip to content

Commit

Permalink
scheduler: move into its own app
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan-Kowal committed Feb 23, 2024
1 parent c24496a commit 3ddb263
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 59 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [v2.0.1] - 2024-02-23

- Updated `coverage` config
- Moved `scheduler` into its own app, rather than being part of the `core` app
- Fixed deprecated `ping` route import in `urls.py`

## [v2.0.0] - 2024-02-18

- **Deploy:**
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ with Django+DRF and React, and comes with many features included.
**A fully working a customizable Django/DRF:**

- Views:
- Basic Django views for good measures: `ping`, `robots.txt`
- Basic Django views for good measures: `robots.txt`
- A default/catch-all route that serves the React frontend (`index` view)
- Authentication API (`login`, `logout`, `check`)
- Current user API (`get`, `update`, `update_password`)
- Health check API (`api`, `db`, `scheduler`)
- Others (`config`)
- A swagger API documentation
- Database:
- Flexible database management (SQLite or PostgreSQL)
- Flexible database management (SQLite or PostgreSQL with postgis)
- A custom user model following best practices
- QA:
- Configuration files for `isort`, `flake8`, `black`, and `mypy`
Expand Down
12 changes: 8 additions & 4 deletions backend/.coveragerc
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
[run]
omit =
# Django
# [Django] Server
manage.py
django_react_starter/*
django_react_starter/settings/development.py
django_react_starter/settings/production.py
django_react_starter/asgi.py
django_react_starter/wsgi.py
# [Django] Scheduler
scheduler/management/**
# [Django] Common files
*/__init__.py
*/migrations/*
*/tests/*
*/admin.py
# Scheduler
*scheduler.py
# Others
wait_for_db.py

Expand Down
2 changes: 1 addition & 1 deletion backend/.isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ skip_glob = src/*

# Structure
known_django = django,django_apscheduler,django_filters,drf_spectacular,rest_framework,dj_database_url,django_prometheus
known_application = django_react_starter,core,user,health
known_application = django_react_starter,core,user,health,scheduler
default_section = THIRDPARTY

# Sections
Expand Down
33 changes: 0 additions & 33 deletions backend/core/tests/factories.py

This file was deleted.

1 change: 1 addition & 0 deletions backend/django_react_starter/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# Custom
"core",
"health",
"scheduler",
"user",
# Other third party
"django_cleanup.apps.CleanupConfig",
Expand Down
3 changes: 1 addition & 2 deletions backend/django_react_starter/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework import routers

# Application
from core.views import AppViewSet, index, ping, robots_txt
from core.views import AppViewSet, index, robots_txt
from health.views import HealthViewSet
from user.views import AuthViewSet, CurrentUserViewSet

Expand All @@ -25,7 +25,6 @@
# URLs
urlpatterns = [
# Django
path("ping/", ping),
path("robots.txt/", robots_txt),
path("admin/", admin.site.urls),
*static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Expand Down
2 changes: 1 addition & 1 deletion backend/health/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from rest_framework.reverse import reverse

# Application
from core import scheduler
from core.tests import BaseActionTestCase
from scheduler import scheduler


class HealthViewSetTestCase(BaseActionTestCase):
Expand Down
7 changes: 2 additions & 5 deletions backend/health/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.status import HTTP_200_OK, HTTP_500_INTERNAL_SERVER_ERROR

# Application
from core import scheduler
from scheduler import scheduler

User = get_user_model()

Expand All @@ -22,10 +22,7 @@ class HealthViewSet(ImprovedViewSet):

@action(detail=False, methods=["get"])
def api(self, _request: Request) -> Response:
try:
return Response(status=HTTP_200_OK, data="API up")
except Exception:
return Response(status=HTTP_500_INTERNAL_SERVER_ERROR, data="API down")
return Response(status=HTTP_200_OK, data="API up")

@action(detail=False, methods=["get"])
def database(self, _request: Request) -> Response:
Expand Down
1 change: 1 addition & 0 deletions backend/scheduler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = "scheduler.apps.SchedulerConfig"
6 changes: 6 additions & 0 deletions backend/scheduler/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Django
from django.apps import AppConfig


class SchedulerConfig(AppConfig):
name = "scheduler"
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class Command(BaseCommand):

def handle(self, *args: Any, **options: Any) -> None:
# Application
from core.scheduler import IS_RUNNING_CACHE_KEY, scheduler
from scheduler.scheduler import IS_RUNNING_CACHE_KEY, blocking_scheduler

try:
cache.set(IS_RUNNING_CACHE_KEY, False)
LOGGER.info("[core] Starting scheduler...")
cache.set(IS_RUNNING_CACHE_KEY, True)
scheduler.start()
blocking_scheduler.start()
except KeyboardInterrupt:
LOGGER.info("[core] Stopping scheduler...")
cache.set(IS_RUNNING_CACHE_KEY, False)
scheduler.shutdown()
blocking_scheduler.shutdown()
LOGGER.info("[core] Scheduler shut down successfully!")
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from apscheduler.schedulers import SchedulerNotRunningError

# Django
from django.core.cache import cache
from django.core.management import BaseCommand

LOGGER = logging.getLogger("default")
Expand All @@ -16,11 +17,12 @@ class Command(BaseCommand):

def handle(self, *args: Any, **options: Any) -> None:
# Application
from core.scheduler import scheduler
from scheduler.scheduler import IS_RUNNING_CACHE_KEY, blocking_scheduler

try:
cache.set(IS_RUNNING_CACHE_KEY, False)
LOGGER.info("[core] Stopping scheduler...")
scheduler.shutdown()
blocking_scheduler.shutdown()
LOGGER.info("[core] Scheduler shut down successfully!")
except SchedulerNotRunningError:
LOGGER.error("[core] No scheduler currently running...")
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
IS_RUNNING_CACHE_KEY = "scheduler_is_running"

# Create scheduler and register jobs
scheduler = BlockingScheduler(
blocking_scheduler = BlockingScheduler(
jobstores={"default": DjangoJobStore()},
executors={"default": ThreadPoolExecutor(10)},
timezone=timezone.utc,
Expand Down
4 changes: 0 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ services:
image: postgis/postgis:16-3.4-alpine
env_file:
- ./backend/.env
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
networks:
- django_react_starter_network
ports:
Expand Down

0 comments on commit 3ddb263

Please sign in to comment.