Exemplo n.º 1
0
 def setUp(self):
     self.loop = TestIOLoop()
     CustomAssertionError.io_loop = self.loop
     self.client = brukva.Client(io_loop=self.loop)
     self.client.connection.connect()
     self.client.select(9)
     self.client.flushdb()
Exemplo n.º 2
0
 def __init__(self, *args, **kwargs):
     super(APIHandler, self).__init__(*args, **kwargs)
     self.client = brukva.Client()
     self.client.connect()
     self.io_loop = tornado.ioloop.IOLoop.instance()
     # Ниже принудительный сброс флага online при рестарте приложения (пока так)
     Node.objects.filter(online=True).update(online=False)
Exemplo n.º 3
0
 def __init__(self, *args, **kwargs):
     super(GameHandler, self).__init__(*args, **kwargs)
     self.client = brukva.Client(
         # host=settings.SESSION_REDIS_HOST,
         # port=int(settings.SESSION_REDIS_PORT)
     )
     self.client.connect()
Exemplo n.º 4
0
 def setUp(self):
     self.loop = IOLoop.instance()
     setattr(self.loop, 'handle_callback_exception',
             handle_callback_exception)
     CustomAssertionError.io_loop = self.loop
     self.client = brukva.Client(io_loop=self.loop)
     self.client.connection.connect()
     self.client.select(9)
     self.client.flushdb()
Exemplo n.º 5
0
    def _wait(self, token, cookie, callback):
        cls = SignalMixin

        # initialize redis
        if not cls.api:
            cls.api = redis.StrictRedis()

        if not cls.listener:
            cls.listener = brukva.Client()
            cls.listener.psubscribe(cls.CHANNEL_PREFIX + '*')
            cls.listener.listen(self._signal)

        # try to find the token
        object_key = cls.api.get(cls.TOKEN_PREFIX + token)
        if not object_key:
            return False

        # update token expiry
        cls.api.expire(cls.TOKEN_PREFIX + token, cls.TOKEN_TTL)

        # try to find to see if there is any event already
        event_keys = cls.api.keys(cls.EVENT_PREFIX + object_key + '.*')
        filtered_event_keys = []
        filtered_event_ids = []

        for event_key in event_keys:
            event_id = int(event_key.rsplit('.', 1)[1])
            if event_id > cookie:
                filtered_event_keys.append(event_key)
                filtered_event_ids.append(event_id)

        # get the list of events in one go and calculate new cookie
        events = []
        cookie = 0
        if filtered_event_keys:
            filtered_events = cls.api.mget(filtered_event_keys)
            for event_id, event in zip(filtered_event_ids, filtered_events):
                if event:
                    events.append(simplejson.loads(event))
                    if event_id > cookie:
                        cookie = event_id

        # early return to caller
        if events:
            return {
                'cookie': cookie,
                'events': events,
            }

        # register callback
        callbacks = []
        if object_key in cls.callbacks:
            callbacks = cls.callbacks[object_key]
        if not callback in callbacks:
            callbacks.append(callback)
        cls.callbacks[object_key] = callbacks
        return None
Exemplo n.º 6
0
 def get(self):
     """Subscribe to a channel.
     """
     channel_id = self.get_argument('id', None)
     logging.debug('GET: application key: %s' %channel_id)
     client = brukva.Client(port=6380)
     client.connect()
     logging.debug('subscribing to %s' %channel_id)
     client.subscribe([channel_id])
     client.listen(self.stream_channel_messages)
Exemplo n.º 7
0
    def __init__(self, ClientNUM, TEST_TIME, DataFile):
        self.ClientNUM = ClientNUM
        self.TEST_TIME = TEST_TIME
        self.DataFile = DataFile
        assert (self.DataFile["PROTOCOL_TYPE"] == u"RedisProtocol")

        self._client = brukva.Client(host=DataFile['HOST'],
                                     port=DataFile['PORT'],
                                     selected_db=DataFile['SELECTED_DB'])
        self._client.connect()
        self._io_loop = self._client.connection._stream.io_loop
Exemplo n.º 8
0
 def post(self):
     message = {
         "id": str(uuid.uuid4()),
         "from": self.current_user["first_name"],
         "body": self.get_argument("body"),
     }
     message["html"] = self.render_string("message.html", message=message)
     c = brukva.Client()
     c.connect()
     text = json.dumps(message)
     c.publish('chat_channel', text)
     self.finish(str(text))
Exemplo n.º 9
0
 def post(self):
     """Handle the POST request for publishing a message.
     """
     channel_id = self.get_argument('id', None)
     channel_data = {}
     channel_data['message'] = self.request.body
     channel_data['content_type'] = self.request.headers.get('Content-Type')
     channel_data['last_modified'] = self.request.headers.get('Last-Modified')
     client = brukva.Client(port=6380)
     client.connect()
     tornado.ioloop.IOLoop().instance().add_callback(
         lambda: self.publish_message(client, channel_id, channel_data))
def get_brukva():
    print 'Get Brukva connect with: %s:%s:%s' % \
        (settings.REDIS_HOST, settings.REDIS_PORT, settings.REDIS_DB)
    try:
        c = brukva.Client(host=settings.REDIS_HOST,
                          port=settings.REDIS_PORT,
                          selected_db=settings.REDIS_DB)
        c.connect()
    except:
        print traceback.format_exc()
    else:
        print 'Ok'
    return c
Exemplo n.º 11
0
    def __init__(self):
        # create a new redis connection
        redis_connection = brukva.Client()
        redis_connection.connect()

        handlers = [
            (r"/hits", HitsHandler, {
                "redis_connection": redis_connection
            }),
            (r"/hit", HitHandler, {
                "redis_connection": redis_connection
            }),
            (r"/(.*)", tornado.web.StaticFileHandler, {
                "path": os.path.join(os.getcwd(), "public")
            }),  # configure static file handler
        ]

        tornado.web.Application.__init__(self, handlers)
Exemplo n.º 12
0
def redis_connect():
    """
    Established an asynchronous resi connection.
    """
    # Get Redis connection settings for Heroku with fallback to defaults.
    redistogo_url = os.getenv('REDISTOGO_URL', None)
    if redistogo_url == None:
        REDIS_HOST = 'localhost'
        REDIS_PORT = 6379
        REDIS_PWD = None
        REDIS_USER = None
    else:
        redis_url = redistogo_url
        redis_url = redis_url.split('redis://')[1]
        redis_url = redis_url.split('/')[0]
        REDIS_USER, redis_url = redis_url.split(':', 1)
        REDIS_PWD, redis_url = redis_url.split('@', 1)
        REDIS_HOST, REDIS_PORT = redis_url.split(':', 1)
    client = brukva.Client(host=REDIS_HOST, port=int(REDIS_PORT), password=REDIS_PWD)
    client.connect()
    return client
Exemplo n.º 13
0
def redis_connect():
    """
    通过Heroku建立一个异步的redis服务器
    """
    redistogo_url = os.getenv('REDISTOGO_URL', None)
    if redistogo_url == None:
        REDIS_HOST = 'localhost'
        REDIS_PORT = 6379
        REDIS_PWD = None
        REDIS_USER = None
    else:
        redis_url = redistogo_url
        redis_url = redis_url.split('redis://')[1]
        redis_url = redis_url.split('/')[0]
        REDIS_USER, redis_url = redis_url.split(':', 1)
        REDIS_PWD, redis_url = redis_url.split('@', 1)
        REDIS_HOST, REDIS_PORT = redis_url.split(':', 1)

    client = brukva.Client(host=REDIS_HOST,
                           port=int(REDIS_PORT),
                           password=REDIS_PWD)
    client.connect()
    return client
