booker_order.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. import os
  2. import time
  3. import json
  4. import threading
  5. import random
  6. import redis
  7. from typing import List, Dict, Callable, Any, Optional
  8. from vs_types import GroupConfig, VSPlgConfig, Task, VSQueryResult, AppointmentType
  9. from vs_plg_factory import VSPlgFactory
  10. from toolkit.thread_pool import ThreadPool
  11. from toolkit.vs_cloud_api import VSCloudApi
  12. from toolkit.backoff import ExponentialBackoff
  13. class OrderBookerGCO:
  14. """
  15. 绑定模式 (订单自带账号):
  16. - 按城市队列维护热机配额。
  17. - 绝对的 1 对 1 关系:一个实例绑定一个云端订单。
  18. - 预订成功后,实例立即销毁。
  19. """
  20. def __init__(self, cfg: GroupConfig, redis_conf: Dict, logger: Callable[[str], None] = None):
  21. self.m_cfg = cfg
  22. self.m_factory = VSPlgFactory()
  23. self.m_logger = logger
  24. self.m_tasks: List[Task] = []
  25. self.m_lock = threading.RLock()
  26. self.m_stop_event = threading.Event()
  27. self.redis_com = redis.Redis(**redis_conf)
  28. self.redis_sub = redis.Redis(**redis_conf)
  29. self.m_pending_order_by_queue: Dict[str, int] = {}
  30. self.m_last_spawn_times: Dict[str, float] = {}
  31. self.m_task_data_cache: Dict[str, dict] = {}
  32. self.m_tracker_key = f"vs:worker:tasks_tracker:{self.m_cfg.identifier}"
  33. self.queue_backoff = ExponentialBackoff(base_delay=1*60.0, max_delay=10*60.0, factor=2.0)
  34. self.account_backoff = ExponentialBackoff(base_delay=5*60.0, max_delay=2*60*60.0, factor=2.0)
  35. self.task_backoff = ExponentialBackoff(base_delay=10, max_delay=30*60.0, factor=2.0)
  36. self.heartbeat_ttl = 2*60.0
  37. def _log(self, message):
  38. if self.m_logger:
  39. self.m_logger(f'[ORDER-BOOKER] [{self.m_cfg.identifier}] {message}')
  40. def start(self):
  41. if not self.m_cfg.enable:
  42. return
  43. self._log("Starting Order Booker...")
  44. plugin_name = self.m_cfg.plugin_config.plugin_name
  45. class_name = "".join(part.title() for part in plugin_name.split('_'))
  46. plugin_path = os.path.join(self.m_cfg.plugin_config.lib_path, self.m_cfg.plugin_config.plugin_bin)
  47. self.m_factory.register_plugin(plugin_name, plugin_path, class_name)
  48. threading.Thread(target=self._booking_trigger_loop, daemon=True).start()
  49. threading.Thread(target=self._creator_loop, daemon=True).start()
  50. threading.Thread(target=self._maintain_loop, daemon=True).start()
  51. threading.Thread(target=self._cache_refresh_loop, daemon=True).start()
  52. def stop(self):
  53. self._log("Stopping Booker...")
  54. self.m_stop_event.set()
  55. self._cleanup_all_tasks("booker stop")
  56. def _cleanup_task(self, task: Task, reason: str = ""):
  57. try:
  58. instance = getattr(task, 'instance', None)
  59. if instance and hasattr(instance, 'cleanup'):
  60. instance.cleanup()
  61. self._log(f"🧹 Cleaned up instance for task={getattr(task, 'task_ref', None)}. Reason: {reason}")
  62. except Exception as e:
  63. self._log(f"Cleanup failed for task={getattr(task, 'task_ref', None)}. Reason: {reason}. Error: {e}")
  64. def _remove_task(self, task: Task, reason: str = "", cleanup: bool = True):
  65. removed = False
  66. with self.m_lock:
  67. if task in self.m_tasks:
  68. self.m_tasks.remove(task)
  69. removed = True
  70. task_id = str(getattr(task, 'task_ref', ''))
  71. self.m_task_data_cache.pop(task_id, None)
  72. if cleanup and removed:
  73. self._cleanup_task(task, reason)
  74. return removed
  75. def _cleanup_all_tasks(self, reason: str = ""):
  76. with self.m_lock:
  77. tasks = list(self.m_tasks)
  78. self.m_tasks.clear()
  79. self.m_task_data_cache.clear()
  80. for task in tasks:
  81. self._cleanup_task(task, reason)
  82. def _get_redis_key(self, routing_key: str) -> str:
  83. return f"vs:signal:{routing_key}"
  84. def _maintain_loop(self):
  85. self._log("Maintain loop started.")
  86. heartbeat_interval = 60
  87. while not self.m_stop_event.is_set():
  88. for _ in range(heartbeat_interval):
  89. if self.m_stop_event.is_set():
  90. return
  91. time.sleep(1.0)
  92. with self.m_lock:
  93. tasks_to_check = list(self.m_tasks)
  94. if not tasks_to_check:
  95. continue
  96. healthy_tasks = []
  97. dead_tasks = []
  98. now = time.time()
  99. for t in tasks_to_check:
  100. if now >= t.next_remote_ping:
  101. try:
  102. t.instance.keep_alive()
  103. if t.instance.health_check():
  104. healthy_tasks.append(t)
  105. next_delay = random.randint(180, 300)
  106. t.next_remote_ping = now + next_delay
  107. self._log(f"🛡️ Task={t.task_ref} keep-alive success. Next ping in {next_delay}s.")
  108. else:
  109. dead_tasks.append(t)
  110. self._log(f"♻️ Instance for task={t.task_ref} unhealthy.")
  111. except Exception as e:
  112. dead_tasks.append(t)
  113. self._log(f"♻️ Instance for task={t.task_ref} keep-alive failed: {e}.")
  114. else:
  115. healthy_tasks.append(t)
  116. if healthy_tasks:
  117. try:
  118. pipeline = self.redis_com.pipeline()
  119. new_deadline = time.time() + self.heartbeat_ttl
  120. for t in healthy_tasks:
  121. if t.task_ref is not None:
  122. pipeline.zadd(self.m_tracker_key, {str(t.task_ref): new_deadline})
  123. pipeline.execute()
  124. self._log(f"💓 Heartbeat sent. Renewed {len(healthy_tasks)} tasks.")
  125. except Exception as e:
  126. self._log(f"Redis Heartbeat update failed: {e}")
  127. if dead_tasks:
  128. try:
  129. pipeline = self.redis_com.pipeline()
  130. for t in dead_tasks:
  131. if t.task_ref is not None:
  132. pipeline.zadd(self.m_tracker_key, {str(t.task_ref): 0})
  133. pipeline.execute()
  134. self._log(f"🗑️ Handed over {len(dead_tasks)} dead tasks to Sweeper.")
  135. except Exception as e:
  136. pass
  137. if dead_tasks:
  138. with self.m_lock:
  139. current_tasks = list(self.m_tasks)
  140. self.m_tasks = [t for t in self.m_tasks if t in healthy_tasks]
  141. for t in dead_tasks:
  142. if t in current_tasks:
  143. self._cleanup_task(t, "unhealthy or keep-alive failed")
  144. else:
  145. with self.m_lock:
  146. self.m_tasks = [t for t in self.m_tasks if t in healthy_tasks]
  147. def _cache_refresh_loop(self):
  148. self._log("Cache refresh loop started.")
  149. refresh_interval = 15*60
  150. while not self.m_stop_event.is_set():
  151. for _ in range(refresh_interval):
  152. if self.m_stop_event.is_set():
  153. return
  154. time.sleep(1.0)
  155. with self.m_lock:
  156. task_ids = list(self.m_task_data_cache.keys())
  157. if not task_ids:
  158. continue
  159. for tid in task_ids:
  160. if self.m_stop_event.is_set():
  161. break
  162. try:
  163. fresh_data = VSCloudApi.Instance().get_vas_task(tid)
  164. if fresh_data:
  165. with self.m_lock:
  166. if tid in self.m_task_data_cache:
  167. self.m_task_data_cache[tid] = fresh_data
  168. except Exception:
  169. pass
  170. time.sleep(0.5)
  171. def _booking_trigger_loop(self):
  172. self._log("Pub/Sub Trigger loop started.")
  173. channel_to_routing_key = {}
  174. for apt in self.m_cfg.appointment_types:
  175. channel = self._get_redis_key(apt.routing_key)
  176. channel_to_routing_key[channel] = apt.routing_key
  177. if not channel_to_routing_key:
  178. self._log("No appointment types configured. Exiting trigger loop.")
  179. return
  180. pubsub = None
  181. while not self.m_stop_event.is_set():
  182. try:
  183. if pubsub is None:
  184. pubsub = self.redis_sub.pubsub(ignore_subscribe_messages=False)
  185. channels_to_sub = list(channel_to_routing_key.keys())
  186. self._log(f"⏳ Sending SUBSCRIBE command to Redis for: {channels_to_sub}")
  187. pubsub.subscribe(*channels_to_sub)
  188. message = pubsub.get_message(timeout=5.0)
  189. if not message:
  190. continue
  191. channel = message['channel']
  192. if isinstance(channel, bytes):
  193. channel = channel.decode('utf-8')
  194. if message['type'] == 'subscribe':
  195. active_subs = message['data']
  196. self._log(f"📡 [Redis ACK] Successfully subscribed to: {channel} (Active connection subs: {active_subs})")
  197. continue
  198. if message['type'] != 'message':
  199. continue
  200. raw_data = message['data']
  201. if isinstance(raw_data, bytes):
  202. raw_data = raw_data.decode('utf-8')
  203. routing_key = channel_to_routing_key.get(channel)
  204. if not routing_key:
  205. continue
  206. try:
  207. data = json.loads(raw_data)
  208. query_result = VSQueryResult.model_validate(data['query_result'])
  209. query_result.apt_type = AppointmentType.model_validate(data['apt_type'])
  210. except Exception as parse_err:
  211. self._log(f"Data parsing error for channel {channel}: {parse_err}")
  212. continue
  213. now = time.time()
  214. matching_tasks = []
  215. with self.m_lock:
  216. for task in self.m_tasks:
  217. if now < task.next_run or not task.book_allowed:
  218. continue
  219. if routing_key not in task.acceptable_routing_keys:
  220. continue
  221. task.next_run = now + self.m_cfg.booker.booking_cooldown
  222. matching_tasks.append(task)
  223. if matching_tasks:
  224. for task in matching_tasks:
  225. self._log(f"🚀 Triggering BOOK for {routing_key} | Order Ref: {task.task_ref}")
  226. t = threading.Thread(
  227. target=self._execute_book_job,
  228. args=(task, query_result),
  229. daemon=True
  230. )
  231. t.start()
  232. except Exception as e:
  233. self._log(f"Trigger loop pub/sub error: {e}")
  234. if pubsub:
  235. try:
  236. pubsub.close()
  237. except:
  238. pass
  239. pubsub = None
  240. time.sleep(2)
  241. if pubsub:
  242. pubsub.close()
  243. self._log("Pub/Sub connection closed.")
  244. def _execute_book_job(self, task: Task, query_result: VSQueryResult):
  245. task_id = task.task_ref
  246. task_data = None
  247. try:
  248. with self.m_lock:
  249. task_data = self.m_task_data_cache.get(str(task_id))
  250. if not task_data:
  251. self._log(f"Cache miss for {task_id}, fetching from cloud...")
  252. task_data = VSCloudApi.Instance().get_vas_task(str(task_id))
  253. if task_data:
  254. with self.m_lock:
  255. self.m_task_data_cache[str(task_id)] = task_data
  256. if not task_data or task_data.get('status') in ['grabbed', 'pause', 'completed', 'cancelled']:
  257. self._log(f"Bound Task={task_id} is no longer valid or already processed. Removing instance.")
  258. self._remove_task(task, "bound task no longer valid")
  259. self.redis_com.zrem(self.m_tracker_key, task_id)
  260. return
  261. order_id = task_data.get('order_id')
  262. user_input = task_data.get('user_inputs', {})
  263. book_res = task.instance.book(query_result, user_input)
  264. if book_res.success:
  265. self._log(f"✅ BOOK SUCCESS! Order: {order_id}. Destroying instance.")
  266. grab_info = {
  267. "account": book_res.account,
  268. "session_id": book_res.session_id,
  269. "urn": book_res.urn,
  270. "slot_date": book_res.book_date,
  271. "slot_time": book_res.book_time,
  272. "timestamp": int(time.time()),
  273. "payment_link": book_res.payment_link
  274. }
  275. def _update_cloud_success():
  276. try:
  277. VSCloudApi.Instance().update_vas_task(str(task_id), {"status": "grabbed", "grabbed_history": grab_info})
  278. push_content = (
  279. f"🎉 【预定成功通知】\n"
  280. f"━━━━━━━━━━━━━━━\n"
  281. f"订单编号: {order_id}\n"
  282. f"预约账号: {book_res.account}\n"
  283. f"预约日期: {book_res.book_date}\n"
  284. f"预约时间: {book_res.book_time}\n"
  285. f"预约编号: {book_res.urn}\n"
  286. f"支付链接: {book_res.payment_link if book_res.payment_link else '无需支付/暂无'}\n"
  287. f"━━━━━━━━━━━━━━━\n"
  288. )
  289. VSCloudApi.Instance().push_weixin_text(push_content)
  290. except Exception as e:
  291. self._log(f"Failed to update success state to cloud: {e}")
  292. ThreadPool.getInstance().enqueue(_update_cloud_success)
  293. self.redis_com.zrem(self.m_tracker_key, task_id)
  294. self._remove_task(task, "booking success")
  295. else:
  296. self._log(f"❌ BOOK FAILED for Order: {order_id}. Will retry on next signal.")
  297. except Exception as e:
  298. err_str = str(e)
  299. self._log(f"Exception during booking: {err_str}")
  300. rate_limited_indicators = [
  301. "42901" in err_str,
  302. "Rate limited" in err_str
  303. ]
  304. if any(rate_limited_indicators):
  305. self._remove_task(task, "booking rate limited")
  306. if task_data and task_id is not None:
  307. task_meta = task_data.get('meta', {})
  308. t_fails = task_meta.get('booking_failures', 0) + 1
  309. task_meta['booking_failures'] = t_fails
  310. def _update_cloud_meta():
  311. try:
  312. VSCloudApi.Instance().update_vas_task(str(task_id), {"meta": task_meta})
  313. except Exception as cloud_err:
  314. self._log(f"Failed to update task meta: {cloud_err}")
  315. ThreadPool.getInstance().enqueue(_update_cloud_meta)
  316. t_cd = self.task_backoff.calculate(t_fails)
  317. self._log(f"⏳ Task={task_id} (Booking Attempt {t_fails}) suspended for {t_cd:.1f}s.")
  318. self.redis_com.zadd(self.m_tracker_key, {str(task_id): time.time() + t_cd})
  319. def _creator_loop(self):
  320. self._log("Creator loop started.")
  321. spawn_interval = 10.0
  322. while not self.m_stop_event.is_set():
  323. time.sleep(2.0)
  324. now = time.time()
  325. for apt in self.m_cfg.appointment_types:
  326. r_key = apt.routing_key
  327. queue_cd_key = f"vs:queue:cooldown:{r_key}"
  328. if self.redis_com.exists(queue_cd_key):
  329. continue
  330. with self.m_lock:
  331. active = sum(1 for t in self.m_tasks if getattr(t, 'source_queue', '') == r_key)
  332. pending = self.m_pending_order_by_queue.get(r_key, 0)
  333. target = self.m_cfg.booker.target_instances
  334. if (active + pending) < target:
  335. last_spawn = self.m_last_spawn_times.get(r_key, 0.0)
  336. if now - last_spawn >= spawn_interval:
  337. self.m_last_spawn_times[r_key] = now
  338. self._spawn_worker(r_key)
  339. def _spawn_worker(self, target_routing_key: str):
  340. with self.m_lock:
  341. self.m_pending_order_by_queue[target_routing_key] = self.m_pending_order_by_queue.get(target_routing_key, 0) + 1
  342. def _job():
  343. success = False
  344. task_id = None
  345. is_rate_limited = False
  346. try:
  347. queue_name = f"auto.{target_routing_key}"
  348. task_data = VSCloudApi.Instance().get_vas_task_pop(queue_name)
  349. if not task_data:
  350. return
  351. task_id = task_data['id']
  352. with self.m_lock:
  353. self.m_task_data_cache[str(task_id)] = task_data
  354. self.redis_com.zadd(self.m_tracker_key, {str(task_id): time.time() + 5*60.0})
  355. user_inputs = task_data.get('user_inputs', {})
  356. plg_cfg = VSPlgConfig()
  357. plg_cfg.debug = self.m_cfg.debug
  358. plg_cfg.free_config = self.m_cfg.free_config
  359. plg_cfg.session_max_life = self.m_cfg.session_max_life
  360. plg_cfg.account.username = user_inputs.get("username", "")
  361. plg_cfg.account.password = user_inputs.get("password", "")
  362. if not plg_cfg.account.username:
  363. return
  364. acceptable_keys = [target_routing_key]
  365. if self.m_cfg.need_proxy:
  366. proxy = VSCloudApi.Instance().get_next_proxy(self.m_cfg.proxy_pool, self.m_cfg.proxy_cd)
  367. plg_cfg.proxy.id = proxy['id']
  368. plg_cfg.proxy.ip = proxy['ip']
  369. plg_cfg.proxy.port = proxy['port']
  370. plg_cfg.proxy.proto = proxy['proto']
  371. plg_cfg.proxy.username = proxy['username']
  372. plg_cfg.proxy.password = proxy['password']
  373. instance = self.m_factory.create(self.m_cfg.identifier, self.m_cfg.plugin_config.plugin_name)
  374. instance.set_log(self.m_logger)
  375. instance.set_config(plg_cfg)
  376. instance.create_session()
  377. with self.m_lock:
  378. self.m_tasks.append(
  379. Task(
  380. instance=instance,
  381. qw_cfg=self.m_cfg.query_wait,
  382. next_run=time.time(),
  383. task_ref=task_id,
  384. acceptable_routing_keys=acceptable_keys,
  385. source_queue=target_routing_key,
  386. book_allowed=True,
  387. next_remote_ping=time.time() + random.randint(180, 300)
  388. )
  389. )
  390. queue_fail_key = f"vs:queue:failures:{target_routing_key}"
  391. self.redis_com.delete(queue_fail_key)
  392. success = True
  393. self._log(f"+++ Order Booker spawned: {plg_cfg.account.username} (Target: {acceptable_keys})")
  394. except Exception as e:
  395. err_str = str(e)
  396. resource_not_found_indicators = [
  397. "40401" in err_str,
  398. "Account not found" in err_str,
  399. "Proxy not found" in err_str
  400. ]
  401. if any(resource_not_found_indicators):
  402. return
  403. self._log(f"Order Booker spawn failed: {e}")
  404. rate_limited_indicators = [
  405. "42901" in err_str,
  406. "Rate limited" in err_str
  407. ]
  408. if any(rate_limited_indicators):
  409. is_rate_limited = True
  410. queue_fail_key = f"vs:queue:failures:{target_routing_key}"
  411. queue_cd_key = f"vs:queue:cooldown:{target_routing_key}"
  412. q_fails = self.redis_com.incr(queue_fail_key)
  413. q_cd = self.queue_backoff.calculate(q_fails)
  414. self.redis_com.set(queue_cd_key, "1", ex=int(q_cd))
  415. self._log(f"📉 [Rate Limited] Queue '{target_routing_key}' failed {q_fails} times. Global Backoff: {q_cd:.1f}s.")
  416. if task_id is not None:
  417. task_meta = task_data.get('meta') or {}
  418. t_fails = task_meta.get('spawn_failures', 0) + 1
  419. task_meta['spawn_failures'] = t_fails
  420. try:
  421. VSCloudApi.Instance().update_vas_task(str(task_id), {"meta": task_meta})
  422. except Exception as cloud_err:
  423. self._log(f"Failed to update task meta: {cloud_err}")
  424. t_cd = self.account_backoff.calculate(t_fails)
  425. self._log(f"⏳ Task={task_id} (Attempt {t_fails}) suspended for {t_cd:.1f}s.")
  426. self.redis_com.zadd(self.m_tracker_key, {str(task_id): time.time() + t_cd})
  427. finally:
  428. with self.m_lock:
  429. self.m_pending_order_by_queue[target_routing_key] = max(0, self.m_pending_order_by_queue[target_routing_key] - 1)
  430. if not success and task_id is not None and not is_rate_limited:
  431. self.redis_com.zadd(self.m_tracker_key, {str(task_id): 0})
  432. self._log(f"♻️ Task={task_id} failed normal spawn. Instantly handed over to Sweeper.")
  433. with self.m_lock:
  434. self.m_task_data_cache.pop(str(task_id), None)
  435. ThreadPool.getInstance().enqueue(_job)