Fixed broken error handling for long-running tasks
This commit is contained in:
parent
e3a27b024a
commit
a11d2145d9
5 changed files with 45 additions and 4 deletions
|
|
@ -1,6 +1,7 @@
|
|||
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
|
||||
self.json = request.get_json() #: dict: Dictionary representation of request 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 = {}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -72,5 +72,24 @@ class LongRunningAPI(MicroscopeViewPlugin):
|
|||
task = self.microscope.task.start(self.plugin.long_running, time_to_run)
|
||||
return jsonify(task.state), 202
|
||||
|
||||
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)
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue