added new daemon events work

master
Kevin Froman 2019-12-30 18:57:14 -06:00
parent a4762ccf9a
commit 66aafb6deb
3 changed files with 95 additions and 10 deletions

36
docs/dev/daemon-events.md Normal file
View File

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

View File

@ -1,9 +1,9 @@
''' """
Onionr - Private P2P Communication Onionr - Private P2P Communication
This file handles configuration setting and getting from the HTTP API This file handles configuration setting and getting from the HTTP API
''' """
''' """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or 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 You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' """
import json import json
from flask import Blueprint, request, Response, abort from flask import Blueprint, request, Response, abort
import config, onionrutils import config, onionrutils
@ -26,17 +26,17 @@ config_BP = Blueprint('config_BP', __name__)
@config_BP.route('/config/get') @config_BP.route('/config/get')
def get_all_config(): 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)) return Response(json.dumps(config.get_config(), indent=4, sort_keys=True))
@config_BP.route('/config/get/<key>') @config_BP.route('/config/get/<key>')
def get_by_key(key): def get_by_key(key):
'''Return a config setting by key''' """Return a config setting by key"""
return Response(json.dumps(config.get(key))) return Response(json.dumps(config.get(key)))
@config_BP.route('/config/setall', methods=['POST']) @config_BP.route('/config/setall', methods=['POST'])
def set_all_config(): def set_all_config():
'''Overwrite existing JSON config with new JSON string''' """Overwrite existing JSON config with new JSON string"""
try: try:
new_config = request.get_json(force=True) new_config = request.get_json(force=True)
except json.JSONDecodeError: except json.JSONDecodeError:
@ -48,12 +48,12 @@ def set_all_config():
@config_BP.route('/config/set/<key>', methods=['POST']) @config_BP.route('/config/set/<key>', methods=['POST'])
def set_by_key(key): def set_by_key(key):
'''Overwrite/set only 1 config key''' """Overwrite/set only 1 config key"""
''' """
{ {
'data': data 'data': data
} }
''' """
try: try:
data = json.loads(onionrutils.OnionrUtils.bytesToStr(request.data))['data'] data = json.loads(onionrutils.OnionrUtils.bytesToStr(request.data))['data']
except (json.JSONDecodeError, KeyError): except (json.JSONDecodeError, KeyError):

View File

@ -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 <https://www.gnu.org/licenses/>.
"""
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