summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Shetye2021-12-03 19:37:41 +0530
committerSunil Shetye2021-12-03 21:55:31 +0530
commit0d6272b44885a838cea4ba3fcb941b61560a85a0 (patch)
treed4e344fd2fab517641e28df8633e7f426cf26fd3
parent389bb9f60310dcb29d40c55d3b6502c0b72e1976 (diff)
downloadCommon-Interface-Project-0d6272b44885a838cea4ba3fcb941b61560a85a0.tar.gz
Common-Interface-Project-0d6272b44885a838cea4ba3fcb941b61560a85a0.tar.bz2
Common-Interface-Project-0d6272b44885a838cea4ba3fcb941b61560a85a0.zip
use StreamingHttpResponse instead of Response
use IgnoreClientContentNegotiation for text/event-stream use Streaming instead of Stream set Streaming state as early as possible
-rw-r--r--blocks/simulationAPI/helpers/ngspice_helper.py7
-rw-r--r--blocks/simulationAPI/negotiation.py14
-rw-r--r--blocks/simulationAPI/tasks.py21
-rw-r--r--blocks/simulationAPI/urls.py2
-rw-r--r--blocks/simulationAPI/views.py26
5 files changed, 49 insertions, 21 deletions
diff --git a/blocks/simulationAPI/helpers/ngspice_helper.py b/blocks/simulationAPI/helpers/ngspice_helper.py
index 12e68cb7..7f1f5185 100644
--- a/blocks/simulationAPI/helpers/ngspice_helper.py
+++ b/blocks/simulationAPI/helpers/ngspice_helper.py
@@ -2,6 +2,7 @@ import os
import logging
import re
import subprocess
+from celery import current_task
from datetime import datetime
from pathlib import Path
from tempfile import mkstemp
@@ -75,6 +76,10 @@ def ExecXml(filepath, file_id, parameters):
os.close(LOGFILEFD)
+ current_task.update_state(
+ state='STREAMING',
+ meta={'current_process': 'Processed Xml, Streaming Output'})
+
cmd = "try;"
cmd += "chdir('%s');" % current_dir
cmd += "loadXcosLibs();"
@@ -102,7 +107,7 @@ def ExecXml(filepath, file_id, parameters):
logger.info('err=%s', err)
log_name = '/tmp/blocks-tmp/scilab-log.txt' # FIXME: remove this line
- return ('Success', log_name, proc.returncode)
+ return ('Streaming', log_name, proc.returncode)
except BaseException as e:
logger.exception('Encountered Exception:')
logger.info('removing %s', filepath)
diff --git a/blocks/simulationAPI/negotiation.py b/blocks/simulationAPI/negotiation.py
new file mode 100644
index 00000000..c94f0ac5
--- /dev/null
+++ b/blocks/simulationAPI/negotiation.py
@@ -0,0 +1,14 @@
+from rest_framework.negotiation import BaseContentNegotiation
+
+class IgnoreClientContentNegotiation(BaseContentNegotiation):
+ def select_parser(self, request, parsers):
+ """
+ Select the first parser in the `.parser_classes` list.
+ """
+ return parsers[0]
+
+ def select_renderer(self, request, renderers, format_suffix):
+ """
+ Select the first renderer in the `.renderer_classes` list.
+ """
+ return (renderers[0], renderers[0].media_type)
diff --git a/blocks/simulationAPI/tasks.py b/blocks/simulationAPI/tasks.py
index f3c4891c..9ec1c4ea 100644
--- a/blocks/simulationAPI/tasks.py
+++ b/blocks/simulationAPI/tasks.py
@@ -1,11 +1,11 @@
-import json
-from celery import shared_task, current_task
-from celery import states
-from simulationAPI.helpers import ngspice_helper
+from celery import shared_task, current_task, states
from celery.exceptions import Ignore
+import json
+import logging
import traceback
+
+from simulationAPI.helpers import ngspice_helper
from simulationAPI.models import TaskFile
-import logging
logger = logging.getLogger(__name__)
@@ -28,13 +28,18 @@ def process_task(task_id):
meta={'current_process': 'Started Processing File'})
output = ngspice_helper.ExecXml(file_path, file_id, parameters)
- if output[0] == "Success":
+ if output[0] == "Streaming":
file_obj.log_name = output[1]
file_obj.returncode = output[2]
file_obj.save()
+ state = 'STREAMING'
+ current_process = 'Processed Xml, Streaming Output'
+ elif output[0] == "Success":
+ state = 'SUCCESS'
+ current_process = 'Processed Xml, Loading Output'
current_task.update_state(
- state='STREAM',
- meta={'current_process': 'Processed Xml, Streaming Output'})
+ state=state,
+ meta={'current_process': current_process})
return output[0]
except Exception as e:
diff --git a/blocks/simulationAPI/urls.py b/blocks/simulationAPI/urls.py
index f03380d1..f19eddc1 100644
--- a/blocks/simulationAPI/urls.py
+++ b/blocks/simulationAPI/urls.py
@@ -9,6 +9,6 @@ urlpatterns = [
path('status/<uuid:task_id>',
simulationAPI_views.CeleryResultView.as_view(), name='celery_status'),
- path('stream/<uuid:task_id>',
+ path('streaming/<uuid:task_id>',
simulationAPI_views.StreamView.as_view(), name='stream_status'),
]
diff --git a/blocks/simulationAPI/views.py b/blocks/simulationAPI/views.py
index 7894a1b6..11261352 100644
--- a/blocks/simulationAPI/views.py
+++ b/blocks/simulationAPI/views.py
@@ -1,17 +1,20 @@
-from simulationAPI.models import TaskFile
-from simulationAPI.serializers import TaskSerializer
-from simulationAPI.tasks import process_task
+import logging
+import os
+import time
+import uuid
+from celery.result import AsyncResult
+from django.http import StreamingHttpResponse
from rest_framework import status
from rest_framework.exceptions import ValidationError
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.permissions import AllowAny
-from rest_framework.views import APIView
from rest_framework.response import Response
-from celery.result import AsyncResult
-import os
-import uuid
-import logging
-import time
+from rest_framework.views import APIView
+
+from simulationAPI.models import TaskFile
+from simulationAPI.negotiation import IgnoreClientContentNegotiation
+from simulationAPI.serializers import TaskSerializer
+from simulationAPI.tasks import process_task
SCILAB_INSTANCE_TIMEOUT_INTERVAL = 300
@@ -152,14 +155,15 @@ class StreamView(APIView):
Streams Simulation results for 'task_id' provided after
uploading the xml
- /api/stream/<uuid>
+ /api/streaming/<uuid>
"""
permission_classes = (AllowAny,)
methods = ['GET']
+ content_negotiation_class = IgnoreClientContentNegotiation
def get(self, request, task_id):
- return Response(self.event_stream(task_id), content_type='text/event-stream')
+ return StreamingHttpResponse(self.event_stream(task_id), content_type='text/event-stream')
def get_log_name(self, task_id):
while True: