Exemplo n.º 1
0
def init_db(redis: NewparpRedis):
    print("Attempting to init database.")

    lock = str(uuid.uuid4())
    redis.setex("lock:initdb", 60, lock)

    time.sleep(random.uniform(1, 2))

    if redis.get("lock:initdb") == lock:
        print("Got the init lock, initating database.")
        subprocess.call(["python3", os.path.dirname(os.path.realpath(__file__)) + "/../newparp/model/init_db.py"])
    else:
        print("Didn't get the init lock, waiting 5 seconds.")
        time.sleep(5)
Exemplo n.º 2
0
def get_ip_banned(ip_address: str,
                  db: Session,
                  redis: NewparpRedis,
                  use_cache: bool = True) -> bool:
    cached_bans = redis.get("bans:%s" % (ip_address))
    if cached_bans:
        try:
            return int(cached_bans) > 0
        except (ValueError, TypeError):
            pass

    ip_bans = db.query(func.count('*')).select_from(IPBan).filter(
        IPBan.address.op(">>=")(ip_address)).scalar()
    banned = ip_bans > 0
    redis.setex("bans:%s" % (ip_address), 60, ip_bans)

    return banned
Exemplo n.º 3
0
def init_db(redis: NewparpRedis):
    print("Attempting to init database.")

    lock = str(uuid.uuid4())
    redis.setex("lock:initdb", 60, lock)

    time.sleep(random.uniform(1, 2))

    if redis.get("lock:initdb") == lock:
        print("Got the init lock, initating database.")
        subprocess.call([
            "python3",
            os.path.dirname(os.path.realpath(__file__)) +
            "/../newparp/model/init_db.py"
        ])
    else:
        print("Didn't get the init lock, waiting 5 seconds.")
        time.sleep(5)