更新任务获取接口
This commit is contained in:
51
main.py
51
main.py
@@ -3,60 +3,65 @@ from spider.task import *
|
|||||||
from logger import error_logger, record_full_log
|
from logger import error_logger, record_full_log
|
||||||
|
|
||||||
TASK_TYPE = {
|
TASK_TYPE = {
|
||||||
0: post,
|
'get_account_profile': playwright_get_user_profile,
|
||||||
|
'check_account_cookies': playwright_check_account_cookies,
|
||||||
|
'post': playwright_post
|
||||||
}
|
}
|
||||||
|
|
||||||
HOST = "http://118.193.40.152:8091"
|
HOST = "http://192.168.1.69:8001"
|
||||||
|
|
||||||
|
|
||||||
def get_task(platform):
|
def get_task():
|
||||||
# if lock._block.locked():
|
if lock._block.locked():
|
||||||
# return
|
return
|
||||||
response = requests.get(f'{HOST}/services/task', json={'platform': platform})
|
url = f'{HOST}/queue/get-data'
|
||||||
|
header = {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
response = requests.post(url, headers=header)
|
||||||
|
if response.status_code == 200:
|
||||||
result = response.json()
|
result = response.json()
|
||||||
if result['code'] == 0:
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def task_callback(task_type, queue_id, data, status=1, err_msg=''):
|
def task_callback(tid, data, status='success', msg='success'):
|
||||||
response = requests.post(
|
body = {
|
||||||
f'{HOST}/services/task/callback',
|
'id': tid,
|
||||||
json={
|
|
||||||
'task_type': task_type,
|
|
||||||
'queue_id': queue_id,
|
|
||||||
'status': status,
|
'status': status,
|
||||||
'data': data,
|
'data': data,
|
||||||
'err_msg': err_msg,
|
'message': msg,
|
||||||
}
|
}
|
||||||
|
response = requests.post(
|
||||||
|
f'{HOST}/queue/handle-data',
|
||||||
|
json=body
|
||||||
)
|
)
|
||||||
result = response.json()
|
result = response.json()
|
||||||
if result['code'] != 0:
|
if response.status_code != 200:
|
||||||
raise RuntimeError(f"任务回调失败:{result['msg']}")
|
raise RuntimeError(f"任务回调失败:{result['msg']}")
|
||||||
|
|
||||||
|
|
||||||
def execute_task(queue_id, task_type, **kwargs):
|
def execute_task(tid, task_type, **kwargs):
|
||||||
try:
|
try:
|
||||||
result = TASK_TYPE.get(task_type)(**kwargs)
|
result = TASK_TYPE.get(task_type)(**kwargs)
|
||||||
task_callback(task_type, queue_id, data=result)
|
task_callback(tid, data=result)
|
||||||
except AuthException as e:
|
except AuthException as e:
|
||||||
record_full_log(error_logger, e)
|
record_full_log(error_logger, e)
|
||||||
task_callback(task_type, queue_id, data={}, status=4, err_msg=str(e))
|
task_callback(tid, data={}, status='fail', msg=str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
record_full_log(error_logger, e)
|
record_full_log(error_logger, e)
|
||||||
task_callback(task_type, queue_id, data={}, status=0, err_msg=str(e))
|
task_callback(tid, data={}, status='fail', msg=str(e))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
with ThreadPoolExecutor(max_workers=1) as t:
|
with ThreadPoolExecutor(max_workers=1) as t:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
task = get_task(0)
|
task = get_task()
|
||||||
if task is None:
|
if task is None:
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
continue
|
continue
|
||||||
|
task['data']['tid'] = task['id']
|
||||||
task['data']['queue_id'] = task['queue_id']
|
task['data']['task_type'] = task['taskType']
|
||||||
task['data']['task_type'] = task['task_type']
|
|
||||||
t.submit(execute_task, **task['data'])
|
t.submit(execute_task, **task['data'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_logger.error(f'Main Error: {e}')
|
error_logger.error(f'Main Error: {e}')
|
||||||
|
|||||||
Reference in New Issue
Block a user