Administrator
2024-06-13 cbe19ea6066a600cbd0b5110db5d43f8252d14a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
同花顺l2监听位置健康度管理
"""
# 不健康
from db.redis_manager_delegate import RedisUtils
from utils import tool
from db import redis_manager_delegate as redis_manager
UN_HEALTHY = 0
UN_KNOWN = 1
HEALTHY = 2
 
__redisManager = redis_manager.RedisManager(0)
 
 
def __get_redis():
    return __redisManager.getRedis()
 
 
def __set_health_state(client_id, pos, state):
    RedisUtils.setex(__get_redis(), f"l2_pos_health_state-{client_id}-{pos}", tool.get_expire(), state)
 
 
def __get_health_state(client_id, pos):
    val = RedisUtils.get(__get_redis(), f"l2_pos_health_state-{client_id}-{pos}")
    if val is None:
        return UN_KNOWN
    return int(val)
 
 
# 初始化所有的健康度
def init_all(clients):
    for client_info in clients:
        init(client_info[0], client_info[1])
 
 
# 初始化健康度
def init(client_id, pos):
    __set_health_state(client_id, pos, UN_KNOWN)
 
 
# 设置不健康
def set_healthy(client_id, pos):
    __set_health_state(client_id, pos, HEALTHY)
 
 
# 设置不健康
def set_unhealthy(client_id, pos):
    # 如果是未知状态才能设置
    state = __get_health_state(client_id, pos)
    if state == UN_KNOWN:
        __set_health_state(client_id, pos, UN_HEALTHY)
 
 
# 查询所有的健康状态
def list_health_state(poses):
    dict_ = {}
    for client_info in poses:
        dict_[client_info] = __get_health_state(client_info[0], client_info[1])
    return dict_
 
 
if __name__ == '__main__':
    # init_all([(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6)])
    set_healthy(2, 0)
    print(list_health_state([(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6)]))
    set_unhealthy(2, 0)
    set_healthy(2, 0)