-
Notifications
You must be signed in to change notification settings - Fork 8
Dev/clientlist #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dev/clientlist #47
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ae12627
feat: update tweet get
6d8418f
feat: update tweet get
6adc2d6
feat: add tweet hub client
9d04d15
feat: add tweet from queue
08e7b34
feat: format kol
c8b06ab
feat: update consume & flash
69d730a
feat: update consume & flash
d09de68
feat: update consume & flash
4a7835b
feat: update consume & flash
096ad10
feat: update consume & flash
4943650
feat: update consume & flash
a6d593d
feat: update consume & flash
9e44c5c
feat: update consume & flash
45b28f9
feat: update consume & flash
c6486b5
feat: update consume & flash
c163167
feat: mypy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/sunagent-ext/src/sunagent_ext/tweet/tweet_from_queue.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import asyncio | ||
| import json | ||
| import logging | ||
| from datetime import datetime | ||
| from typing import Any, Callable, Dict, List, Optional | ||
|
|
||
| import nats | ||
| from nats.aio.msg import Msg | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TweetFromQueueContext: | ||
| # ---------- 新增哨兵 ---------- | ||
| _SENTINEL = object() # 用于通知 worker 立即退出 | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| batch_size: int, | ||
| flush_seconds: float, | ||
| callback: Callable[[List[Dict[str, Any]]], Any], | ||
| nats_url: str, | ||
| subject: str, | ||
| user_ids: Optional[List[str]] = None, | ||
| ): | ||
| if batch_size <= 0 or flush_seconds <= 0: | ||
| raise ValueError("batch_size / flush_seconds 必须为正") | ||
| self.batch_size = batch_size | ||
| self.flush_seconds = flush_seconds | ||
| self.callback = callback | ||
| self.nats_url = nats_url | ||
| self.subject = subject | ||
| self.user_ids = set(user_ids) if user_ids else None | ||
|
|
||
| self._nc: Optional[nats.NATS] = None # type: ignore[name-defined] | ||
| self._sub = None | ||
| # 队列元素现在是 Dict | sentinel | ||
| self._queue: asyncio.Queue[Any] = asyncio.Queue() | ||
| self._stop_evt = asyncio.Event() | ||
| self._worker_task: Optional[asyncio.Task] = None # type: ignore[type-arg] | ||
|
|
||
| # -------------------- 生命周期 -------------------- | ||
| async def start(self) -> None: | ||
| self._nc = await nats.connect(self.nats_url) | ||
| self._sub = await self._nc.subscribe(self.subject, cb=self._on_msg) # type: ignore[assignment] | ||
| logger.info("Subscribed to <%s>, filter=%s", self.subject, self.user_ids) | ||
| self._worker_task = asyncio.create_task(self._worker_loop()) | ||
|
|
||
| async def stop(self) -> None: | ||
| logger.info("Stopping AsyncBatchingQueue...") | ||
|
|
||
| # 1. 立即停止接收新消息 | ||
| if self._sub: | ||
| await self._sub.unsubscribe() | ||
|
|
||
| # 2. 通知 worker 退出并等待它刷完剩余 | ||
| self._stop_evt.set() | ||
| await self._queue.put(self._SENTINEL) # 让 worker 从 queue.get() 立即返回 | ||
| if self._worker_task: | ||
| await self._worker_task # 内部已 _drain_remaining() | ||
|
|
||
| # 3. 关闭 NATS 连接 | ||
| if self._nc: | ||
| await self._nc.close() | ||
| logger.info("AsyncBatchingQueue stopped") | ||
|
|
||
| async def __aenter__(self): # type: ignore[no-untyped-def] | ||
| await self.start() | ||
| return self | ||
|
|
||
| async def __aexit__(self, exc_type, exc, tb): # type: ignore[no-untyped-def] | ||
| await self.stop() | ||
|
|
||
| # -------------------- 公共入口 -------------------- | ||
| async def add(self, item: Dict[str, Any]) -> None: | ||
| await self._queue.put(item) | ||
|
|
||
| # -------------------- NATS 回调 -------------------- | ||
| async def _on_msg(self, msg: Msg) -> None: | ||
| try: | ||
| tweet = json.loads(msg.data.decode()) | ||
| tweet = self._fix_tweet_dict(tweet) | ||
| if self.user_ids and tweet.get("author_id") not in self.user_ids: | ||
| return | ||
| except Exception as e: | ||
| logger.exception("Bad msg: %s", e) | ||
| return | ||
| await self.add(tweet) | ||
|
|
||
| # -------------------- 核心 worker -------------------- | ||
| async def _worker_loop(self) -> None: | ||
| """单协程:永久阻塞等第一条 -> 设 deadline -> 超时/满批刷 -> 收到哨兵退出""" | ||
| while not self._stop_evt.is_set(): | ||
| batch: List[Dict[str, Any]] = [] | ||
| # 1. 永久阻塞等第一条(CPU 不再空转) | ||
| first = await self._queue.get() | ||
| if first is self._SENTINEL: # 收到哨兵直接退出 | ||
| break | ||
| batch.append(first) | ||
| deadline = asyncio.get_event_loop().time() + self.flush_seconds | ||
|
|
||
| # 2. 收集剩余 | ||
| while len(batch) < self.batch_size and not self._stop_evt.is_set(): | ||
| remaining = deadline - asyncio.get_event_loop().time() | ||
| if remaining <= 0: | ||
| break | ||
| try: | ||
| item = await asyncio.wait_for(self._queue.get(), timeout=remaining) | ||
| if item is self._SENTINEL: # 哨兵提前结束收集 | ||
| break | ||
| batch.append(item) | ||
| except asyncio.TimeoutError: | ||
| break | ||
|
|
||
| # 3. 处理 | ||
| if batch: | ||
| try: | ||
| await self.callback(batch) | ||
| except Exception as e: | ||
| logger.exception("Callback error: %s", e) | ||
|
|
||
| # 4. 退出时刷剩余 | ||
| await self._drain_remaining() | ||
|
|
||
| async def _flush(self) -> None: | ||
boboliu-1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """同步刷剩余(给 _drain_remaining 用)""" | ||
| batch = [] | ||
| while not self._queue.empty(): | ||
| item = self._queue.get_nowait() | ||
| if item is self._SENTINEL: # 忽略哨兵 | ||
| continue | ||
| batch.append(item) | ||
| if batch: | ||
| try: | ||
| await self.callback(batch) | ||
| except Exception as e: | ||
| logger.exception("Final callback error: %s", e) | ||
|
|
||
| async def _drain_remaining(self) -> None: | ||
| await self._flush() | ||
|
|
||
| @staticmethod | ||
| def _fix_tweet_dict(msg: Dict[str, Any]) -> Dict[str, Any]: | ||
| fixed = msg.copy() | ||
| for key in ("created_at", "updated_at"): | ||
| if key in fixed and isinstance(fixed[key], str): | ||
| fixed[key] = datetime.fromisoformat(fixed[key]) | ||
| return fixed | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.