-
Notifications
You must be signed in to change notification settings - Fork 14
207255_Celery_tasks_refactor #5815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 95 commits
Commits
Show all changes
97 commits
Select commit
Hold shift + click to select a range
900aeb9
account, accountability, core
56c9a05
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 11e8c78
generic import, cov
c44fad9
Merge branch '207255_Celery_tasks_refactor' of github.com:unicef/hope…
451ce51
geo
7a1d54d
grievance
a9bece9
cov
5671f78
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 223d4fe
household
b45077e
fix group_key
c27c0e6
Merge branch '207255_Celery_tasks_refactor' of github.com:unicef/hope…
6944208
ut
1e58257
ut
9309cf9
ut
4e79125
cov
5f4cfaa
payment app, generic task jobs refactor
2474787
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 14d575f
ut
7c72cd9
cov
aacf455
refactor
454e10a
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
47d1b19
conflicts
4ae7c01
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 42f6f09
207255_Celery_tasks_refactor_2
c8a6b6c
Merge branch '207255_Celery_tasks_refactor' into 207255_Celery_tasks_…
MarekBiczysko 0387204
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko df14bbb
ut
c6e0e47
Merge branch '207255_Celery_tasks_refactor' of github.com:unicef/hope…
8f3e8af
ut
21941b6
recover_missing_async_jobs_task
841e69c
DEFAULT_RECOVER_MISSING_ASYNC_JOBS_LIMIT
9846f2b
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
7961c78
ut
a8aaa60
cov
a1fff80
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
74f3d27
lint
d0b438d
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
46194c9
remove registration_data revoke tasks, remove univeral update celey m…
b279496
fix universal update admin
a680852
fill async job program
b22044d
refactor pdu app
dd47c61
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko f363e1f
remove pragma
baf0e22
Merge branch '207255_Celery_tasks_refactor' of github.com:unicef/hope…
9c4d77e
remove pragma
d688683
migration
f5a60ba
pdu fixes
182a29c
ut
0a210e8
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 4a77d7f
fix timeout envs
5b6277a
remove outer layer celery tasks
2451f0b
ut fix
190b7fd
ut fix
7e1c9d5
review fixes
e1c2f2d
Merge branch '207255_Celery_tasks_refactor' of github.com:unicef/hope…
765b466
review fixes
3cdf63f
review fixes
5e3f9d0
review fixes
4ca8d62
review fixes
1ea4ebc
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
ce9f054
ut
96a0cbc
ut
50e6cbb
ut
81843eb
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 25457a0
ut
63e4965
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
f01f4c7
ut
ac7816c
ut
e58004d
ut
9a77f46
ut
d3003a1
ut
22fb74a
ut
56af457
ut
c9e8fba
cov, e2e
da9f028
cov
cdc082d
cleanup
9f3cddd
cleanup
5678c3d
cleanup
8a0cffd
cleanup
5cc90ed
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko e46a236
ut
67fe048
cov
c6f760d
cov
cb883f2
e2e
5b3877f
cov
1a1f2ba
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko 406edd3
ut, admin
8f59242
ut
baa51b5
cov
f188109
e2e
0e40658
Merge remote-tracking branch 'origin/develop' into 207255_Celery_task…
b0ebaad
merge fix
c6e03db
merge fix
c2b1453
mypy
22c3382
ut
ba2e28c
fix aurora schedule task names
57072e9
Merge branch 'develop' into 207255_Celery_tasks_refactor
MarekBiczysko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| from typing import Any, cast | ||
|
|
||
| from adminfilters.autocomplete import AutoCompleteFilter | ||
| from django.contrib import admin | ||
| from django.db.models import QuerySet | ||
| from django.http import HttpRequest | ||
|
|
||
| from hope.admin.utils import HOPEModelAdminBase | ||
| from hope.models import AsyncJob | ||
|
|
||
|
|
||
| class HasErrorsListFilter(admin.SimpleListFilter): | ||
| title = "has errors" | ||
| parameter_name = "has_errors" | ||
|
|
||
| def lookups( | ||
| self, request: HttpRequest, model_admin: admin.ModelAdmin[Any] | ||
| ) -> tuple[tuple[str, str], tuple[str, str]]: | ||
| return ( | ||
| ("yes", "Yes"), | ||
| ("no", "No"), | ||
| ) | ||
|
|
||
| def queryset(self, request: HttpRequest, queryset: QuerySet[AsyncJob]) -> QuerySet[AsyncJob]: | ||
| if self.value() == "yes": | ||
| return queryset.exclude(errors={}) | ||
| if self.value() == "no": | ||
| return queryset.filter(errors={}) | ||
| return queryset | ||
|
|
||
|
|
||
| @admin.register(AsyncJob) | ||
| class AsyncJobAdmin(HOPEModelAdminBase): | ||
| list_display = ( | ||
| "id", | ||
| "job_name", | ||
| "type", | ||
| "task_status_display", | ||
| "local_status", | ||
| "program", | ||
| "gfk_display", | ||
| "curr_async_result_id", | ||
| "repeatable", | ||
| "datetime_created", | ||
| "datetime_queued", | ||
| ) | ||
| list_filter = ( | ||
| ("program", AutoCompleteFilter), | ||
| ("content_type", AutoCompleteFilter), | ||
| ("job_name", AutoCompleteFilter), | ||
| HasErrorsListFilter, | ||
| "type", | ||
| "repeatable", | ||
| "local_status", | ||
| ) | ||
| search_fields = ( | ||
| "job_name", | ||
| "action", | ||
| "program__id", | ||
| "curr_async_result_id", | ||
| "last_async_result_id", | ||
| "object_id", | ||
| ) | ||
| readonly_fields = ( | ||
| "task_status_display", | ||
| "content_object_display", | ||
| "id", | ||
| "version", | ||
| "description", | ||
| "curr_async_result_id", | ||
| "last_async_result_id", | ||
| "datetime_created", | ||
| "datetime_queued", | ||
| "repeatable", | ||
| "celery_history", | ||
| "local_status", | ||
| "owner", | ||
| "group_key", | ||
| "type", | ||
| "config", | ||
| "action", | ||
| "sentry_id", | ||
| "errors", | ||
| "program", | ||
| "content_type", | ||
| "object_id", | ||
| "job_name", | ||
| ) | ||
| ordering = ("-datetime_created",) | ||
|
|
||
| fieldsets = ( | ||
| ( | ||
| None, | ||
| { | ||
| "fields": ( | ||
| "id", | ||
| "job_name", | ||
| "type", | ||
| "task_status_display", | ||
| "local_status", | ||
| "description", | ||
| "action", | ||
| "config", | ||
| "errors", | ||
| ) | ||
| }, | ||
| ), | ||
| ( | ||
| "Relations", | ||
| { | ||
| "fields": ( | ||
| "program", | ||
| "owner", | ||
| "content_type", | ||
| "object_id", | ||
| "content_object_display", | ||
| ) | ||
| }, | ||
| ), | ||
| ( | ||
| "Execution", | ||
| { | ||
| "fields": ( | ||
| "repeatable", | ||
| "group_key", | ||
| "curr_async_result_id", | ||
| "last_async_result_id", | ||
| "datetime_created", | ||
| "datetime_queued", | ||
| "sentry_id", | ||
| "celery_history", | ||
| "version", | ||
| ) | ||
| }, | ||
| ), | ||
| ) | ||
|
|
||
| @admin.display(description="Task status") | ||
| def task_status_display(self, obj: AsyncJob) -> str: | ||
| return obj.task_status | ||
|
|
||
| @admin.display(description="GFK") | ||
| def gfk_display(self, obj: AsyncJob) -> str: | ||
| if not obj.content_type_id or not obj.object_id: | ||
| return "-" | ||
| return f"{obj.content_type}:{obj.object_id}" | ||
|
|
||
| @admin.display(description="Content object") | ||
| def content_object_display(self, obj: AsyncJob) -> str: | ||
| return str(obj.content_object) if obj.content_object else "-" | ||
|
|
||
| def get_queryset(self, request: HttpRequest) -> QuerySet[AsyncJob]: | ||
| return cast( | ||
| "QuerySet[AsyncJob]", | ||
| super() | ||
| .get_queryset(request) | ||
| .select_related("program", "owner", "content_type") | ||
| .only( | ||
| "id", | ||
| "job_name", | ||
| "type", | ||
| "local_status", | ||
| "program", | ||
| "program__id", | ||
| "program__name", | ||
| "content_type", | ||
| "content_type__app_label", | ||
| "content_type__model", | ||
| "object_id", | ||
| "curr_async_result_id", | ||
| "repeatable", | ||
| "datetime_created", | ||
| "datetime_queued", | ||
| "description", | ||
| "action", | ||
| "config", | ||
| "errors", | ||
| "version", | ||
| "last_async_result_id", | ||
| "celery_history", | ||
| "owner", | ||
| "owner__id", | ||
| "owner__username", | ||
| "group_key", | ||
| "sentry_id", | ||
| ), | ||
| ) | ||
|
|
||
| def has_add_permission(self, request: HttpRequest) -> bool: | ||
| return False | ||
|
|
||
| def has_delete_permission(self, request: HttpRequest, obj: Any | None = None) -> bool: | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| from django.contrib.messages import DEFAULT_TAGS | ||
| from django.core.exceptions import ObjectDoesNotExist | ||
| from django.db import transaction | ||
| from django.db.models import F, Q, QuerySet, Value | ||
| from django.db.models import Case, F, Q, QuerySet, Value, When | ||
| from django.db.transaction import atomic | ||
| from django.forms import Form | ||
| from django.http import HttpRequest, HttpResponse, HttpResponseBase, HttpResponseRedirect | ||
|
|
@@ -37,8 +37,8 @@ | |
| from hope.apps.grievance.signals import increment_grievance_ticket_version_cache_for_ticket_ids | ||
| from hope.apps.household.api.caches import invalidate_household_and_individual_list_cache | ||
| from hope.apps.household.celery_tasks import ( | ||
| enroll_households_to_program_task, | ||
| mass_withdraw_households_from_list_task, | ||
| enroll_households_to_program_async_task, | ||
| mass_withdraw_households_from_list_async_task, | ||
| ) | ||
| from hope.apps.household.forms import ( | ||
| MassEnrollForm, | ||
|
|
@@ -244,31 +244,35 @@ def has_add_permission(self, request: HttpRequest, obj: Household | None = None) | |
|
|
||
| class HouseholdWithdrawFromListMixin: | ||
| @staticmethod | ||
| def get_household_queryset_from_list(household_id_list: list, program: Program) -> QuerySet: | ||
| def get_household_queryset_from_list(household_id_list: list[str], program: Program) -> QuerySet: | ||
| return Household.objects.filter( | ||
| unicef_id__in=household_id_list, | ||
| withdrawn=False, | ||
| program=program, | ||
| ) | ||
|
|
||
| @transaction.atomic | ||
| def mass_withdraw_households_from_list_bulk(self, household_id_list: list, tag: str, program: Program) -> None: | ||
| def mass_withdraw_households_from_list_bulk(self, household_id_list: list[str], tag: str, program: Program) -> None: | ||
| households = self.get_household_queryset_from_list(household_id_list, program) | ||
| individuals = Individual.objects.filter(household__in=households, withdrawn=False, duplicate=False) | ||
|
|
||
| tickets = GrievanceTicket.objects.belong_households_individuals(households, individuals) | ||
| ticket_ids = [str(t.ticket.id) for t in tickets] | ||
| for status, _ in GrievanceTicket.STATUS_CHOICES: | ||
| if status == GrievanceTicket.STATUS_CLOSED: | ||
| continue | ||
| GrievanceTicket.objects.filter(id__in=ticket_ids, status=status).update( | ||
| extras=JSONBSet( | ||
| F("extras"), | ||
| Value("{status_before_withdrawn}"), | ||
| Value(f'"{status}"'), | ||
| ), | ||
| status=GrievanceTicket.STATUS_CLOSED, | ||
| ) | ||
| ticket_ids = list({t.ticket_id for t in tickets}) | ||
| previous_status = Case( | ||
| *[ | ||
| When(status=status, then=Value(f'"{status}"')) | ||
| for status, _ in GrievanceTicket.STATUS_CHOICES | ||
| if status != GrievanceTicket.STATUS_CLOSED | ||
| ] | ||
| ) | ||
| GrievanceTicket.objects.filter(id__in=ticket_ids).exclude(status=GrievanceTicket.STATUS_CLOSED).update( | ||
| extras=JSONBSet( | ||
| F("extras"), | ||
| Value("{status_before_withdrawn}"), | ||
| previous_status, | ||
| ), | ||
| status=GrievanceTicket.STATUS_CLOSED, | ||
| ) | ||
| increment_grievance_ticket_version_cache_for_ticket_ids(program.business_area.slug, ticket_ids) | ||
|
|
||
| Document.objects.filter(individual__in=individuals).update(status=Document.STATUS_INVALID) | ||
|
|
@@ -350,7 +354,7 @@ def withdraw_households_from_list(self, request: HttpRequest) -> HttpResponse | | |
| ) | ||
|
|
||
| if step == "3": | ||
| mass_withdraw_households_from_list_task.delay(household_id_list, tag, str(program.id)) | ||
| mass_withdraw_households_from_list_async_task(household_id_list, tag, program) | ||
| self.message_user(request, f"{len(household_id_list)} Households are being withdrawn.") | ||
| return HttpResponseRedirect(reverse("admin:household_household_changelist")) | ||
| return TemplateResponse( | ||
|
|
@@ -639,9 +643,9 @@ def mass_enroll_to_another_program(self, request: HttpRequest, qs: QuerySet) -> | |
| if form.is_valid(): | ||
| program_for_enroll = form.cleaned_data["program_for_enroll"] | ||
| households_ids = list(qs.distinct("unicef_id").values_list("id", flat=True)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why we do no pass the |
||
| enroll_households_to_program_task.delay( | ||
| enroll_households_to_program_async_task( | ||
| households_ids=households_ids, | ||
| program_for_enroll_id=str(program_for_enroll.id), | ||
| program_for_enroll_id=program_for_enroll, | ||
| user_id=str(request.user.id), | ||
| ) | ||
| self.message_user( | ||
|
|
@@ -663,7 +667,7 @@ def mass_enroll_to_another_program(self, request: HttpRequest, qs: QuerySet) -> | |
| context["action"] = "mass_enroll_to_another_program" | ||
| return TemplateResponse( | ||
| request, | ||
| "admin/household/household/enroll_households_to_program.html", | ||
| "admin/household/household/enroll_households_to_program_async_task.html", | ||
| context, | ||
| ) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.