Python redis.from_url() Examples

The following are 30 code examples of redis.from_url(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module redis , or try the search function .
Example #1
Source File: views.py    From flask-redis-queue with MIT License 7 votes vote down vote up
def get_status(task_id):
    with Connection(redis.from_url(current_app.config["REDIS_URL"])):
        q = Queue()
        task = q.fetch_job(task_id)
    if task:
        response_object = {
            "status": "success",
            "data": {
                "task_id": task.get_id(),
                "task_status": task.get_status(),
                "task_result": task.result,
            },
        }
    else:
        response_object = {"status": "error"}
    return jsonify(response_object) 
Example #2
Source File: manage.py    From flask-spark-docker with MIT License 6 votes vote down vote up
def run_worker():
    redis_url = app.config['REDIS_URL']
    redis_connection = redis.from_url(redis_url)
    with Connection(redis_connection):
        worker = Worker(app.config['QUEUES'])
        worker.work() 
Example #3
Source File: redis_store.py    From plaso with Apache License 2.0 6 votes vote down vote up
def _GetRedisClient(self):
    """Creates a Redis client for testing.

    This method will attempt to use a Redis server listening on localhost and
    fallback to a fake Redis client if no server is availble or the connection
    timed out.

    Returns:
      Redis: a Redis client.
    """
    try:
      redis_client = redis.from_url(self._REDIS_URL, socket_timeout=60)
      redis_client.ping()
    except redis.exceptions.ConnectionError:
      redis_client = fakeredis.FakeStrictRedis()

    return redis_client 
Example #4
Source File: backends.py    From syntheticmass with Apache License 2.0 6 votes vote down vote up
def redis(app, config, args, kwargs):
        kwargs.update(dict(
            host=config.get('CACHE_REDIS_HOST', 'localhost'),
            port=config.get('CACHE_REDIS_PORT', 6379),
        ))
        password = config.get('CACHE_REDIS_PASSWORD')
        if password:
            kwargs['password'] = password

        key_prefix = config.get('CACHE_KEY_PREFIX')
        if key_prefix:
            kwargs['key_prefix'] = key_prefix

        db_number = config.get('CACHE_REDIS_DB')
        if db_number:
            kwargs['db'] = db_number

        redis_url = config.get('CACHE_REDIS_URL')
        if redis_url:
            kwargs['host'] = redis_from_url(
                                redis_url,
                                db=kwargs.pop('db', None),
                            )

        return RedisCache(*args, **kwargs) 
Example #5
Source File: gcp.py    From code-coverage with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, reports_dir=None):
        # Open redis connection
        self.redis = redis.from_url(taskcluster.secrets["REDIS_URL"])
        assert self.redis.ping(), "Redis server does not ping back"

        # Open gcp connection to bucket
        assert (
            taskcluster.secrets["GOOGLE_CLOUD_STORAGE"] is not None
        ), "Missing GOOGLE_CLOUD_STORAGE secret"
        self.bucket = get_bucket(taskcluster.secrets["GOOGLE_CLOUD_STORAGE"])

        # Local storage for reports
        self.reports_dir = reports_dir or os.path.join(
            tempfile.gettempdir(), "ccov-reports"
        )
        os.makedirs(self.reports_dir, exist_ok=True)
        logger.info("Reports will be stored in {}".format(self.reports_dir))

        # Load most recent reports in cache
        for repo in REPOSITORIES:
            for report in self.list_reports(repo, nb=1):
                self.download_report(report) 
Example #6
Source File: views.py    From openci with MIT License 6 votes vote down vote up
def grade_project(project_id):
    project = Project.query.filter_by(id=project_id).first_or_404()
    with Connection(redis.from_url(current_app.config['REDIS_URL'])):
        q = Queue()
        task = q.enqueue(
            create_task,
            project.url,
            current_app.config["OPENFAAS_URL"]
        )
    response_object = {
        'status': 'success',
        'data': {
            'task_id': task.get_id()
        }
    }
    return jsonify(response_object), 202 
