-
-
Notifications
You must be signed in to change notification settings - Fork 217
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
Trigger Geopoint ES Index on Geospatial Feature Flag Enable #35126
base: master
Are you sure you want to change the base?
Changes from all commits
4b32aec
6280c17
7f52f0e
2184e76
d7abcee
5409b02
177b1b3
9deab96
b647463
196ffd3
6906d82
ca4ad45
128757f
edb3a61
a4c7bb5
707a97d
ded63ee
a161b10
a030555
29ec236
037e5f2
0724d43
d96c1d4
e1d82cc
5137009
dd28ee8
1581ca5
cbe08cf
e1ef27a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
from dimagi.utils.logging import notify_exception | ||
|
||
from corehq.apps.celery import task | ||
from corehq.apps.geospatial.const import INDEX_ES_TASK_HELPER_BASE_KEY | ||
from corehq.apps.geospatial.es import case_query_for_missing_geopoint_val | ||
from corehq.apps.geospatial.utils import ( | ||
CeleryTaskTracker, | ||
get_celery_task_tracker, | ||
get_flag_assigned_cases_config, | ||
CeleryTaskTracker, | ||
update_cases_owner, | ||
get_geo_case_property, | ||
) | ||
from corehq.apps.geospatial.management.commands.index_geolocation_case_properties import ( | ||
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. Nit: It feels awkward for the tasks module to be importing from a management command, instead of the other way round (or both importing from somewhere else, but I'd prefer not to throw everything in the utils module, like that second drawer in the kitchen that somehow ends up not only with tongs and skewers, but also clothes pegs, and screws, and one rusty paper clip). 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. Perhaps a good alternative would be to move the Alternatively, I can also simply create a file in a new "utils" directory to contain just the above helper function. I do feel the first option makes sense enough and is a bit more straightforward, but happy to go in this direction as well if you think it sounds more suitable @kaapstorm. 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. The 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. Addressed in a161b10. |
||
get_batch_count, | ||
process_batch, | ||
DEFAULT_QUERY_LIMIT, | ||
DEFAULT_CHUNK_SIZE, | ||
) | ||
|
||
from settings import MAX_GEOSPATIAL_INDEX_DOC_LIMIT | ||
|
||
|
||
@task(queue="background_queue", ignore_result=True) | ||
|
@@ -14,3 +28,42 @@ def geo_cases_reassignment_update_owners(domain, case_owner_updates_dict, task_k | |
finally: | ||
celery_task_tracker = CeleryTaskTracker(task_key) | ||
celery_task_tracker.mark_completed() | ||
|
||
|
||
@task(queue='geospatial_queue', ignore_result=True) | ||
def index_es_docs_with_location_props(domain): | ||
celery_task_tracker = get_celery_task_tracker(domain, INDEX_ES_TASK_HELPER_BASE_KEY) | ||
if celery_task_tracker.is_active(): | ||
return | ||
|
||
geo_case_prop = get_geo_case_property(domain) | ||
query = case_query_for_missing_geopoint_val(domain, geo_case_prop) | ||
doc_count = query.count() | ||
if doc_count > MAX_GEOSPATIAL_INDEX_DOC_LIMIT: | ||
celery_task_tracker.mark_as_error(error_slug='TOO_MANY_CASES') | ||
return | ||
|
||
celery_task_tracker.mark_requested() | ||
batch_count = get_batch_count(doc_count, DEFAULT_QUERY_LIMIT) | ||
try: | ||
for i in range(batch_count): | ||
docs_left = doc_count - (DEFAULT_QUERY_LIMIT * i) | ||
limit = min(DEFAULT_QUERY_LIMIT, docs_left) | ||
process_batch( | ||
domain, | ||
geo_case_prop, | ||
case_type=None, | ||
query_limit=limit, | ||
chunk_size=DEFAULT_CHUNK_SIZE, | ||
offset=i * DEFAULT_QUERY_LIMIT, | ||
) | ||
celery_task_tracker.update_progress(current=i + 1, total=batch_count) | ||
except Exception as e: | ||
celery_task_tracker.mark_as_error(error_slug='CELERY') | ||
notify_exception( | ||
None, | ||
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. nit: how do we ensure this is followed up on? 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. The domain will receive an error alert if the Celery task fails, asking them to reach out to support. Sending this off to Sentry will allow us to have a bit more insight other than a generic 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. Cool, that works 👍 |
||
'Something went wrong with index_es_docs_with_location_props()', | ||
details={'error': str(e)} | ||
) | ||
else: | ||
celery_task_tracker.mark_completed() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{% load i18n %} | ||
|
||
{% if task_status.status == 'ERROR' %} | ||
<div class="alert alert-danger"> | ||
<p> | ||
{% if task_status.error_slug == 'TOO_MANY_CASES' %} | ||
{% blocktrans %} | ||
This domain contains too many cases and so they will not be made available | ||
for use by this feature. Please reach out to support. | ||
{% endblocktrans %} | ||
{% elif task_status.error_slug == 'CELERY' %} | ||
{% blocktrans %} | ||
Oops! Something went wrong while attempting to make cases ready for use | ||
by this feature. Please reach out to support. | ||
{% endblocktrans %} | ||
{% endif %} | ||
</p> | ||
</div> | ||
{% elif task_status.status == 'ACTIVE' %} | ||
<div class="alert alert-info"> | ||
<p> | ||
{% blocktrans %} | ||
Cases are being made ready for use by this feature. Please be patient. | ||
{% endblocktrans %} | ||
({{ task_status.progress}}%) | ||
</p> | ||
</div> | ||
{% endif %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥇