Exemplo n.º 1
0
    def test_get_redis_from_config(self):
        """Ensure Redis connection params are properly parsed"""
        settings = {
            'REDIS_URL': 'redis://localhost:1/1'
        }

        # Ensure REDIS_URL is read
        redis = get_redis_from_config(settings)
        connection_kwargs = redis.connection_pool.connection_kwargs
        self.assertEqual(connection_kwargs['db'], 1)
        self.assertEqual(connection_kwargs['port'], 1)

        settings = {
            'REDIS_URL': 'redis://localhost:1/1',
            'REDIS_HOST': 'foo',
            'REDIS_DB': 2,
            'REDIS_PORT': 2,
            'REDIS_PASSWORD': '******'
        }

        # Ensure REDIS_URL is preferred
        redis = get_redis_from_config(settings)
        connection_kwargs = redis.connection_pool.connection_kwargs
        self.assertEqual(connection_kwargs['db'], 1)
        self.assertEqual(connection_kwargs['port'], 1)

        # Ensure fall back to regular connection parameters
        settings['REDIS_URL'] = None
        redis = get_redis_from_config(settings)
        connection_kwargs = redis.connection_pool.connection_kwargs
        self.assertEqual(connection_kwargs['host'], 'foo')
        self.assertEqual(connection_kwargs['db'], 2)
        self.assertEqual(connection_kwargs['port'], 2)
        self.assertEqual(connection_kwargs['password'], 'bar')
Exemplo n.º 2
0
def get_redis_connection():
    global _conn

    if _conn is None:
        _conn = get_redis_from_config(settings)

    return _conn
Exemplo n.º 3
0
def get_async_redis_connection():
    global _a_conn

    if _a_conn is None:
        _a_conn = get_redis_from_config(settings, aRedis)

    return _a_conn
Exemplo n.º 4
0
def get_redis_connection():
    global _conn

    if _conn is None:
        _conn = get_redis_from_config(settings)

    return _conn
Exemplo n.º 5
0
import os

from db import get_items, record_item, rollback
from flask import abort, Blueprint, Flask, jsonify, request
from rq import Queue
from rq.cli.helpers import get_redis_from_config

q = Queue(connection=get_redis_from_config(os.environ))
root = Blueprint('root', __name__)


@root.route('/')
def index():
  return "Hello, world"


@root.route('/items', methods=['GET', 'POST'])
def items():
  if request.method == 'GET':
    return jsonify({'items': get_items()})
  elif request.method == 'POST':
    q.enqueue(record_item, request.get_json())
    return jsonify({'ok': True})
  else:
    abort(405)


@root.teardown_request
def rollback_on_exception(exc):
  if exc:
    rollback()
Exemplo n.º 6
0
import os

from db import get_items, record_item, rollback
from flask import abort, Blueprint, Flask, jsonify, request
from rq import Queue
from rq.cli.helpers import get_redis_from_config

q = Queue(connection=get_redis_from_config(os.environ))
root = Blueprint('root', __name__)


@root.route('/')
def index():
    return "Hello, world"


@root.route('/items', methods=['GET', 'POST'])
def items():
    if request.method == 'GET':
        return jsonify({'items': get_items()})
    elif request.method == 'POST':
        q.enqueue(record_item, request.get_json())
        return jsonify({'ok': True})
    else:
        abort(405)


@root.teardown_request
def rollback_on_exception(exc):
    if exc:
        rollback()