Example #7
Source File: worker_bot.py    From mee6 with MIT License 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        self.broker_url = config.BROKER_URL or DEFAULT_BROKER_URL
        self.redis_url = config.REDIS_URL or DEFAULT_BROKER_URL

        self.log = logging.getLogger('worker').info

        self.redis = redis.from_url(self.redis_url,
                                    decode_responses=True)
        self.log('Connected to redis database')

        discord_config = ClientConfig()
        discord_config.token = config.MEE6_TOKEN
        discord_client = Client(discord_config)
        self.api = discord_client.api

        self.listeners = []
        self.plugins = [] 
Example #8
Source File: config.py    From nidaba with GNU General Public License v2.0 6 votes vote down vote up
def reload_config():
    """
    Triggers a global reloading of the configuration files and reinitializes
    the redis connection pool.

    As of now configuration files are only read from sys.prefix/etc/nidaba/.
    """
    global nidaba_cfg, celery_cfg, Redis
    ipath = path.join(sys.prefix, 'etc', 'nidaba', 'nidaba.yaml')
    with open(ipath, 'rb') as fp:
        nidaba_cfg = yaml.safe_load(fp)
    if 'redis_url' not in nidaba_cfg:
        raise NidabaConfigException('No redis URL defined')
    Redis = redis.from_url(nidaba_cfg['redis_url'])

    cpath = path.join(sys.prefix, 'etc', 'nidaba', 'celery.yaml')
    with open(cpath, 'rb') as fp:
        celery_cfg = yaml.safe_load(fp) 
Example #9
Source File: app.py    From AlexaBot with MIT License 6 votes vote down vote up
def gettoken(uid):
    red = redis.from_url(redis_url)
    token = red.get(uid+"-access_token")
    refresh = red.get(uid+"-refresh_token")
    if token:
        return token
    elif refresh:
        #good refresh token
        try:
            payload = {"client_id" : Client_ID, "client_secret" : Client_Secret, "refresh_token" : refresh, "grant_type" : "refresh_token", }
            url = "https://api.amazon.com/auth/o2/token"
            r = requests.post(url, data = payload)
            resp = json.loads(r.text)
            red.set(uid+"-access_token", resp['access_token'])
            red.expire(uid+"-access_token", 3600)
            return resp['access_token']
        #bad refresh token
        except:
            return False
    else:
        return False

# Get Alexa's [text] response to a [text] query 
Example #10
Source File: app.py    From AlexaBot with MIT License 6 votes vote down vote up
def get(self):
        code=self.get_argument("code")
        mid=self.get_cookie("user")
        path = "https" + "://" + self.request.host 
        callback = path+"/code"
        payload = {"client_id" : Client_ID, "client_secret" : Client_Secret, "code" : code, "grant_type" : "authorization_code", "redirect_uri" : callback }
        url = "https://api.amazon.com/auth/o2/token"
        r = requests.post(url, data = payload)
        red = redis.from_url(redis_url)
        resp = json.loads(r.text)
        if mid != None:
            print("fetched MID: ",mid)
            red.set(mid+"-access_token", resp['access_token'])
            red.expire(mid+"-access_token", 3600)
            red.set(mid+"-refresh_token", resp['refresh_token'])
            self.render("static/return.html")
            bot.send_text_message(mid, "Great, you're logged in. Start talking to Alexa!")
        else:
            self.redirect("/?refreshtoken="+resp['refresh_token']) 
Example #11
Source File: test_storage.py    From limits with MIT License 5 votes vote down vote up
def setUp(self):
        pymemcache.client.Client(('localhost', 22122)).flush_all()
        redis.from_url('unix:///tmp/limits.redis.sock').flushall()
        redis.from_url("redis://localhost:7379").flushall()
        redis.from_url("redis://:sekret@localhost:7389").flushall()
        redis.sentinel.Sentinel([
            ("localhost", 26379)
        ]).master_for("localhost-redis-sentinel").flushall()
        rediscluster.RedisCluster("localhost", 7000).flushall()
        if RUN_GAE:
            from google.appengine.ext import testbed
            tb = testbed.Testbed()
            tb.activate()
            tb.init_memcache_stub() 
