task_config.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. from dataclasses import dataclass
  2. @dataclass
  3. class TaskConfig:
  4. """任务配置"""
  5. timeout: int # 超时时间(秒)
  6. max_concurrent: int = 5 # 最大并发数
  7. retry_times: int = 0 # 重试次数
  8. retryable: bool = True # 是否可重试
  9. alert_on_failure: bool = True # 失败时是否告警
  10. cancel_check_interval: int = 5 # 取消检测轮询间隔(秒)
  11. class TaskStatus:
  12. """任务状态常量"""
  13. INIT = 0
  14. PROCESSING = 1
  15. SUCCESS = 2
  16. CANCELLING = 3 # 待取消(用户请求取消)
  17. CANCELLED = 4 # 已取消(协程已停止)
  18. FAILED = 99
  19. class TaskConstants:
  20. """任务系统常量"""
  21. # 默认配置
  22. DEFAULT_TIMEOUT = 1800
  23. DEFAULT_MAX_CONCURRENT = 5
  24. DEFAULT_RETRY_TIMES = 0
  25. # 数据库表名
  26. TASK_TABLE = "long_articles_task_manager"
  27. # 所有任务的配置映射
  28. TASK_CONFIGS = {
  29. # 监控类任务
  30. "check_kimi_balance": TaskConfig(
  31. timeout=20,
  32. max_concurrent=1,
  33. retry_times=3,
  34. ),
  35. "get_off_videos": TaskConfig(
  36. timeout=1800,
  37. max_concurrent=3,
  38. ),
  39. "check_publish_video_audit_status": TaskConfig(
  40. timeout=1800,
  41. max_concurrent=3,
  42. ),
  43. "outside_article_monitor": TaskConfig(
  44. timeout=3 * 3600,
  45. max_concurrent=2,
  46. ),
  47. "inner_article_monitor": TaskConfig(
  48. timeout=3600,
  49. max_concurrent=3,
  50. ),
  51. "task_processing_monitor": TaskConfig(
  52. timeout=300,
  53. max_concurrent=1,
  54. ),
  55. # 爬虫类任务
  56. "crawler_toutiao": TaskConfig(
  57. timeout=5 * 3600,
  58. max_concurrent=3,
  59. retry_times=2,
  60. ),
  61. "crawler_gzh_articles": TaskConfig(
  62. timeout=4 * 3600,
  63. max_concurrent=3,
  64. retry_times=2,
  65. ),
  66. "crawler_account_manager": TaskConfig(
  67. timeout=1800,
  68. max_concurrent=5,
  69. ),
  70. "crawler_detail_analysis": TaskConfig(
  71. timeout=3600,
  72. max_concurrent=3,
  73. ),
  74. # 数据处理类任务
  75. "daily_publish_articles_recycle": TaskConfig(
  76. timeout=3600,
  77. max_concurrent=2,
  78. ),
  79. "update_root_source_id": TaskConfig(
  80. timeout=3600,
  81. max_concurrent=2,
  82. ),
  83. "recycle_outside_account_articles": TaskConfig(
  84. timeout=3600,
  85. max_concurrent=2,
  86. ),
  87. "update_outside_account_article_root_source_id": TaskConfig(
  88. timeout=3600,
  89. max_concurrent=2,
  90. ),
  91. "fwh_daily_recycle": TaskConfig(
  92. timeout=3600,
  93. max_concurrent=2,
  94. ),
  95. # 算法类任务
  96. "article_pool_cold_start": TaskConfig(
  97. timeout=4 * 3600,
  98. max_concurrent=2,
  99. retry_times=1,
  100. ),
  101. "candidate_account_quality_analysis": TaskConfig(
  102. timeout=3600,
  103. max_concurrent=3,
  104. ),
  105. "article_pool_category_generation": TaskConfig(
  106. timeout=3600,
  107. max_concurrent=3,
  108. ),
  109. "account_category_analysis": TaskConfig(
  110. timeout=3600,
  111. max_concurrent=3,
  112. ),
  113. "update_limited_account_info": TaskConfig(
  114. timeout=3600,
  115. max_concurrent=2,
  116. ),
  117. # LLM 类任务
  118. "title_rewrite": TaskConfig(
  119. timeout=1800,
  120. max_concurrent=3,
  121. retry_times=2,
  122. ),
  123. "extract_title_features": TaskConfig(
  124. timeout=1800,
  125. max_concurrent=3,
  126. retry_times=2,
  127. ),
  128. # 统计分析类任务
  129. "update_account_read_rate_avg": TaskConfig(
  130. timeout=1800,
  131. max_concurrent=3,
  132. ),
  133. "update_account_read_avg": TaskConfig(
  134. timeout=1800,
  135. max_concurrent=3,
  136. ),
  137. "update_account_open_rate_avg": TaskConfig(
  138. timeout=1800,
  139. max_concurrent=3,
  140. ),
  141. # 自动化任务
  142. "auto_follow_account": TaskConfig(
  143. timeout=1800,
  144. max_concurrent=2,
  145. ),
  146. "get_follow_result": TaskConfig(
  147. timeout=1800,
  148. max_concurrent=2,
  149. ),
  150. "extract_reply_result": TaskConfig(
  151. timeout=1800,
  152. max_concurrent=2,
  153. ),
  154. # 合作账号任务
  155. "cooperate_accounts_monitor": TaskConfig(
  156. timeout=7200,
  157. max_concurrent=2,
  158. ),
  159. "cooperate_accounts_detail": TaskConfig(
  160. timeout=3600,
  161. max_concurrent=2,
  162. ),
  163. # 其他任务
  164. "mini_program_detail_process": TaskConfig(
  165. timeout=1800,
  166. max_concurrent=3,
  167. ),
  168. }
  169. def get_task_config(task_name: str) -> TaskConfig:
  170. """获取任务配置,如果不存在则返回默认配置"""
  171. return TASK_CONFIGS.get(
  172. task_name,
  173. TaskConfig(
  174. timeout=TaskConstants.DEFAULT_TIMEOUT,
  175. max_concurrent=TaskConstants.DEFAULT_MAX_CONCURRENT,
  176. retry_times=TaskConstants.DEFAULT_RETRY_TIMES,
  177. ),
  178. )
  179. __all__ = [
  180. "TaskConfig",
  181. "TaskStatus",
  182. "TaskConstants",
  183. "TASK_CONFIGS",
  184. "get_task_config",
  185. ]