-
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 7 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
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
| # tweet_from_queue.py | ||
| import asyncio | ||
| import json | ||
| import logging | ||
| from datetime import datetime | ||
| from typing import Any, Callable, List | ||
|
|
||
| import nats | ||
| from apscheduler.schedulers.asyncio import AsyncIOScheduler | ||
| from nats.aio.msg import Msg | ||
| from nats.aio.subscription import Subscription | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TweetFromQueueContext: | ||
| """ | ||
| 1. 订阅 NATS subject,累积推文 | ||
| 2. APScheduler 每 10 秒强制 flush 一次(不管有没有消息) | ||
| 3. 支持优雅关闭 | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| size: int, | ||
| user_ids: List[str], | ||
| nats_url: str, | ||
| callback: Callable[[List[dict[str, Any]]], Any], | ||
| flush_seconds: int = 10, | ||
| ): | ||
| self.size = size | ||
| self.user_ids = user_ids | ||
| self.nats_url = nats_url | ||
| self.flush_seconds = flush_seconds | ||
|
|
||
| self._nc = None | ||
| self._buffer: List[dict[str, Any]] = [] | ||
| self._callback: Callable[[List[dict[str, Any]]], Any] = callback | ||
| self._sub: Subscription | None = None | ||
| self._scheduler: AsyncIOScheduler | None = None # type: ignore[no-any-unimported] | ||
| self._lock = asyncio.Lock() | ||
|
|
||
| # -------------------- 生命周期 -------------------- | ||
| async def start(self, subject: str) -> None: | ||
| # 1. 连接 NATS | ||
| self._nc = await nats.connect(self.nats_url) # type: ignore[assignment] | ||
| self._sub = await self._nc.subscribe(subject, cb=self._nc_consume) # type: ignore[attr-defined] | ||
| logger.info("Subscribed to <%s>, agent=%s", subject, self.user_ids) | ||
|
|
||
| # 2. 启动 APScheduler 定时 flush | ||
| self._scheduler = AsyncIOScheduler() | ||
| self._scheduler.add_job( | ||
| self._flush_wrap, # 定时执行的协程 | ||
| trigger="interval", | ||
| seconds=self.flush_seconds, | ||
| max_instances=1, | ||
| next_run_time=datetime.now(), # 立即执行一次 | ||
| ) | ||
| self._scheduler.start() | ||
|
|
||
| async def close(self) -> None: | ||
| """优雅关闭:停订阅 -> 停调度器 -> 刷剩余数据 -> 断 NATS""" | ||
| # 1. 停止订阅(不再接收新消息) | ||
| if self._sub: | ||
| await self._sub.unsubscribe() | ||
| self._sub = None | ||
|
|
||
| # 2. 停止调度器(不再定时 flush) | ||
| if self._scheduler: | ||
| self._scheduler.shutdown(wait=False) | ||
|
|
||
| # 3. 刷剩余数据 | ||
| async with self._lock: | ||
| await self._flush() | ||
|
|
||
| # 4. 断开 NATS | ||
| if self._nc: | ||
| await self._nc.close() | ||
| logger.info("NATS connection closed") | ||
|
|
||
| # -------------------- NATS 回调 -------------------- | ||
| async def _nc_consume(self, msg: Msg) -> None: | ||
| try: | ||
| tweet = json.loads(msg.data.decode()) | ||
| tweet = self._fix_tweet_dict(tweet) | ||
| if tweet["author_id"] not in self.user_ids: | ||
| return | ||
| except Exception as e: | ||
| logger.exception("Bad message: %s", e) | ||
| return | ||
|
|
||
| async with self._lock: | ||
| self._buffer.append(tweet) | ||
| # 如果满了也立即刷 | ||
| if len(self._buffer) >= self.size: | ||
| await self._flush() | ||
|
|
||
| # -------------------- 定时刷新包装 -------------------- | ||
| async def _flush_wrap(self) -> None: | ||
| """APScheduler 只支持调用普通函数,这里包一层协程""" | ||
| async with self._lock: | ||
|
boboliu-1010 marked this conversation as resolved.
Outdated
|
||
| await self._flush() | ||
|
|
||
| # -------------------- 真正刷新 -------------------- | ||
| async def _flush(self) -> None: | ||
|
boboliu-1010 marked this conversation as resolved.
|
||
| if not self._buffer: | ||
| return | ||
| # 1. 锁内只做“拷贝 + 清空” | ||
| async with self._lock: | ||
| to_send = self._buffer.copy() | ||
| self._buffer.clear() | ||
| # 2. 锁外执行回调,避免阻塞后续入队 / 定时 flush | ||
| logger.info("Flush %d tweets", len(to_send)) | ||
| try: | ||
| await self._callback(to_send) | ||
| except Exception as e: | ||
| logger.exception("Callback error: %s", e) | ||
|
|
||
| # -------------------- 工具 -------------------- | ||
| @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
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.