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 pprint
|
||||||
|
import logging
|
||||||
import copy
|
import copy
|
||||||
|
from werkzeug.exceptions import BadRequest
|
||||||
|
|
||||||
class JsonPayload:
|
class JsonPayload:
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
|
|
@ -8,7 +9,11 @@ class JsonPayload:
|
||||||
Object to wrap up simple functionality for parsing a JSON response.
|
Object to wrap up simple functionality for parsing a JSON response.
|
||||||
"""
|
"""
|
||||||
# Try to load as json
|
# 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:
|
if self.json is None:
|
||||||
self.json = {}
|
self.json = {}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,8 @@ class TaskListAPI(MicroscopeView):
|
||||||
:>header Content-Type: application/json
|
:>header Content-Type: application/json
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
print(self.microscope.task.state)
|
||||||
|
|
||||||
data = self.microscope.task.state
|
data = self.microscope.task.state
|
||||||
|
|
||||||
return jsonify(data)
|
return jsonify(data)
|
||||||
|
|
|
||||||
|
|
@ -74,3 +74,22 @@ class LongRunningAPI(MicroscopeViewPlugin):
|
||||||
|
|
||||||
except TaskDeniedException:
|
except TaskDeniedException:
|
||||||
return abort(409)
|
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
|
import time
|
||||||
from openflexure_microscope.plugins import MicroscopePlugin
|
from openflexure_microscope.plugins import MicroscopePlugin
|
||||||
|
|
||||||
from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI
|
from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI, SomeExceptionAPI
|
||||||
|
|
||||||
|
|
||||||
class Plugin(MicroscopePlugin):
|
class Plugin(MicroscopePlugin):
|
||||||
|
|
@ -14,6 +14,7 @@ class Plugin(MicroscopePlugin):
|
||||||
'/identify': IdentifyAPI,
|
'/identify': IdentifyAPI,
|
||||||
'/hello': HelloWorldAPI,
|
'/hello': HelloWorldAPI,
|
||||||
'/long_running': LongRunningAPI,
|
'/long_running': LongRunningAPI,
|
||||||
|
'/some_exception': SomeExceptionAPI,
|
||||||
}
|
}
|
||||||
|
|
||||||
def identify(self):
|
def identify(self):
|
||||||
|
|
@ -32,6 +33,16 @@ class Plugin(MicroscopePlugin):
|
||||||
|
|
||||||
return "Hello world!"
|
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):
|
def long_running(self, t_run):
|
||||||
"""
|
"""
|
||||||
Demonstrate a long-running method that requires microscope hardware
|
Demonstrate a long-running method that requires microscope hardware
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import datetime
|
import datetime
|
||||||
|
import logging
|
||||||
|
import traceback
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
|
@ -111,7 +113,9 @@ class Task:
|
||||||
r = f(*args, **kwargs)
|
r = f(*args, **kwargs)
|
||||||
s = 'success'
|
s = 'success'
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
r = e
|
logging.error(e)
|
||||||
|
logging.error(traceback.format_exc())
|
||||||
|
r = str(e)
|
||||||
s = 'error'
|
s = 'error'
|
||||||
self.state['return'] = r
|
self.state['return'] = r
|
||||||
self.state['status'] = s
|
self.state['status'] = s
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue