def from_settings(cls, settings):
        kwargs = {
            'persist': settings.getbool('SCHEDULER_PERSIST'),
            'flush_on_start': settings.getbool('SCHEDULER_FLUSH_ON_START'),
            'idle_before_close':
            settings.getint('SCHEDULER_IDLE_BEFORE_CLOSE'),
        }

        # If these values are missing, it means we want to use the defaults.
        optional = {
            # TODO: Use custom prefixes for this settings to note that are
            # specific to scrapy-redis.
            'queue_key': 'SCHEDULER_QUEUE_KEY',
            'queue_cls': 'SCHEDULER_QUEUE_CLASS',
            'dupefilter_key': 'SCHEDULER_DUPEFILTER_KEY',
            # We use the default setting name to keep compatibility.
            'dupefilter_cls': 'DUPEFILTER_CLASS',
            'serializer': 'SCHEDULER_SERIALIZER',
        }
        for name, setting_name in optional.items():
            val = settings.get(setting_name)
            if val:
                kwargs[name] = val

        # Support serializer as a path to a module.
        if isinstance(kwargs.get('serializer'), six.string_types):
            kwargs['serializer'] = importlib.import_module(
                kwargs['serializer'])

        server = Client(host='localhost', port=6379, decode_responses=True)
        # Ensure the connection is working.
        server.ping()

        return cls(server=server, **kwargs)
示例#2
0
def webhook_save():
    if request.method == 'POST':
        timestamp_human = datetime.datetime.now()
        timestamp = int(time.time())
        nowDatetime = timestamp_human.strftime('%Y-%m-%d(%H:%M:%S)')
        req_data = request.get_json()
        alertname = req_data['commonLabels']['alertname']
        severity = ''
        receiver = req_data['receiver']
        key_name = str(timestamp) + "_" + alertname + "_" + receiver
        try:
            # conn = redis.Redis(host=REDIS_SERVER, port=6379, db=0, password=REDIS_PWD)
            conn = Client(host=REDIS_SERVER,
                          port=6379,
                          db=0,
                          password=REDIS_PWD)
            conn.ping()
            print
            'Redis connected %s' % (REDIS_SERVER)
        except Exception as e:
            print
            'Error:', e
            exit('Failed to connecting')

        conn = Client(host=REDIS_SERVER, port=6379)
        conn.jsonset(key_name, Path.rootPath(), req_data)
        data = json.dumps(conn.jsonget(key_name))
        print
        data
        # Redis : SCAN 0 match 1527911[1-9][1-9]*

    else:
        abort(400)

    if not conn.exists(key_name):
        print
        "Error: %s is doesn't exist" % (key_name)

    return jsonify({'status': 'success'}), 200
示例#3
0
class Database:
    """Database handler"""
    def __init__(self, database_name, port=6379):
        self.database = Client(host=database_name,
                               port=port,
                               decode_responses=True)

    def flush(self):
        """Flushing a certain table of the database"""
        return self.database.flushdb()

    def import_data(self, keyname, data):
        """Load data into the database"""
        return self.database.jsonset(keyname, Path.rootPath(), data)

    def ping(self):
        """Tests the connection"""
        return self.database.ping()