Split entrypoints into api and worker for simplicity.

Change-Id: I7045fd4b8a0b0a70265bafb16505f0c145cb53f6
This commit is contained in:
Robert Putt 2017-12-15 17:38:22 +00:00
parent dae39ef8ae
commit 778093da01
8 changed files with 158 additions and 17 deletions

View File

@ -1,6 +1,7 @@
[DEFAULT]
rpc_backend = rabbit
transport_url = rabbit://<user>:<password>@<host>/<vhost>
analysis_plugins =
[keystone_authtoken]
identity_uri =

32
python_nemesis/api_app.py Normal file
View File

@ -0,0 +1,32 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from python_nemesis.api.v1 import V1_API
from python_nemesis.base_app import configure_app
from python_nemesis.base_app import configure_blueprints
from python_nemesis.base_app import configure_extensions
from python_nemesis.base_app import configure_notifier
from python_nemesis.base_app import create_app
def create_api_app():
app = create_app('nemesis-api')
configure_app(app)
configure_extensions(app)
configure_notifier(app)
configure_blueprints(app, [V1_API])
return app
if __name__ == "__main__": # pragma: no cover
app = create_api_app()
app.run(threaded=True)

View File

@ -78,7 +78,7 @@ def configure_notifier(app):
topics=topics)
def create_app(app_name=None, blueprints=None):
def create_app(app_name=None):
"""Create the flask app.
This function is intended to be used with the app factory
@ -91,19 +91,4 @@ def create_app(app_name=None, blueprints=None):
:rtype: :py:class:`flask.Flask`
"""
app = Flask(app_name)
configure_app(app)
configure_extensions(app)
configure_notifier(app)
# Here we register the application blueprints.
from python_nemesis.api.v1 import V1_API
blueprints = [V1_API]
configure_blueprints(app, blueprints)
return app
if __name__ == "__main__": # pragma: no cover
app = create_app('nemesis-api')
app.run(threaded=True)

View File

@ -11,11 +11,12 @@
# under the License.
from oslo_config import cfg
from oslo_config import types
DEFAULT_OPT_GRP = cfg.OptGroup(name='DEFAULT')
DEFAULT_OPTS = [
cfg.StrOpt('test_value', default="this is a value")
cfg.ListOpt('analysis_plugins', item_type=types.String()),
]
SQLALCHEMY_OPT_GRP = cfg.OptGroup(name='sqlalchemy')

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
class NemesisPlugin(object):
def __init__(self, file_path):
self.file_path = file_path
def analyse(self):
return None

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from flask import current_app
import oslo_messaging
class NewFileEndpoint(object):
filter_rule = oslo_messaging.NotificationFilter(
event_type='nemsis.new_file')
def info(self, ctxt, publisher_id, event_type, payload, metadata):
print(payload)
def run_worker():
cfg = current_app.config['cfg']
transport = oslo_messaging.get_notification_transport(cfg)
targets = [
oslo_messaging.Target(topic='nemesis_notifications')
]
endpoints = [
NewFileEndpoint()
]
pool = "nemesis_notifications.info"
server = oslo_messaging.get_notification_listener(transport,
targets,
endpoints,
executor='threading',
pool=pool)
server.start()
server.wait()

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import importlib
def load_class(class_str):
# Dynamically loads a class based on the string provided.
class_data = class_str.split(".")
module_path = ".".join(class_data[:-1])
class_str = class_data[-1]
module = importlib.import_module(module_path)
return getattr(module, class_str)

View File

@ -0,0 +1,29 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from python_nemesis.base_app import configure_app
from python_nemesis.base_app import configure_extensions
from python_nemesis.base_app import create_app
from python_nemesis.worker import run_worker
def create_worker_app():
app = create_app('nemesis-worker')
configure_app(app)
configure_extensions(app)
return app
if __name__ == "__main__": # pragma: no cover
app = create_worker_app()
with app.app_context():
run_worker()