优化任务处理逻辑,修正任务函数以确保正确执行 docker_pull、docker_tag 和 docker_push 操作

This commit is contained in:
远野千束 2025-04-17 21:27:42 +08:00
parent 2a9d0baa6a
commit be652dfadd

41
sync.py
View File

@ -62,25 +62,44 @@ async def main():
for image in config.images:
if len(image.tags) > 0:
for tag in image.tags:
async def task() -> int | None:
return (await docker_pull(f"{image.source}:{tag}")) \
or (await docker_pull(f"{image.source}:{tag}")) \
or (await docker_push(f"{image.target}:{tag}"))
# 定义任务函数
async def task():
if r := await docker_pull(f"{image.source}:{tag}"):
if r != 0:
return r
await docker_tag(f"{image.source}:{tag}", f"{image.target}:{tag}")
if r := await docker_push(f"{image.target}:{tag}"):
if r != 0:
return r
await docker_push(f"{image.target}:{tag}")
if r := await docker_push(image.target):
if r != 0:
return r
return 0
# 将协程对象添加到任务列表
tasks.append(limited_task(task))
else:
async def task() -> int | None:
return (await docker_pull(image.source)) \
or (await docker_pull(image.source)) \
or (await docker_push(image.target))
async def task():
if r := await docker_pull(image.source):
if r != 0:
return r
if r:= await docker_tag(image.source, image.target):
if r != 0:
return r
if r := await docker_push(image.target):
if r != 0:
return r
return 0
tasks.append(limited_task(task))
results = await asyncio.gather(*tasks)
print(tasks)
failed_tasks = 0
for result in results:
if result is not None and result != 0:
failed_tasks += 1
if failed_tasks > 0:
raise Exception(f"{failed_tasks} tasks failed.")
print(f"{len(results)} tasks completed. {len(results) - failed_tasks} succeed, {failed_tasks} failed.")
if __name__ == "__main__":