Fixed broken error handling for long-running tasks

This commit is contained in:
Joel Collins 2019-01-28 17:49:44 +00:00
parent e3a27b024a
commit a11d2145d9
5 changed files with 45 additions and 4 deletions

View file

@ -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)

View file

@ -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