Skip to content

Commit

Permalink
clean up old print statements from server/celery and remove signal th…
Browse files Browse the repository at this point in the history
…at was causing problems
  • Loading branch information
dpgraham4401 committed Nov 20, 2023
1 parent be2cccf commit 6218596
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion server/apps/core/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .profile_service import get_or_create_haztrak_profile
from .rcrainfo_service import RcrainfoService, get_rcrainfo_client
from .task_service import TaskService
from .task_service import TaskService, get_task_status, launch_example_task
16 changes: 12 additions & 4 deletions server/apps/core/services/task_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from typing import Optional

from celery.result import AsyncResult
from django.core.cache import CacheKeyWarning, cache
from django_celery_results.models import TaskResult
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -45,6 +46,15 @@ def _parse_status(task_status: dict) -> ReturnDict:
raise ValidationError(task_serializer.errors)


def launch_example_task() -> str | None:
"""Launches an example long-running celery task"""
try:
task: AsyncResult = example_task.delay()
return task.id
except KeyError:
return None


class TaskService:
"""
Service class for interacting with the Task model layer and celery tasks.
Expand Down Expand Up @@ -103,11 +113,9 @@ def _get_cached_status(task_id: str) -> dict | None:

@staticmethod
def launch_example_task() -> str | None:
"""
Launches an example long-running celery task
"""
"""Launches an example long-running celery task"""
try:
task = example_task.delay()
task: AsyncResult = example_task.delay()
return task.id
except KeyError:
return None
Expand Down
2 changes: 0 additions & 2 deletions server/apps/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

@shared_task(bind=True, name="example task")
def example_task(self: Task):
# print(f"task ID: {self.request.id}")
cache.set(
self.request.id,
{"status": "STARTED", "taskName": self.name, "taskId": self.request.id},
)
# print(f"task status: {cache.get(self.request.id)}")
time.sleep(15)
cache.set(
self.request.id,
Expand Down
4 changes: 2 additions & 2 deletions server/apps/core/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_returns_cached_status_if_exists(self, factory, user):
assert response.data["taskId"] == self.mock_task_id
assert response.data["status"] == task_status

def test_returns_404_if_no_cache_or_db_results(self, factory, user, cache_factory):
def test_returns_200_if_no_cache_or_db_results(self, factory, user, cache_factory):
# Arrange
cache_factory("test_returns_404")
cache.set(self.mock_task_id, None)
Expand All @@ -53,7 +53,7 @@ def test_returns_404_if_no_cache_or_db_results(self, factory, user, cache_factor
# Act
response: Response = TaskStatusView.as_view()(request, task_id=self.mock_task_id)
# Assert
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.status_code == status.HTTP_200_OK

def test_returns_db_results(self, factory, user, cache_factory):
# Arrange
Expand Down
8 changes: 6 additions & 2 deletions server/apps/core/views/task_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.core.services.task_service import TaskService, get_task_status # type: ignore
from apps.core.services.task_service import ( # type: ignore
TaskService,
get_task_status,
launch_example_task,
)


class CeleryTaskResultSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -33,7 +37,7 @@ class LaunchExampleTaskView(APIView):

def get(self, request, *args, **kwargs):
try:
task_id = TaskService.launch_example_task()
task_id = launch_example_task()
return Response(data={"taskId": task_id}, status=status.HTTP_200_OK)
except KeyError:
return Response(
Expand Down
1 change: 0 additions & 1 deletion server/apps/sites/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ class HandlerSearchSerializer(serializers.Serializer):
def post(self, request: Request) -> Response:
serializer = self.HandlerSearchSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
print("hello")
sites = RcraSiteService(username=request.user.username)
data = sites.search_rcrainfo_handlers(
epaSiteId=serializer.data["siteId"],
Expand Down
3 changes: 1 addition & 2 deletions server/apps/trak/models/manifest_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Max, Q, QuerySet
from django.db.models import Q, QuerySet
from django.utils.translation import gettext_lazy as _

from apps.sites.models import RcraSiteType, RcraStates, Role
Expand All @@ -22,7 +22,6 @@ def draft_mtn():
official MTN from e-Manifest
"""
mtn_count: int = Manifest.objects.all().count()
print(mtn_count)
return f"{str(mtn_count).zfill(9)}DFT"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def test_serializer_saves_first_wasteline(self, manifest_10003114elc_serializer)

def test_saves_additional_info(self, manifest_10003114elc_serializer):
manifest_10003114elc_serializer.is_valid()
print(manifest_10003114elc_serializer.validated_data["additional_info"])
manifest = manifest_10003114elc_serializer.save()
additional_info = manifest.additional_info
assert isinstance(additional_info, AdditionalInfo)

0 comments on commit 6218596

Please sign in to comment.