Exemplo n.º 14
0
def redis_connect():
    """
    Redis connection
    """
    herokuredis_url = os.getenv('REDIS_URL', None)
    logging.warning(str(herokuredis_url))
    if herokuredis_url == None:
        REDIS_HOST = 'localhost'
        REDIS_PORT = 6379
        REDIS_PWD = None
        REDIS_USER = None
    else:
        redis_url = herokuredis_url
        redis_url = redis_url.split('redis://')[1]
        redis_url = redis_url.split('/')[0]
        REDIS_USER, redis_url = redis_url.split(':', 1)
        REDIS_PWD, redis_url = redis_url.split('@', 1)
        REDIS_HOST, REDIS_PORT = redis_url.split(':', 1)
    client = brukva.Client(host=REDIS_HOST,
                           port=int(REDIS_PORT),
                           password=REDIS_PWD)
    client.connect()
    return client
Exemplo n.º 15
0
    def __init__(self, host, port, dbnum, password=None):
        """Create our redis object, connecting to the specified host, port and
        database.

        :param host: Redis host to connect to
        :type host: str
        :param port: Redis port to connect to
        :type port: int
        :param dbnum: Redis database number to connect to
        :type dbnum: int
        :param password: Redis database password
        :type password: str
        """
        self._logger = logging.getLogger(__name__)

        ioloop_ = ioloop.IOLoop.instance()

        self._logger.info('Connecting to Redis (%s:%i:%i)', host, port, dbnum)
        self.client = brukva.Client(host, port, password, dbnum, ioloop_)

        try:
            self.client.connect()
        except brukva.ConnectionError as error:
            self._logger.error('Redis connection error: %s', error)
Exemplo n.º 16
0
#! /usr/bin/env python

from functools import partial
import time
import os
from pprint import pprint

import brukva

c = brukva.Client()
c.connect()

def delayed(dt, cmd,  *args, **kwargs):
    c._io_loop.add_timeout(
        time.time()+dt,
        partial(cmd, *args, **kwargs)
    )

def ac(cmd, *args, **kwargs):
    c._io_loop.add_callback(
        partial(cmd, *args, **kwargs)
    )

stt = time.time()
def on_resp(res):

    pprint(res)
    print (time.time() - stt)

c.set('gt', 'er', on_resp)
c.mget(['gt','sdfas'], on_resp)
Exemplo n.º 17
0
 def __init__(self, *args, **kwargs):
     super(MessagesHandler, self).__init__(*args, **kwargs)
     self.client = brukva.Client()
     self.client.connect()
Exemplo n.º 18
0
 def __init__(self, *args, **kwargs):
     super(MessagesCatcher, self).__init__(*args, **kwargs)
     self.client = brukva.Client()
     self.client.connect()
     self.client.subscribe('test_channel')
Exemplo n.º 19
0
 def setUp(self, *args, **kwargs):
     super(PubSubTestCase, self).setUp(*args, **kwargs)
     self.client2 = brukva.Client(io_loop=self.loop)
     self.client2.connection.connect()
     self.client2.select(9)
Exemplo n.º 20
0
 def set_source(self):
     cls = self.__class__
     if not cls._source:
         cls._source = brukva.Client()
         cls._source.connect()
Exemplo n.º 21
0
 def post(self):
     cursor = self.get_argument("cursor", None)
     self.client = brukva.Client()
     self.client.connect()
     self.client.subscribe('chat_channel')
     self.client.listen(self.on_message)
Exemplo n.º 22
0
import brukva
import tornado.httpserver
import tornado.web
import tornado.websocket
import tornado.ioloop
import redis

REDIS_HOST = 'localhost'
REDIS_PORT = 6379

r = redis.Redis(REDIS_HOST, REDIS_PORT, db=9)

c = brukva.Client(REDIS_HOST, REDIS_PORT)
c.connect()
c.select(9)

c.set('foo', 'bar')
c.set('foo2', 'bar2')


class BrukvaHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @brukva.adisp.process
    def get(self):
        foo, foo2 = yield [c. async .get('foo'), c. async .get('foo2')]
        self.set_header('Content-Type', 'text/plain')
        self.write(foo)
        self.write(foo2)
        self.finish()