Example #12
Source File: test_redis.py    From limits with MIT License 5 votes vote down vote up
def setUp(self):
        self.storage_url = "redis://localhost:7379"
        self.storage = RedisStorage(self.storage_url)
        redis.from_url(self.storage_url).flushall() 
Example #13
Source File: test_redis.py    From limits with MIT License 5 votes vote down vote up
def test_init_options(self):
        with mock.patch(
            "limits.storage.redis.get_dependency"
        ) as get_dependency:
            storage_from_string(self.storage_url, connection_timeout=1)
            self.assertEqual(
                get_dependency().from_url.call_args[1]['connection_timeout'], 1
            ) 
Example #14
Source File: app.py    From AlexaBot with MIT License 5 votes vote down vote up
def get(self):
        uid = tornado.escape.xhtml_escape(self.current_user)
        red = redis.from_url(redis_url)
        red.delete(uid+"-access_token")
        red.delete(uid+"-refresh_token")
        self.clear_cookie("user")
        self.set_header('Content-Type', 'text/plain')
        self.write("Logged Out, Goodbye")
        self.finish()

# Facebook Messenger webhook 
Example #15
Source File: test_redis.py    From limits with MIT License 5 votes vote down vote up
def setUp(self):
        self.storage_url = "redis+unix:///tmp/limits.redis.sock"
        self.storage = RedisStorage(self.storage_url)
        redis.from_url('unix:///tmp/limits.redis.sock').flushall() 
Example #16
Source File: test_redis.py    From limits with MIT License 5 votes vote down vote up
def test_init_options(self):
        with mock.patch(
            "limits.storage.redis.get_dependency"
        ) as get_dependency:
            storage_from_string(self.storage_url, connection_timeout=1)
            self.assertEqual(
                get_dependency().from_url.call_args[1]['connection_timeout'], 1
            ) 
Example #17
Source File: utils.py    From depsy with MIT License 5 votes vote down vote up
def setup_redis_for_unittests():
    # do the same thing for the redis db, set up the test redis database.  We're using DB Number 8
    r = redis.from_url("redis://localhost:6379", db=REDIS_UNITTEST_DATABASE_NUMBER)
    r.flushdb()
    return r


# from http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DropEverything
# with a few changes 
Example #18
Source File: tool.py    From SwarmOps with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_redis_engine():
    """ 创建redis连接 """
    from redis import from_url
    from config import REDIS as REDIS_URL
    return from_url(REDIS_URL) 
Example #19
Source File: redis.py    From zeus with Apache License 2.0 5 votes vote down vote up
def init_app(self, app):
        self.redis = redis.from_url(app.config["REDIS_URL"])
        self.logger = app.logger 
Example #20
Source File: redis_data_source.py    From Rqalpha-myquant-learning with Apache License 2.0 5 votes vote down vote up
def __init__(self, path, redis_uri):
        super(RedisDataSource, self).__init__(path)
        self._env = Environment.get_instance()
        import redis
        self._redis_client = redis.from_url(redis_uri) 
Example #21
Source File: redis.py    From Rqalpha-myquant-learning with Apache License 2.0 5 votes vote down vote up
def __init__(self, redis_url):
        import redis
        self._client = redis.from_url(redis_url) 
Example #22
Source File: redis_data_source.py    From rqalpha-mod-stock-realtime with Apache License 2.0 5 votes vote down vote up
def __init__(self, path, redis_uri):
        super(RedisDataSource, self).__init__(path)
        self._env = Environment.get_instance()
        import redis
        self._redis_client = redis.from_url(redis_uri) 
Example #23
Source File: config.py    From docket with Apache License 2.0 5 votes vote down vote up
def redis(cls):
        if cls._redis:
            return cls._redis
        if cls.get('DOCKET_NO_REDIS') or not cls.get('REDIS_URL'):
            return None
        cls._redis = redis.from_url(url=cls.get('REDIS_URL'))
        return cls._redis 
