#!/usr/bin/env python3 """ Example usage of the queue system. """ import sys import time from pathlib import Path # 添加项目根目录到Python路径 project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root)) from task_queue.manager import queue_manager from task_queue.tasks import process_file_async, process_multiple_files_async def example_single_file(): """示例:处理单个文件""" print("=== 示例:处理单个文件 ===") project_id = "test_project" file_path = "public/test_document.txt" # 将文件加入队列 task_id = queue_manager.enqueue_file( project_id=project_id, file_path=file_path, original_filename="example_document.txt" ) print(f"任务已提交,任务ID: {task_id}") # 检查任务状态 time.sleep(2) status = queue_manager.get_task_status(task_id) print(f"任务状态: {status}") def example_multiple_files(): """示例:批量处理文件""" print("\n=== 示例:批量处理文件 ===") project_id = "test_project_batch" file_paths = [ "public/test_document.txt", "public/goods.xlsx" # 假设这个文件存在 ] original_filenames = [ "batch_document_1.txt", "batch_goods.xlsx" ] # 将多个文件加入队列 task_ids = queue_manager.enqueue_multiple_files( project_id=project_id, file_paths=file_paths, original_filenames=original_filenames ) print(f"批量任务已提交,任务ID: {task_ids}") def example_zip_file(): """示例:处理zip文件""" print("\n=== 示例:处理zip文件 ===") project_id = "test_project_zip" zip_path = "public/all_hp_product_spec_book2506.zip" # 将zip文件加入队列 task_id = queue_manager.enqueue_zip_file( project_id=project_id, zip_path=zip_path ) print(f"zip任务已提交,任务ID: {task_id}") def example_queue_stats(): """示例:获取队列统计信息""" print("\n=== 示例:队列统计信息 ===") stats = queue_manager.get_queue_stats() print(f"队列统计信息:") for key, value in stats.items(): if key != "recent_tasks": print(f" {key}: {value}") def example_cleanup(): """示例:清理任务""" print("\n=== 示例:清理任务 ===") project_id = "test_project" # 将清理任务加入队列(延迟10秒执行) task_id = queue_manager.enqueue_cleanup_task( project_id=project_id, older_than_days=1, # 清理1天前的文件 delay=10 ) print(f"清理任务已提交,任务ID: {task_id}") def main(): """主函数""" print("队列系统使用示例") print("=" * 50) try: # 运行示例 example_single_file() example_multiple_files() example_zip_file() example_queue_stats() example_cleanup() print("\n" + "=" * 50) print("示例运行完成!") print("\n要查看任务执行情况,请运行:") print("python queue/consumer.py --check") print("\n要启动队列消费者,请运行:") print("python queue/consumer.py") except Exception as e: print(f"运行示例时发生错误: {str(e)}") if __name__ == "__main__": main()