From ab11380b343e965e537ea569e39d33b304cf66c9 Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Wed, 22 Apr 2015 15:50:55 -0700 Subject: [PATCH] Refactored event worker plugin structure into plugin directory. Worker plugins are now part of the general plugin architecture and framework. Subscription plugin has also been moved to storyboard.plugin to indicate that it's a plugin (though inline). Change-Id: I8477c0aeda5270d6bc9ae1a4da90ab14e657961f --- doc/source/extending/index.rst | 4 +- setup.cfg | 6 +-- storyboard/notifications/subscriber.py | 2 +- .../daemon.py => plugin/event_worker.py} | 29 ++++++++++- .../subscription}/__init__.py | 0 .../subscription/base.py} | 2 +- storyboard/worker/task/__init__.py | 0 storyboard/worker/task/base.py | 51 ------------------- 8 files changed, 34 insertions(+), 60 deletions(-) rename storyboard/{worker/daemon.py => plugin/event_worker.py} (80%) rename storyboard/{worker => plugin/subscription}/__init__.py (100%) rename storyboard/{worker/task/subscription.py => plugin/subscription/base.py} (99%) delete mode 100644 storyboard/worker/task/__init__.py delete mode 100644 storyboard/worker/task/base.py diff --git a/doc/source/extending/index.rst b/doc/source/extending/index.rst index 171ce526..00b78139 100644 --- a/doc/source/extending/index.rst +++ b/doc/source/extending/index.rst @@ -23,7 +23,7 @@ setup.cfg file. For example:: [entry_points] storyboard.plugin.user_preferences = my-plugin-config = my.namespace.plugin:UserPreferences - storyboard.worker.task = + storyboard.plugin.worker = my-plugin-worker = my.namespace.plugin:EventWorker storyboard.plugin.cron = my-plugin-cron = my.namespace.plugin:CronWorker @@ -89,7 +89,7 @@ and your plugin can decide how to process each event in an asynchronous thread which will not impact the stability of the API:: [entry_points] - storyboard.worker.task = + storyboard.plugin.worker = my-plugin-worker = my.namespace.plugin:EventWorker To learn how to write a user preference plugin, please contribute to this diff --git a/setup.cfg b/setup.cfg index 7d33c5dc..42a538ac 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,12 +30,12 @@ data_files = console_scripts = storyboard-api = storyboard.api.app:start storyboard-subscriber = storyboard.notifications.subscriber:subscribe - storyboard-worker-daemon = storyboard.worker.daemon:run + storyboard-worker-daemon = storyboard.plugin.daemon:run_daemon storyboard-db-manage = storyboard.db.migration.cli:main storyboard-migrate = storyboard.migrate.cli:main storyboard-cron = storyboard.plugin.cron:main -storyboard.worker.task = - subscription = storyboard.worker.task.subscription:Subscription +storyboard.plugin.worker = + subscription = storyboard.plugin.subscription.base:Subscription storyboard.plugin.user_preferences = storyboard.plugin.scheduler = token-cleaner = storyboard.plugin.token_cleaner.cleaner:TokenCleaner diff --git a/storyboard/notifications/subscriber.py b/storyboard/notifications/subscriber.py index e62e5b1f..957089ef 100644 --- a/storyboard/notifications/subscriber.py +++ b/storyboard/notifications/subscriber.py @@ -44,7 +44,7 @@ def subscribe(): subscriber.start() manager = enabled.EnabledExtensionManager( - namespace='storyboard.worker.task', + namespace='storyboard.plugin.worker', check_func=check_enabled, invoke_on_load=True, invoke_args=(CONF,) diff --git a/storyboard/worker/daemon.py b/storyboard/plugin/event_worker.py similarity index 80% rename from storyboard/worker/daemon.py rename to storyboard/plugin/event_worker.py index cf2f07f2..094dcf01 100644 --- a/storyboard/worker/daemon.py +++ b/storyboard/plugin/event_worker.py @@ -12,6 +12,7 @@ # implied. See the License for the specific language governing permissions and # limitations under the License. +import abc import signal from multiprocessing import Process @@ -21,7 +22,7 @@ from threading import Timer from oslo.config import cfg from storyboard.notifications.subscriber import subscribe from storyboard.openstack.common.gettextutils import _LI, _LW # noqa - +from storyboard.plugin.base import PluginBase CONF = cfg.CONF LOG = log.getLogger(__name__) @@ -34,7 +35,7 @@ IMPORT_OPTS = [ ] -def run(): +def run_daemon(): """Start the daemon manager. """ global MANAGER @@ -151,3 +152,27 @@ class PerpetualTimer(): def cancel(self): self.thread.cancel() + + +class WorkerTaskBase(PluginBase): + """Base class for a worker that listens to API Events.""" + + __metaclass__ = abc.ABCMeta + + @abc.abstractmethod + def handle(self, author_id, method, path, status, resource, resource_id, + sub_resource=None, sub_resource_id=None, + resource_before=None, resource_after=None): + """Handle an event. + + :param author_id: ID of the author's user record. + :param method: The HTTP Method. + :param path: The full HTTP Path requested. + :param status: The returned HTTP Status of the response. + :param resource: The resource type. + :param resource_id: The ID of the resource. + :param sub_resource: The subresource type. + :param sub_resource_id: The ID of the subresource. + :param resource_before: The resource state before this event occurred. + :param resource_after: The resource state after this event occurred. + """ diff --git a/storyboard/worker/__init__.py b/storyboard/plugin/subscription/__init__.py similarity index 100% rename from storyboard/worker/__init__.py rename to storyboard/plugin/subscription/__init__.py diff --git a/storyboard/worker/task/subscription.py b/storyboard/plugin/subscription/base.py similarity index 99% rename from storyboard/worker/task/subscription.py rename to storyboard/plugin/subscription/base.py index da96fed2..c8a4ab4a 100644 --- a/storyboard/worker/task/subscription.py +++ b/storyboard/plugin/subscription/base.py @@ -17,7 +17,7 @@ import json import storyboard.db.api.base as db_api from storyboard.db.api import subscriptions as sub_api import storyboard.db.models as models -from storyboard.worker.task.base import WorkerTaskBase +from storyboard.plugin.event_worker import WorkerTaskBase class Subscription(WorkerTaskBase): diff --git a/storyboard/worker/task/__init__.py b/storyboard/worker/task/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/storyboard/worker/task/base.py b/storyboard/worker/task/base.py deleted file mode 100644 index a71607d4..00000000 --- a/storyboard/worker/task/base.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. -# -# 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 abc - - -class WorkerTaskBase(object): - """Base class for a worker that listens to events that occur within the - API. - """ - - __metaclass__ = abc.ABCMeta - - def __init__(self, config): - self.config = config - - @abc.abstractmethod - def enabled(self): - """A method which indicates whether this worker task is properly - configured and should be enabled. If it's ready to go, return True. - Otherwise, return False. - """ - - @abc.abstractmethod - def handle(self, author_id, method, path, status, resource, resource_id, - sub_resource=None, sub_resource_id=None, - resource_before=None, resource_after=None): - """Handle an event. - - :param author_id: ID of the author's user record. - :param method: The HTTP Method. - :param path: The full HTTP Path requested. - :param status: The returned HTTP Status of the response. - :param resource: The resource type. - :param resource_id: The ID of the resource. - :param sub_resource: The subresource type. - :param sub_resource_id: The ID of the subresource. - :param resource_before: The resource state before this event occurred. - :param resource_after: The resource state after this event occurred. - """