| 1 |
# Copyright (C) 2013 Rackspace Hosting Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import contextlib |
| 16 |
|
| 17 |
from stevedore import driver |
| 18 |
|
| 19 |
from taskflow import exceptions as exc |
| 20 |
from taskflow import logging |
| 21 |
from taskflow.utils import misc |
| 22 |
|
| 23 |
|
| 24 |
# NOTE(harlowja): this is the entrypoint namespace, not the module namespace. |
| 25 |
BACKEND_NAMESPACE = 'taskflow.persistence' |
| 26 |
|
| 27 |
LOG = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
def fetch(conf, namespace=BACKEND_NAMESPACE, **kwargs): |
| 31 |
"""Fetch a persistence backend with the given configuration. |
| 32 |
|
| 33 |
This fetch method will look for the entrypoint name in the entrypoint |
| 34 |
namespace, and then attempt to instantiate that entrypoint using the |
| 35 |
provided configuration and any persistence backend specific kwargs. |
| 36 |
|
| 37 |
NOTE(harlowja): to aid in making it easy to specify configuration and |
| 38 |
options to a backend the configuration (which is typical just a dictionary) |
| 39 |
can also be a URI string that identifies the entrypoint name and any |
| 40 |
configuration specific to that backend. |
| 41 |
|
| 42 |
For example, given the following configuration URI:: |
| 43 |
|
| 44 |
mysql://<not-used>/?a=b&c=d |
| 45 |
|
| 46 |
This will look for the entrypoint named 'mysql' and will provide |
| 47 |
a configuration object composed of the URI's components, in this case that |
| 48 |
is ``{'a': 'b', 'c': 'd'}`` to the constructor of that persistence backend |
| 49 |
instance. |
| 50 |
""" |
| 51 |
backend, conf = misc.extract_driver_and_conf(conf, 'connection') |
| 52 |
# If the backend is like 'mysql+pymysql://...' which informs the |
| 53 |
# backend to use a dialect (supported by sqlalchemy at least) we just want |
| 54 |
# to look at the first component to find our entrypoint backend name... |
| 55 |
if backend.find("+") != -1: |
| 56 |
backend = backend.split("+", 1)[0] |
| 57 |
LOG.debug('Looking for %r backend driver in %r', backend, namespace) |
| 58 |
try: |
| 59 |
mgr = driver.DriverManager(namespace, backend, |
| 60 |
invoke_on_load=True, |
| 61 |
invoke_args=(conf,), |
| 62 |
invoke_kwds=kwargs) |
| 63 |
return mgr.driver |
| 64 |
except RuntimeError as e: |
| 65 |
raise exc.NotFound(f"Could not find backend {backend}: {e}") |
| 66 |
|
| 67 |
|
| 68 |
@contextlib.contextmanager |
| 69 |
def backend(conf, namespace=BACKEND_NAMESPACE, **kwargs): |
| 70 |
"""Fetches a backend, connects, upgrades, then closes it on completion. |
| 71 |
|
| 72 |
This allows a backend instance to be fetched, connected to, have its schema |
| 73 |
upgraded (if the schema is already up to date this is a no-op) and then |
| 74 |
used in a context manager statement with the backend being closed upon |
| 75 |
context manager exit. |
| 76 |
""" |
| 77 |
with contextlib.closing(fetch(conf, namespace=namespace, **kwargs)) as be: |
| 78 |
with contextlib.closing(be.get_connection()) as conn: |
| 79 |
conn.upgrade() |
| 80 |
yield be |