Exemplo n.º 23
0
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from .models import ChatMessage
from quiz.models.models import Question
import json
import brukva
from chat.models import ChatRoom
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext as __
import threading
import time
from .utils import get_current_question, check_answer, get_answers
from quiz.views.api.utils import CsrfExemptSessionAuthentication

bclient = brukva.Client()
bclient.connect()


@api_view(['POST'])
@authentication_classes([CsrfExemptSessionAuthentication])
@permission_classes([])
def submit(request):
    input_data = json.loads(request.body)
    is_true = check_answer(input_data['message'])
    if is_true:
        input_data['message'] = input_data['message'] + get_answers()
    #print is_true
    #input_data['is_right']=check_answer(input_data['message'])
    input_data['is_right'] = is_true
    input_data['is_service'] = False
Exemplo n.º 24
0
 def client(cls):
     if not hasattr(cls, '_client'):
         cls._client = brukva.Client()
         cls._client.connect()
     return cls._client
Exemplo n.º 25
0
 def _init_redis_listener(self):
     self.redis_client = brukva.Client()
     self.redis_client.connect()
     self.redis_client.subscribe('%s#%s#' %
                                 (self.get_redis_app_name(), self.name))
     self.redis_client.listen(self.on_messages_published)
Exemplo n.º 26
0

# Create tornadio router
WSRouter = TornadioRouter(
    WebSocketHandler,
    dict(enabled_protocols=[
        'websocket', 'xhr-polling', 'jsonp-polling', 'htmlfile'
    ]))

# Create socket application
application = web.Application(
    WSRouter.apply_routes([(r"/step-(\d)+[/]?", StepPageHandler)]),
    flash_policy_port=843,
    flash_policy_file=os.path.join(ROOT, 'flashpolicy.xml'),
    template_path=os.path.join(ROOT, 'templates'),
    static_path=os.path.join(ROOT, 'static'),
    socket_io_port=8001,
    enable_pretty_logging=True)

if __name__ == '__main__':
    socketio_server = SocketServer(application, auto_start=False)

    STATS = ServerStats()

    redis = brukva.Client(host='localhost', port=6379, selected_db=0)
    redis.connect()
    redis.subscribe('events')
    redis.listen(broadcast_events)

    ioloop.IOLoop.instance().start()
Exemplo n.º 27
0
 def __init__(self, *args):
     super(QuizConnection, self).__init__(*args)
     self.client = brukva.Client()
     self.client.connect()
     self.user = None
     self.lesson = None
Exemplo n.º 28
0
import os
import sys

lib_path = os.path.realpath(
    os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'lib'))
if lib_path not in sys.path:
    sys.path[0:0] = [lib_path]

PORT = 9090

import redis
REDIS_SYNC = redis.StrictRedis(db=0)

import brukva
REDIS_ASYNC = brukva.Client(selected_db=0)

import celery
CELERY = celery.Celery('spider', broker='redis://localhost:6379/0')

STORE = os.path.realpath(
    os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'store'))

CONFIDENCE = 0.5
SCORE = 1000.0

try:
    from settings_local import *
except ImportError:
    pass
Exemplo n.º 29
0
from .helpers import (RedisSourceService, DataSourceService, TaskService,
                      TaskStatusEnum, TaskErrorCodeEnum)
from core.models import (Datasource, Dimension, Measure, QueueList,
                         DatasourceMeta, DatasourceMetaKeys,
                         DatasourceSettings, Dataset, DatasetToMeta, Cube)
from django.conf import settings

from djcelery import celery
from itertools import groupby, izip

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production"

client = brukva.Client(host=settings.REDIS_HOST,
                       port=int(settings.REDIS_PORT),
                       selected_db=settings.REDIS_DB)
client.connect()

logger = logging.getLogger(__name__)

ASCENDING = 1


class TaskProcessing(object):
    """
    Базовый класс, отвечающий за про процесс выполнения celery-задач

    Attributes:
        task_id(int): id задачи
        channel(str): Канал передачи на клиент