Commit b0888dd8 authored by Jalin's avatar Jalin

优化同步处理

parent 0b054997
...@@ -146,7 +146,7 @@ class Cluster(): ...@@ -146,7 +146,7 @@ class Cluster():
if not master: if not master:
if Config().NODE_SLAVE_CAN_BE_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.session.hset(self.KEY_NODES, slave, self.KEY_MASTER)
self.publish_log_message(ClusterLog.MESSAGE_ASCENDING_MASTER_NODE.format(slave, self.publish_log_message(ClusterLog.MESSAGE_ASCENDING_MASTER_NODE.format(slave,
ClusterLog.get_print_nodes( ClusterLog.get_print_nodes(
......
...@@ -118,8 +118,7 @@ class Config: ...@@ -118,8 +118,7 @@ class Config:
setattr(self, key, value) setattr(self, key, value)
if not first: if not first:
if key == 'USER_ACCOUNTS' and old != value: 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: elif key == 'QUERY_JOBS' and old != value:
Query().update_query_jobs(auto=True) # 任务修改 Query().update_query_jobs(auto=True) # 任务修改
elif key == 'QUERY_INTERVAL' and old != value: elif key == 'QUERY_INTERVAL' and old != value:
......
...@@ -35,13 +35,14 @@ class OCR: ...@@ -35,13 +35,14 @@ class OCR:
def get_image_position_by_offset(self, offsets): def get_image_position_by_offset(self, offsets):
positions = [] positions = []
width = 70 width = 75
height = 70 height = 75
random_num = random.randint(0, 8)
for offset in offsets: for offset in offsets:
random_x = random.randint(-5, 5)
random_y = random.randint(-5, 5)
offset = int(offset) offset = int(offset)
x = width * (offset % 5) - width / 2 + random_num x = width * ((offset - 1) % 4 + 1) - width / 2 + random_x
y = height * math.ceil(offset / 4) - height / 2 - random_num y = height * math.ceil(offset / 4) - height / 2 + random_y
positions.append(int(x)) positions.append(int(x))
positions.append(int(y)) positions.append(int(y))
return positions return positions
......
...@@ -60,7 +60,7 @@ class AuthCode: ...@@ -60,7 +60,7 @@ class AuthCode:
校验验证码 校验验证码
:return: :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) response = self.session.get(url)
result = response.json() result = response.json()
if result.get('result_code') == '4': if result.get('result_code') == '4':
......
...@@ -63,7 +63,7 @@ class BaseLog: ...@@ -63,7 +63,7 @@ class BaseLog:
if is_main_thread(): if is_main_thread():
self.logs = [] self.logs = []
else: 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 @classmethod
def add_quick_log(cls, content=''): def add_quick_log(cls, content=''):
......
...@@ -57,7 +57,7 @@ class UserLog(BaseLog): ...@@ -57,7 +57,7 @@ class UserLog(BaseLog):
@classmethod @classmethod
def print_start_login(cls, user): def print_start_login(cls, user):
self = cls() self = cls()
self.add_log('正在登录用户 {}'.format(user.user_name)) self.add_quick_log('正在登录用户 {}'.format(user.user_name))
self.flush() self.flush()
return self return self
......
...@@ -14,6 +14,7 @@ from py12306.log.user_log import UserLog ...@@ -14,6 +14,7 @@ from py12306.log.user_log import UserLog
class UserJob: class UserJob:
# heartbeat = 60 * 2 # 心跳保持时长 # heartbeat = 60 * 2 # 心跳保持时长
heartbeat_interval = 60 * 2 heartbeat_interval = 60 * 2
check_interval = 5
key = None key = None
user_name = '' user_name = ''
password = '' password = ''
...@@ -67,7 +68,7 @@ class UserJob: ...@@ -67,7 +68,7 @@ class UserJob:
if Config().is_master() and not self.cookie: self.load_user_from_remote() # 主节点加载一次 Cookie if Config().is_master() and not self.cookie: self.load_user_from_remote() # 主节点加载一次 Cookie
self.check_heartbeat() self.check_heartbeat()
if Const.IS_TEST: return if Const.IS_TEST: return
sleep(self.heartbeat_interval) sleep(self.check_interval)
def check_heartbeat(self): def check_heartbeat(self):
# 心跳检测 # 心跳检测
...@@ -75,7 +76,7 @@ class UserJob: ...@@ -75,7 +76,7 @@ class UserJob:
return True return True
# 只有主节点才能走到这 # 只有主节点才能走到这
if self.is_first_time() or not self.check_user_is_login(): 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 self.is_ready = True
message = UserLog.MESSAGE_USER_HEARTBEAT_NORMAL.format(self.get_name(), self.heartbeat_interval) message = UserLog.MESSAGE_USER_HEARTBEAT_NORMAL.format(self.get_name(), self.heartbeat_interval)
...@@ -130,6 +131,7 @@ class UserJob: ...@@ -130,6 +131,7 @@ class UserJob:
user_name = self.auth_uamauthclient(new_tk) user_name = self.auth_uamauthclient(new_tk)
self.update_user_info({'user_name': user_name}) self.update_user_info({'user_name': user_name})
self.login_did_success() self.login_did_success()
return True
elif result.get('result_code') == 2: # 账号之内错误 elif result.get('result_code') == 2: # 账号之内错误
# 登录失败,用户名或密码为空 # 登录失败,用户名或密码为空
# 密码输入错误 # 密码输入错误
...@@ -237,11 +239,13 @@ class UserJob: ...@@ -237,11 +239,13 @@ class UserJob:
UserLog.add_quick_log(UserLog.MESSAGE_USER_COOKIE_NOT_FOUND_FROM_REMOTE.format(self.user_name)).flush() UserLog.add_quick_log(UserLog.MESSAGE_USER_COOKIE_NOT_FOUND_FROM_REMOTE.format(self.user_name)).flush()
stay_second(self.retry_time) stay_second(self.retry_time)
return self.load_user_from_remote() return self.load_user_from_remote()
if cookie:
self.session.cookies.update(cookie) self.session.cookies.update(cookie)
if not self.cookie: # 第一次加载 if not self.cookie: # 第一次加载
self.cookie = True self.cookie = True
self.did_loaded_user() self.did_loaded_user()
return True return True
return False
def check_is_ready(self): def check_is_ready(self):
return self.is_ready return self.is_ready
......
...@@ -50,6 +50,7 @@ class User: ...@@ -50,6 +50,7 @@ class User:
def init_user(self, info): def init_user(self, info):
user = UserJob(info=info) user = UserJob(info=info)
self.users.append(user) self.users.append(user)
return user
def refresh_users(self, old): def refresh_users(self, old):
for account in self.user_accounts: for account in self.user_accounts:
...@@ -58,8 +59,10 @@ class User: ...@@ -58,8 +59,10 @@ class User:
if old_account and account != old_account: if old_account and account != old_account:
user = self.get_user(key) user = self.get_user(key)
user.init_data(account) user.init_data(account)
elif not old_account: elif not old_account: # 新用户 添加到 多线程
self.init_user(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: # 退出已删除的用户 for account in old: # 退出已删除的用户
if not array_dict_find_by_key_value(self.user_accounts, 'key', account.get('key')): if not array_dict_find_by_key_value(self.user_accounts, 'key', account.get('key')):
user = self.get_user(account.get('key')) user = self.get_user(account.get('key'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment