task_config.py 4.5 KB

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