From 66aafb6deb0acfe93148f4a4de5f270da73ddea0 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 30 Dec 2019 18:57:14 -0600 Subject: [PATCH] added new daemon events work --- docs/dev/daemon-events.md | 36 ++++++++++++++++++++ src/httpapi/configapi/__init__.py | 20 ++++++------ src/httpapi/daemonevents/__init__.py | 49 ++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 docs/dev/daemon-events.md create mode 100644 src/httpapi/daemonevents/__init__.py diff --git a/docs/dev/daemon-events.md b/docs/dev/daemon-events.md new file mode 100644 index 00000000..25769344 --- /dev/null +++ b/docs/dev/daemon-events.md @@ -0,0 +1,36 @@ +# DaemonEvents + +For things that need to be processed by the daemon + +Observer pattern + +Register listeners dynamically per event + +Spawn new greenlets + +------------------- + +## Attributes + +events: dict + +schema: + +{ + "event_id": dict{ + "event_name": string, + "result_data": bytes, + "started": epoch, + "finished": epoch, + "done": bool + } +} + + +-------------------- + +MsgPack schema: + +event_name: string +event_id: uuid4 + diff --git a/src/httpapi/configapi/__init__.py b/src/httpapi/configapi/__init__.py index d760ff7b..c5345e2b 100755 --- a/src/httpapi/configapi/__init__.py +++ b/src/httpapi/configapi/__init__.py @@ -1,9 +1,9 @@ -''' +""" Onionr - Private P2P Communication This file handles configuration setting and getting from the HTTP API -''' -''' +""" +""" This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or @@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . -''' +""" import json from flask import Blueprint, request, Response, abort import config, onionrutils @@ -26,17 +26,17 @@ config_BP = Blueprint('config_BP', __name__) @config_BP.route('/config/get') def get_all_config(): - '''Simply return all configuration as JSON string''' + """Simply return all configuration as JSON string""" return Response(json.dumps(config.get_config(), indent=4, sort_keys=True)) @config_BP.route('/config/get/') def get_by_key(key): - '''Return a config setting by key''' + """Return a config setting by key""" return Response(json.dumps(config.get(key))) @config_BP.route('/config/setall', methods=['POST']) def set_all_config(): - '''Overwrite existing JSON config with new JSON string''' + """Overwrite existing JSON config with new JSON string""" try: new_config = request.get_json(force=True) except json.JSONDecodeError: @@ -48,12 +48,12 @@ def set_all_config(): @config_BP.route('/config/set/', methods=['POST']) def set_by_key(key): - '''Overwrite/set only 1 config key''' - ''' + """Overwrite/set only 1 config key""" + """ { 'data': data } - ''' + """ try: data = json.loads(onionrutils.OnionrUtils.bytesToStr(request.data))['data'] except (json.JSONDecodeError, KeyError): diff --git a/src/httpapi/daemonevents/__init__.py b/src/httpapi/daemonevents/__init__.py new file mode 100644 index 00000000..6f8aa509 --- /dev/null +++ b/src/httpapi/daemonevents/__init__.py @@ -0,0 +1,49 @@ +"""Onionr - Private P2P Communication. + +Event driven interface to trigger events in communicator +""" +import json +from flask import Blueprint, request, Response +import config +""" + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +""" + + +event_BP = Blueprint('event_BP', __name__) + + +class DaemonEvents: + def __init__(self): + """Create DaemonEvents instance, intended to be a singleton. + + Attributes: + events: dict of current/finished events + listeners: callables that are called when a new event is added. + The callables name should match the event name + _too_many: TooManyObjects instance set by external code + """ + self.events = {} + self.flask_bp = event_BP + event_BP = self.flask_bp + + @event_BP.route('/daemon-event', methods=['POST']) + def daemon_event_handler() -> Response: + return + + def clean_old(self): + """Deletes old daemon events based on their completion date.""" + pass + +