task_config.py 4.6 KB

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