|
9 | 9 | from django.utils.decorators import method_decorator |
10 | 10 | from django.http import JsonResponse |
11 | 11 |
|
| 12 | +from django_email_learning.models import JobName |
| 13 | +from django_email_learning.services.metrics_service import MetricsService |
| 14 | + |
| 15 | + |
| 16 | +metric_service = MetricsService() |
| 17 | + |
12 | 18 |
|
13 | 19 | @method_decorator(check_api_key(), name="get") |
14 | 20 | class DeliverContentsJobView(View): |
15 | 21 | def get(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def] |
16 | | - job = DeliverContentsJob() |
17 | | - job.run() |
18 | | - return JsonResponse({"status": "DeliverContentsJob triggered"}, status=202) |
| 22 | + try: |
| 23 | + job = DeliverContentsJob() |
| 24 | + job.run() |
| 25 | + return JsonResponse({"status": "DeliverContentsJob triggered"}, status=202) |
| 26 | + except Exception as e: |
| 27 | + metric_service.job_execution_failed(job_name=JobName.DELIVER_CONTENTS.value) |
| 28 | + return JsonResponse( |
| 29 | + {"status": "DeliverContentsJob failed", "error": str(e)}, status=500 |
| 30 | + ) |
19 | 31 |
|
20 | 32 |
|
21 | 33 | @method_decorator(check_api_key(), name="get") |
22 | 34 | class CheckIMAPJobView(View): |
23 | 35 | def get(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def] |
24 | | - job = CheckIMAPJob() |
25 | | - job.run() |
26 | | - return JsonResponse({"status": "CheckIMAPJob triggered"}, status=202) |
| 36 | + try: |
| 37 | + job = CheckIMAPJob() |
| 38 | + job.run() |
| 39 | + return JsonResponse({"status": "CheckIMAPJob triggered"}, status=202) |
| 40 | + except Exception as e: |
| 41 | + metric_service.job_execution_failed(job_name=JobName.CHECK_IMAP.value) |
| 42 | + return JsonResponse( |
| 43 | + {"status": "CheckIMAPJob failed", "error": str(e)}, status=500 |
| 44 | + ) |
27 | 45 |
|
28 | 46 |
|
29 | 47 | @method_decorator(check_api_key(), name="get") |
30 | 48 | class SendQuizRemindersJobView(View): |
31 | 49 | def get(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def] |
32 | | - job = SendRemindersJob() |
33 | | - job.run() |
34 | | - return JsonResponse({"status": "SendRemidersJob triggered"}, status=202) |
| 50 | + try: |
| 51 | + job = SendRemindersJob() |
| 52 | + job.run() |
| 53 | + return JsonResponse({"status": "SendRemidersJob triggered"}, status=202) |
| 54 | + except Exception as e: |
| 55 | + metric_service.job_execution_failed(job_name=JobName.SEND_REMINDERS.value) |
| 56 | + return JsonResponse( |
| 57 | + {"status": "SendRemidersJob failed", "error": str(e)}, status=500 |
| 58 | + ) |
35 | 59 |
|
36 | 60 |
|
37 | 61 | @method_decorator(check_api_key(), name="get") |
38 | 62 | class DeactivateInactiveEnrollmentsJobView(View): |
39 | 63 | def get(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def] |
40 | | - job = DeactivateInactiveEnrollmentsJob() |
41 | | - job.run() |
42 | | - return JsonResponse( |
43 | | - {"status": "DeactivateInactiveEnrollmentsJob triggered"}, status=202 |
44 | | - ) |
| 64 | + try: |
| 65 | + job = DeactivateInactiveEnrollmentsJob() |
| 66 | + job.run() |
| 67 | + return JsonResponse( |
| 68 | + {"status": "DeactivateInactiveEnrollmentsJob triggered"}, status=202 |
| 69 | + ) |
| 70 | + except Exception as e: |
| 71 | + metric_service.job_execution_failed( |
| 72 | + job_name=JobName.DEACTIVATE_ENROLLMENTS.value |
| 73 | + ) |
| 74 | + return JsonResponse( |
| 75 | + { |
| 76 | + "status": "DeactivateInactiveEnrollmentsJob failed", |
| 77 | + "error": str(e), |
| 78 | + }, |
| 79 | + status=500, |
| 80 | + ) |
0 commit comments