Added HTTP control stuff
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -4,7 +4,8 @@ from config import Config
|
||||
from plugins import SleepPlugin, SyncPlugin, WaitPlugin, URRTDEPlugin, LogPlugin
|
||||
from plugin_repository import PluginRepository
|
||||
from program_executor import ProgramExecutor, ProgramExecutorStates
|
||||
from http_server import ControllerHTTPServer
|
||||
import weakref
|
||||
from tiny_http_server import TinyHTTPServer
|
||||
import logging
|
||||
import signal
|
||||
|
||||
@@ -14,14 +15,66 @@ from program_loader import load_program
|
||||
|
||||
class HTTPControl:
|
||||
|
||||
def GET_status(self, path, data):
|
||||
return 200, "Very good!"
|
||||
def __init__(self, executor: ProgramExecutor, wait_plugin: WaitPlugin):
|
||||
self._executor_ref = weakref.ref(executor)
|
||||
|
||||
def POST_terminate(self, path, data):
|
||||
return 200, "Will do sir!"
|
||||
if wait_plugin:
|
||||
self._wait_plugin_ref = weakref.ref(wait_plugin)
|
||||
else:
|
||||
self._wait_plugin_ref = None
|
||||
|
||||
def GET_status(self, path, data):
|
||||
|
||||
executor: ProgramExecutor = self._executor_ref()
|
||||
if not executor:
|
||||
return 503, "Service unavailable"
|
||||
|
||||
try:
|
||||
return 200, executor.get_status()
|
||||
except Exception as e:
|
||||
return 500, str(e)
|
||||
|
||||
def POST_abort(self, path, data):
|
||||
# TODO: announce to redis message bus
|
||||
executor: ProgramExecutor = self._executor_ref()
|
||||
if not executor:
|
||||
return 503, "Service unavailable"
|
||||
|
||||
try:
|
||||
executor.abort()
|
||||
except Exception as e:
|
||||
return 500, str(e)
|
||||
|
||||
return 200, "Aborting"
|
||||
|
||||
def POST_continue(self, path, data):
|
||||
return 201, "Will do sir!"
|
||||
|
||||
if not self._wait_plugin_ref:
|
||||
return 422, "Plugin not loaded"
|
||||
|
||||
wait_plugin: WaitPlugin = self._wait_plugin_ref()
|
||||
|
||||
if not wait_plugin:
|
||||
return 503, "Service unavailable"
|
||||
|
||||
try:
|
||||
wait_plugin.cont()
|
||||
except Exception as e:
|
||||
return 500, str(e)
|
||||
|
||||
return 200, "Continuing"
|
||||
|
||||
def POST_unloop(self, path, data):
|
||||
executor: ProgramExecutor = self._executor_ref()
|
||||
if not executor:
|
||||
return 503, "Service unavailable"
|
||||
|
||||
try:
|
||||
executor.stop_looping()
|
||||
except Exception as e:
|
||||
return 500, str(e)
|
||||
|
||||
return 200, "Looping off"
|
||||
|
||||
|
||||
def main() -> int:
|
||||
@@ -72,6 +125,20 @@ def main() -> int:
|
||||
logging.info("Preparing for execution...")
|
||||
executor = ProgramExecutor(program, loop=False)
|
||||
|
||||
# Prepare the HTTP Server
|
||||
if Config.HTTP_ENABLE:
|
||||
|
||||
try:
|
||||
loaded_wait_plugin = plugin_repo.get_plugin_instance('wait')
|
||||
except KeyError:
|
||||
loaded_wait_plugin = None
|
||||
|
||||
# The HTTP server stores weak reference to the executor and the wait plugin (if loaded)
|
||||
httpd = TinyHTTPServer(HTTPControl(executor, loaded_wait_plugin), port=Config.HTTP_PORT)
|
||||
httpd.start()
|
||||
else:
|
||||
httpd = None
|
||||
|
||||
# Setup signal handler
|
||||
def handle_stop_signal(signum, frame):
|
||||
logging.warning(f"Signal {signum} received. Aborting execution!")
|
||||
@@ -103,7 +170,11 @@ def main() -> int:
|
||||
|
||||
# Close all resources
|
||||
logging.info("Cleaning up...")
|
||||
if httpd:
|
||||
httpd.shutdown()
|
||||
plugin_repo.close()
|
||||
|
||||
# Set exit code
|
||||
return 0 if execution_success else 5
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user