Skip to content

Commit

Permalink
Add hack to handle mondrian exception with logging proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Oct 13, 2021
1 parent 27163a6 commit 60261dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions django_geosource/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import json
import sys
from io import BytesIO
from datetime import datetime, timedelta
from enum import Enum, IntEnum, auto

import fiona
import psycopg2
import pyexcel
from celery.result import AsyncResult
from celery.utils.log import LoggingProxy
from django.conf import settings
from django.contrib.gis.gdal.error import GDALException
from django.contrib.gis.geos import GEOSGeometry
Expand Down Expand Up @@ -400,6 +403,17 @@ class CommandSource(Source):
def refresh_data(self):
layer = self.get_layer()
begin_date = datetime.now()

try:
if isinstance(sys.stdout, LoggingProxy):
# Hack to be able to launch command with mondrian logging
sys.stdout.buffer = BytesIO()
sys.stdout.encoding = None
sys.stderr.buffer = BytesIO()
sys.stderr.encoding = None
except AttributeError:
pass

call_command(self.command)

self.clear_features(layer, begin_date)
Expand Down
3 changes: 3 additions & 0 deletions django_geosource/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def run_model_object_method(self, app, model, pk, method, success_state=states.S

try:
obj = Model.objects.get(pk=pk)

logger.info(f"Call method {method} on {obj}")
state = {"action": method, **getattr(obj, method)()}
logger.info(f"Method {method} on {obj} ended")

self.update_state(state=success_state, meta=state)

Expand Down
4 changes: 3 additions & 1 deletion django_geosource/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def refresh(self, request, pk):

source = self.get_object()

refresh_job = source.run_async_method("refresh_data")
force_refresh = request.query_params.get("force")

refresh_job = source.run_async_method("refresh_data", force=force_refresh)
if refresh_job:
return Response(data=source.get_status(), status=status.HTTP_202_ACCEPTED)

Expand Down

0 comments on commit 60261dd

Please sign in to comment.