booker_order.py 23 KB

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