优化同步处理

This commit is contained in:
Jalin
2019-01-09 22:20:54 +08:00
parent 0b054997c5
commit b0888dd8c6
8 changed files with 27 additions and 20 deletions

View File

@@ -146,7 +146,7 @@ class Cluster():
if not master:
if Config().NODE_SLAVE_CAN_BE_MASTER:
# 提升子节点为主节点
slave = list(self.nodes)[-1]
slave = list(self.nodes)[0]
self.session.hset(self.KEY_NODES, slave, self.KEY_MASTER)
self.publish_log_message(ClusterLog.MESSAGE_ASCENDING_MASTER_NODE.format(slave,
ClusterLog.get_print_nodes(

View File

@@ -118,8 +118,7 @@ class Config:
setattr(self, key, value)
if not first:
if key == 'USER_ACCOUNTS' and old != value:
# 用户修改 print('用户修改了')
User.update_user_accounts(auto=True, old=old)
User().update_user_accounts(auto=True, old=old)
elif key == 'QUERY_JOBS' and old != value:
Query().update_query_jobs(auto=True) # 任务修改
elif key == 'QUERY_INTERVAL' and old != value:

View File

@@ -35,13 +35,14 @@ class OCR:
def get_image_position_by_offset(self, offsets):
positions = []
width = 70
height = 70
random_num = random.randint(0, 8)
width = 75
height = 75
for offset in offsets:
random_x = random.randint(-5, 5)
random_y = random.randint(-5, 5)
offset = int(offset)
x = width * (offset % 5) - width / 2 + random_num
y = height * math.ceil(offset / 4) - height / 2 - random_num
x = width * ((offset - 1) % 4 + 1) - width / 2 + random_x
y = height * math.ceil(offset / 4) - height / 2 + random_y
positions.append(int(x))
positions.append(int(y))
return positions

View File

@@ -60,7 +60,7 @@ class AuthCode:
校验验证码
:return:
"""
url = API_AUTH_CODE_CHECK.get('url').format(answer=answer, random=random.random())
url = API_AUTH_CODE_CHECK.get('url').format(answer=answer, random=time_int())
response = self.session.get(url)
result = response.json()
if result.get('result_code') == '4':

View File

@@ -63,7 +63,7 @@ class BaseLog:
if is_main_thread():
self.logs = []
else:
if logs: del self.thread_logs[current_thread_id()]
if logs and self.thread_logs.get(current_thread_id()): del self.thread_logs[current_thread_id()]
@classmethod
def add_quick_log(cls, content=''):

View File

@@ -57,7 +57,7 @@ class UserLog(BaseLog):
@classmethod
def print_start_login(cls, user):
self = cls()
self.add_log('正在登录用户 {}'.format(user.user_name))
self.add_quick_log('正在登录用户 {}'.format(user.user_name))
self.flush()
return self

View File

@@ -14,6 +14,7 @@ from py12306.log.user_log import UserLog
class UserJob:
# heartbeat = 60 * 2 # 心跳保持时长
heartbeat_interval = 60 * 2
check_interval = 5
key = None
user_name = ''
password = ''
@@ -67,7 +68,7 @@ class UserJob:
if Config().is_master() and not self.cookie: self.load_user_from_remote() # 主节点加载一次 Cookie
self.check_heartbeat()
if Const.IS_TEST: return
sleep(self.heartbeat_interval)
sleep(self.check_interval)
def check_heartbeat(self):
# 心跳检测
@@ -75,7 +76,7 @@ class UserJob:
return True
# 只有主节点才能走到这
if self.is_first_time() or not self.check_user_is_login():
self.handle_login()
if not self.handle_login(): return
self.is_ready = True
message = UserLog.MESSAGE_USER_HEARTBEAT_NORMAL.format(self.get_name(), self.heartbeat_interval)
@@ -130,6 +131,7 @@ class UserJob:
user_name = self.auth_uamauthclient(new_tk)
self.update_user_info({'user_name': user_name})
self.login_did_success()
return True
elif result.get('result_code') == 2: # 账号之内错误
# 登录失败,用户名或密码为空
# 密码输入错误
@@ -237,11 +239,13 @@ class UserJob:
UserLog.add_quick_log(UserLog.MESSAGE_USER_COOKIE_NOT_FOUND_FROM_REMOTE.format(self.user_name)).flush()
stay_second(self.retry_time)
return self.load_user_from_remote()
self.session.cookies.update(cookie)
if not self.cookie: # 第一次加载
self.cookie = True
self.did_loaded_user()
return True
if cookie:
self.session.cookies.update(cookie)
if not self.cookie: # 第一次加载
self.cookie = True
self.did_loaded_user()
return True
return False
def check_is_ready(self):
return self.is_ready

View File

@@ -50,6 +50,7 @@ class User:
def init_user(self, info):
user = UserJob(info=info)
self.users.append(user)
return user
def refresh_users(self, old):
for account in self.user_accounts:
@@ -58,8 +59,10 @@ class User:
if old_account and account != old_account:
user = self.get_user(key)
user.init_data(account)
elif not old_account:
self.init_user(account)
elif not old_account: # 新用户 添加到 多线程
new_user = self.init_user(account)
create_thread_and_run(jobs=new_user, callback_name='run', wait=Const.IS_TEST)
for account in old: # 退出已删除的用户
if not array_dict_find_by_key_value(self.user_accounts, 'key', account.get('key')):
user = self.get_user(account.get('key'))