redis.py 1.38 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
4
5
6
7
8
9
10
import redis
import toml
import os

redis_config_path = os.path.join(os.path.dirname(__file__), "redis_config.toml")

REDIS_CONFIG = toml.load(redis_config_path)


class Redis(redis.Redis):
BO ZHANG's avatar
BO ZHANG committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    def __init__(self, location="naoc"):
        if location not in REDIS_CONFIG.keys():
            print("Available redis locations: ", list(REDIS_CONFIG.keys()))
            raise ValueError(f"Unknown redis location: {location}")
        super().__init__(
            host=REDIS_CONFIG[location]["host"],
            port=REDIS_CONFIG[location]["port"],
            db=REDIS_CONFIG[location]["db"],
            password=REDIS_CONFIG[location]["password"],
        )
        self.qname = password = REDIS_CONFIG[location]["qname"]
        self.config = REDIS_CONFIG[location]

        print("Setting redis config:")
        for k, v in self.config.items():
            print(f" - {k}: {v}")
BO ZHANG's avatar
BO ZHANG committed
27
28

    def push(self, msg):
BO ZHANG's avatar
BO ZHANG committed
29
        self.lpush(self.qname, msg)
BO ZHANG's avatar
BO ZHANG committed
30
31

    def pop(self):
BO ZHANG's avatar
BO ZHANG committed
32
        return self.rpop(self.qname)
BO ZHANG's avatar
BO ZHANG committed
33
34

    def get_all(self):
BO ZHANG's avatar
BO ZHANG committed
35
        return self.lrange(self.qname, 0, -1)
BO ZHANG's avatar
BO ZHANG committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


# msgs = r.lrange(name, 0, -1)
# for chipid in range(6, 26):
#     this_msg = gen_msg(
#         dag_id="csst-msc-l1-mbi", obsid="11009101682009", chipid=f"{chipid:02d}"
#     )
#     print(this_msg)
#     r.lpush(name, this_msg)
#
# msgs = r.lrange(name, 0, -1)
# print(msgs)
#
# msgs_later = r.lrange(name, 0, -1)
# print(msgs_later)