This repository was archived by the owner on Mar 28, 2025. It is now read-only.
File tree 3 files changed +53
-3
lines changed
tests/unit/task_processor
3 files changed +53
-3
lines changed Original file line number Diff line number Diff line change
1
+ from django .utils import timezone
2
+
3
+ from task_processor .models import Task
4
+
5
+
6
+ def get_num_waiting_tasks () -> int :
7
+ return Task .objects .filter (
8
+ num_failures__lt = 3 ,
9
+ completed = False ,
10
+ scheduled_for__lt = timezone .now (),
11
+ is_locked = False ,
12
+ ).count ()
Original file line number Diff line number Diff line change 3
3
from rest_framework .permissions import IsAdminUser , IsAuthenticated
4
4
from rest_framework .response import Response
5
5
6
- from task_processor .models import Task
6
+ from task_processor .monitoring import get_num_waiting_tasks
7
7
from task_processor .serializers import MonitoringSerializer
8
8
9
9
10
10
@swagger_auto_schema (method = "GET" , responses = {200 : MonitoringSerializer ()})
11
11
@api_view (http_method_names = ["GET" ])
12
12
@permission_classes ([IsAuthenticated , IsAdminUser ])
13
13
def monitoring (request , ** kwargs ):
14
- waiting_tasks = Task .objects .filter (num_failures__lt = 3 , completed = False ).count ()
15
14
return Response (
16
- data = {"waiting" : waiting_tasks }, headers = {"Content-Type" : "application/json" }
15
+ data = {"waiting" : get_num_waiting_tasks ()},
16
+ headers = {"Content-Type" : "application/json" },
17
17
)
Original file line number Diff line number Diff line change
1
+ from datetime import timedelta
2
+
3
+ from django .utils import timezone
4
+
5
+ from task_processor .models import Task
6
+ from task_processor .monitoring import get_num_waiting_tasks
7
+
8
+
9
+ def test_get_num_waiting_tasks (db : None ) -> None :
10
+ # Given
11
+ now = timezone .now ()
12
+
13
+ # a task that is waiting
14
+ Task .objects .create (task_identifier = "tasks.test_task" )
15
+
16
+ # a task that is scheduled for the future
17
+ Task .objects .create (
18
+ task_identifier = "tasks.test_task" , scheduled_for = now + timedelta (days = 1 )
19
+ )
20
+
21
+ # and a task that has been completed
22
+ Task .objects .create (
23
+ task_identifier = "tasks.test_task" ,
24
+ scheduled_for = now - timedelta (days = 1 ),
25
+ completed = True ,
26
+ )
27
+
28
+ # and a task that has been locked for processing
29
+ Task .objects .create (
30
+ task_identifier = "tasks.test_task" ,
31
+ is_locked = True ,
32
+ )
33
+
34
+ # When
35
+ num_waiting_tasks = get_num_waiting_tasks ()
36
+
37
+ # Then
38
+ assert num_waiting_tasks == 1
You can’t perform that action at this time.
0 commit comments