Skip to content

Commit

Permalink
Update gea area load tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 authored and AdityaKhatri committed Dec 4, 2024
1 parent 665f4b0 commit 3e13c57
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions apps/geo/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,34 +181,26 @@ def _load_geo_areas(region_id):
Basically, it starts with root admin level and iterate through all the
children.
"""
region = Region.objects.filter(pk=region_id).first()
if not region:
logger.error("Region not found", exc_info=True)
return False
try:
with reversion.create_revision():
if AdminLevel.objects.filter(region=region).count() == 0:
return True

parent_admin_levels = AdminLevel.objects.filter(
region=region, parent=None
)
completed_levels = []
_extract_from_admin_levels(
parent_admin_levels,
None,
completed_levels,
)
with reversion.create_revision():
region = Region.objects.get(pk=region_id)

region.calc_cache()
region.status = Region.Status.COMPLETED
region.save()
return True
if AdminLevel.objects.filter(region=region).count() == 0:
return True

except Exception:
logger.error('Load Geo Areas', exc_info=True)
region.status = Region.Status.FAILED
region.save()
parent_admin_levels = AdminLevel.objects.filter(
region=region, parent=None
)
completed_levels = []
_extract_from_admin_levels(
parent_admin_levels,
None,
completed_levels,
)

region.calc_cache()

return True


@shared_task
Expand All @@ -218,14 +210,17 @@ def load_geo_areas(region_id):
have_lock = lock.acquire(blocking=False)
if not have_lock:
return False

region = Region.objects.filter(id=region_id).first()
region.status = Region.Status.INITIATED
try:
region_id.status = Region.Status.INITIATED
return_value = _load_geo_areas(region_id)
region.status = Region.Status.COMPLETED
except Exception:
logger.error('Load Geo Areas', exc_info=True)
return_value = False
region.status = Region.Status.FAILED

region.save()
lock.release()
return return_value

Expand Down

0 comments on commit 3e13c57

Please sign in to comment.