Skip to content
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

Add hack to handle mondrian exception with logging proxy #40

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 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,18 @@ class CommandSource(Source):
def refresh_data(self):
layer = self.get_layer()
begin_date = datetime.now()

try:
# wheter we are in a celery task ?
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