Example #24
Source File: connection.py    From openslack-crawler with Apache License 2.0 5 votes vote down vote up
def from_settings(settings):
    url = settings.get('REDIS_URL', REDIS_URL)
    host = settings.get('REDIS_HOST', REDIS_HOST)
    port = settings.get('REDIS_PORT', REDIS_PORT)

    # REDIS_URL takes precedence over host/port specification.
    if url:
        return redis.from_url(url)
    else:
        return redis.Redis(host=host, port=port) 
Example #25
Source File: conftest.py    From flask-limiter with MIT License 5 votes vote down vote up
def redis_connection():
    r = redis.from_url("redis://localhost:36379")
    r.flushall()
    return r 
Example #26
Source File: views.py    From openci with MIT License 5 votes vote down vote up
def get_status(project_id, task_id):
    with Connection(redis.from_url(current_app.config['REDIS_URL'])):
        q = Queue()
        task = q.fetch_job(task_id)
    if task:
        response_object = {
            'status': 'success',
            'data': {
                'task_id': task.get_id(),
                'task_status': task.get_status(),
                'task_result': task.result
            }
        }
        if task.get_status() == 'finished':
            project = Project.query.filter_by(id=project_id).first()
            project.status = False
            if bool(task.result['status']):
                project.status = True
            db.session.commit()
            db.session.add(
                Build(
                    project_id=project.id,
                    status=project.status,
                    datetime=datetime.today().strftime('%d-%m-%Y %H:%M:%S')
                )
            )
            db.session.commit()
    else:
        response_object = {'status': 'error'}
    return jsonify(response_object) 
Example #27
Source File: manage.py    From openci with MIT License 5 votes vote down vote up
def run_worker():
    redis_url = app.config['REDIS_URL']
    redis_connection = redis.from_url(redis_url)
    with Connection(redis_connection):
        worker = Worker(app.config['QUEUES'])
        worker.work() 
Example #28
Source File: views.py    From flask-spark-docker with MIT License 5 votes vote down vote up
def push_rq_connection():
    push_connection(redis.from_url(current_app.config['REDIS_URL'])) 
Example #29
Source File: inject.py    From satori with Apache License 2.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser('inject')
    parser.add_argument('--redis',    type=str, default='redis://localhost:6379/0')
    parser.add_argument('--id',       type=str, default='injected-test-event-id')
    parser.add_argument('--endpoint', type=str, default='test-endpoint')
    parser.add_argument('--note',     type=str, default="SpaceX have just blasted Facebook's satellite!")
    parser.add_argument('--status',   type=str.upper, default='PROBLEM')
    parser.add_argument('--level',    type=int, default=3)
    parser.add_argument('--metric',   type=str, default='facebook.satellite.healthy')
    parser.add_argument('--time',     type=int, default=int(time.time()))
    parser.add_argument('--tags',     type=eval, default={})
    parser.add_argument('--actual',   type=float, default=0)
    parser.add_argument('--expected', type=float, default=1)
    parser.add_argument('--teams',    type=str, nargs='+', default=['operation'])

    options = parser.parse_args()

    ev = {
        'status':   options.status,
        'tags':     options.tags,
        'metric':   options.metric,
        'groups':   options.teams,
        'id':       options.id,
        'endpoint': options.endpoint,
        'actual':   options.actual,
        'level':    options.level,
        'note':     options.note,
        'time':     options.time,
        'expected': options.expected,
    }

    r = redis.from_url(options.redis)
    r.rpush("satori-events:%s" % options.level, json.dumps(ev)) 
Example #30
Source File: views.py    From flask-redis-queue with MIT License 5 votes vote down vote up
def run_task():
    task_type = request.form["type"]
    with Connection(redis.from_url(current_app.config["REDIS_URL"])):
        q = Queue()
        task = q.enqueue(create_task, task_type)
    response_object = {
        "status": "success",
        "data": {
            "task_id": task.get_id()
        }
    }
    return jsonify(response_object), 202