Commit a11d2145 authored by Joel Collins's avatar Joel Collins
Browse files

Fixed broken error handling for long-running tasks

parent e3a27b02
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
import pprint
import logging
import copy

from werkzeug.exceptions import BadRequest

class JsonPayload:
    def __init__(self, request):
@@ -8,7 +9,11 @@ class JsonPayload:
        Object to wrap up simple functionality for parsing a JSON response.
        """
        # Try to load as json
        try:
            self.json = request.get_json()  #: dict: Dictionary representation of request JSON
        except BadRequest as e:
            logging.error(e)
            self.json = {}

        if self.json is None:
            self.json = {}
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ class TaskListAPI(MicroscopeView):
        :>header Content-Type: application/json
        """

        print(self.microscope.task.state)

        data = self.microscope.task.state

        return jsonify(data)
+19 −0
Original line number Diff line number Diff line
@@ -74,3 +74,22 @@ class LongRunningAPI(MicroscopeViewPlugin):

        except TaskDeniedException:
            return abort(409)

class SomeExceptionAPI(MicroscopeViewPlugin):
    """
    An example API plugin that uses a long-running but broken plugin method.
    """
    def post(self):
        """
        Method to call when an HTTP POST request is made.
        """
        # Get payload JSON
        payload = JsonPayload(request)

        # Attach the long-running method as a microscope task
        try:
            task = self.microscope.task.start(self.plugin.some_exception)
            return jsonify(task.state), 202

        except TaskDeniedException:
            return abort(409)
 No newline at end of file
+12 −1
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ import random
import time
from openflexure_microscope.plugins import MicroscopePlugin

from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI
from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI, SomeExceptionAPI


class Plugin(MicroscopePlugin):
@@ -14,6 +14,7 @@ class Plugin(MicroscopePlugin):
        '/identify': IdentifyAPI,
        '/hello': HelloWorldAPI,
        '/long_running': LongRunningAPI,
        '/some_exception': SomeExceptionAPI,
    }

    def identify(self):
@@ -32,6 +33,16 @@ class Plugin(MicroscopePlugin):

        return "Hello world!"

    def some_exception(self):
        """
        Demonstrate some broken plugin method that would be long running
        """
        with self.microscope.lock:
            time.sleep(3)
            result = 15./0

        return result

    def long_running(self, t_run):
        """
        Demonstrate a long-running method that requires microscope hardware
+5 −1
Original line number Diff line number Diff line
from threading import Thread
from functools import wraps
import datetime
import logging
import traceback
import time
import uuid

@@ -111,7 +113,9 @@ class Task:
                r = f(*args, **kwargs)
                s = 'success'
            except Exception as e:
                r = e
                logging.error(e)
                logging.error(traceback.format_exc())
                r = str(e)
                s = 'error'
            self.state['return'] = r
            self.state['status'] = s