This commit is contained in:
yumoqing 2025-11-04 17:31:41 +08:00
parent d3a8c44650
commit 3cb0dc22f2
2 changed files with 18 additions and 24 deletions

View File

@ -12,12 +12,17 @@ from appPublic.worker import schedule_once
class MyTasks(LongTasks): class MyTasks(LongTasks):
# Child class # Child class
async def process_task(self, payload): async def process_task(self, payload, workerid=None):
# use logic to execute task # use logic to execute task
pass pass
def load_longtasks() def load_longtasks()
longtasks = MyTasks('redis://127.0.0.1:6379', 'example') longtasks = MyTasks('redis://127.0.0.1:6379', 'example', worker_cnt=2, stuck_seconds=600, max_age_hours=3)
## arguments
## example: task name of this instance, each instance must has difference task name
## worker_cnt: backend job count
## stuck_seconds: tasks in processing_queue have waited longer than, will reenter the queue
## max_age_hours: task created longer the max_age_hours will deleted from the LongTasks instance
env = ServerEnv() env = ServerEnv()
env.longtasks = longtasks env.longtasks = longtasks
# run the backend job # run the backend job

View File

@ -7,12 +7,7 @@ import time
from typing import Any, Dict from typing import Any, Dict
from appPublic.worker import get_event_loop, schedule_interval, schedule_once from appPublic.worker import get_event_loop, schedule_interval, schedule_once
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
from appPublic.log import debug, exception
def work1(x, y=1):
print(f'{x} * {y} = {x*y}')
async def work2():
print('task2 ...')
class LongTasks: class LongTasks:
def __init__(self, redis_url, taskname, worker_cnt=2, stuck_seconds=600, max_age_hours=3): def __init__(self, redis_url, taskname, worker_cnt=2, stuck_seconds=600, max_age_hours=3):
@ -37,15 +32,15 @@ class LongTasks:
continue continue
if created + self.max_age_seconds < now: if created + self.max_age_seconds < now:
print(f"🧹 删除过期任务: {key}") debug(f"🧹 删除过期任务: {key}")
await self.redis.delete(key) await self.redis.delete(key)
await self.redis.lrem("task_queue", 0, key) # 从任务队列中移除(可选) await self.redis.lrem("task_queue", 0, key) # 从任务队列中移除(可选)
async def process_task(self, payload:dict): async def process_task(self, payload:dict, workid:int=None):
sec = randint(0,5) sec = randint(0,5)
await asyncio.sleep(sec) await asyncio.sleep(sec)
print(f'{payload=} done') debug(f'{payload=} done')
return { return {
'result': 'OK' 'result': 'OK'
} }
@ -91,7 +86,7 @@ class LongTasks:
# 这里我们选择重新入队并删除 processing entry # 这里我们选择重新入队并删除 processing entry
await self.redis.rpush(self.task_queue, item) await self.redis.rpush(self.task_queue, item)
await self.redis.lrem(self.processing_queue, 1, item) await self.redis.lrem(self.processing_queue, 1, item)
print(f"[recover] requeued missing-hash {task_id}") debug(f"[recover] requeued missing-hash {task_id}")
continue continue
started_at = float(info.get("started_at") or 0) started_at = float(info.get("started_at") or 0)
@ -104,11 +99,11 @@ class LongTasks:
await self.update_task_hash(task_id, {"status": "PENDING", "attempts": attempts}) await self.update_task_hash(task_id, {"status": "PENDING", "attempts": attempts})
await self.redis.rpush(self.task_queue, item) await self.redis.rpush(self.task_queue, item)
await self.redis.lrem(self.processing_queue, 1, item) await self.redis.lrem(self.processing_queue, 1, item)
print(f"[recover] task {task_id} requeued due to stuck") debug(f"[recover] task {task_id} requeued due to stuck")
# else: 正常 running 或其他状态,不处理 # else: 正常 running 或其他状态,不处理
async def worker_loop(self, worker_id: int): async def worker_loop(self, worker_id: int):
print(f"[worker {worker_id}] start") debug(f"[worker {worker_id}] start")
while True: while True:
try: try:
# BRPOPLPUSH: 从 task_queue 弹出(阻塞),并 push 到 processing_queue原子 # BRPOPLPUSH: 从 task_queue 弹出(阻塞),并 push 到 processing_queue原子
@ -124,7 +119,7 @@ class LongTasks:
task_id = task_obj["task_id"] task_id = task_obj["task_id"]
payload = task_obj["payload"] payload = task_obj["payload"]
except Exception as e: except Exception as e:
print(f"[worker {worker_id}] bad item in queue, removing: {e}") exception(f"[worker {worker_id}] bad item in queue, removing: {e}")
# 异常数据从 processing_queue 中移除 # 异常数据从 processing_queue 中移除
await self.redis.lrem(self.processing_queue, 1, item) await self.redis.lrem(self.processing_queue, 1, item)
continue continue
@ -143,7 +138,7 @@ class LongTasks:
# 2) 执行任务catch exceptions # 2) 执行任务catch exceptions
try: try:
result = await self.process_task(payload) result = await self.process_task(worker_id, payload)
except asyncio.CancelledError: except asyncio.CancelledError:
# 若希望支持取消,可把 status 设为 cancelling 等 # 若希望支持取消,可把 status 设为 cancelling 等
await self.update_task_hash(task_id, {"status": "FAILED", "error": "cancelled"}) await self.update_task_hash(task_id, {"status": "FAILED", "error": "cancelled"})
@ -169,12 +164,12 @@ class LongTasks:
}) })
# 最后一步:从 processing_queue 中移除任务项LREM # 最后一步:从 processing_queue 中移除任务项LREM
await self.redis.lrem(self.processing_queue, 1, item) await self.redis.lrem(self.processing_queue, 1, item)
print(f"[worker {worker_id}] finished {task_id}") debug(f"[worker {worker_id}] finished {task_id}")
except asyncio.CancelledError: except asyncio.CancelledError:
break break
except Exception as e: except Exception as e:
print(f"[worker {worker_id}] loop error: {e}") exception(f"[worker {worker_id}] loop error: {e}")
await asyncio.sleep(1) await asyncio.sleep(1)
async def submit_task(self, payload): async def submit_task(self, payload):
@ -208,12 +203,6 @@ class LongTasks:
if __name__ == '__main__': if __name__ == '__main__':
async def main(lt): async def main(lt):
x = schedule_interval(5, work1, 6)
print(f'interval worker {x}')
y = schedule_interval(3, work2)
print(f'interval worker {y}')
await asyncio.sleep(10000)
return
tasks = [] tasks = []
for i in range(0, 10): for i in range(0, 10):
payload = { payload = {