Skip to content

Commit

Permalink
优化nsq连接问题
Browse files Browse the repository at this point in the history
  • Loading branch information
秦宏有 committed Jun 10, 2024
1 parent c82b372 commit ff02051
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 16 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ https://discordpy.readthedocs.io/en/stable/api.html?highlight=on_message#discord
https://github.com/discord/discord-api-docs/blob/main/docs/interactions/Application_Commands.md

## 导出环境包

pip freeze > requirements.txt

## nsq 教程

https://www.cnblogs.com/yezigege/p/13791476.html

## 远程端口映射
ssh -R 9914:47.101.161.124:9914 [email protected]

ssh -L 9914:47.101.161.124:9914 [email protected]

## ChatGPT&Midjounery 第三方技术文章

Expand All @@ -43,15 +45,14 @@ https://zhuanlan.zhihu.com/p/643189533
git config --global --unset http.proxy
git config --global --unset https.proxy

## docker安装(centor)
## docker 安装(centor)

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker

## 安装制定的docker-compose版本
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose


## 安装制定的 docker-compose 版本

sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2 changes: 1 addition & 1 deletion designer-service/manifest/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ logger:
level: "all"
stdout: false
proxy:
url: "http://43.130.233.13:8000"
url: "http://43.153.178.204:8000"

discord:
trigger_url: "https://discord.com/api/v9/interactions"
Expand Down
1 change: 1 addition & 0 deletions midjourney-bot/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from config.toml import load_cfg
from config.config_manager import ConfigManager
16 changes: 16 additions & 0 deletions midjourney-bot/config/config_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class ConfigManager:
_instance = None
cfg = None

def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(ConfigManager, cls).__new__(cls, *args, **kwargs)
return cls._instance

@classmethod
def set_cfg(cls, cfg):
cls.cfg = cfg

@classmethod
def get_cfg(cls):
return cls.cfg
7 changes: 4 additions & 3 deletions midjourney-bot/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from loguru import logger
from my_discord import bot, NsqMsg
from config import load_cfg
from dotenv import load_dotenv
from os import getenv
from typing import List
import threading
from config import load_cfg, ConfigManager
from dotenv import load_dotenv
from os import getenv


# 加载环境
load_dotenv()
cfg_path = getenv("CONFIG_FILE_PATH")
cfg = load_cfg(cfg_path)
ConfigManager.set_cfg(cfg)


def nsq_thread(addresses: List[str]):
Expand Down
2 changes: 1 addition & 1 deletion midjourney-bot/my_discord/channel_listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@bot.event
async def on_ready():
logger.debug(f"Logged in as {bot.user} (ID: {bot.user.id})")



@bot.event
Expand Down Expand Up @@ -51,6 +50,7 @@ async def on_message_edit(before: Message, after: Message):
status = TriggerStatus.GEN
await write_message_to_nsq(user_id, status, after)


@bot.event
async def on_message_delete(message: Message):
logger.debug(f"on_message_delete content: {message.content}")
18 changes: 17 additions & 1 deletion midjourney-bot/my_discord/msg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import TypedDict, List
from dataclasses import dataclass
import json


@dataclass
class Attachment(TypedDict):
"""这里面是存的关于下载的mj图片信息"""

Expand All @@ -15,11 +18,24 @@ class Attachment(TypedDict):
ephemeral: bool


@dataclass
class TopicData:
"""消息主题数据"""

use_id: str
user_id: str
status: str
content: str
attachments: List[Attachment]
message_id: str

def to_dict(self):
return {
"user_id": self.user_id,
"status": self.status,
"content": self.content,
"attachments": self.attachments,
"message_id": self.message_id,
}

def to_json(self):
return json.dumps(self.to_dict())
10 changes: 8 additions & 2 deletions midjourney-bot/my_discord/msg_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from discord import Message
from my_discord.msg_nsq import NsqMsg
from my_discord.msg import TopicData, Attachment
from config import ConfigManager
from loguru import logger


PROMPT_PREFIX = "<#"
Expand All @@ -30,7 +32,9 @@ def match_user_id(content: str) -> Union[str, None]:

async def write_message_to_nsq(user_id: str, status: str, message: Message):
# 获取nsq_msg单例实例
nsq_msg = NsqMsg()
cfg = ConfigManager.get_cfg()
addresses = cfg["nsq"]["addresses"]
nsq_msg = NsqMsg(addresses)
topic = TopicData(
user_id=user_id,
status=status,
Expand All @@ -40,4 +44,6 @@ async def write_message_to_nsq(user_id: str, status: str, message: Message):
],
message_id=message.id,
)
nsq_msg.pub_message(MSG_TOPIC, topic)
topic_json = topic.to_dict()
logger.debug(topic_json)
nsq_msg.pub_message(MSG_TOPIC, topic_json)
4 changes: 2 additions & 2 deletions midjourney-bot/my_discord/msg_nsq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def __init__(self, addresses) -> None:
self.queue = Queue()
self.loop = tornado.ioloop.IOLoop.current()

def pub_message(self, topic: str, data: TopicData):
def pub_message(self, topic: str, data: str):
"""推送数据到消息队列上去
Args:
data (TopicData) pub
data (str) pub
"""
self.queue.put((topic, data))
self.loop.add_callback(self._pub_msg_next)
Expand Down
17 changes: 17 additions & 0 deletions midjourney-bot/test/tes_nsq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import nsq
import tornado.ioloop
import time


def pub_message():
message = time.strftime("%H:%M:%S").encode("utf-8") # Encode the message to bytes
writer.pub("test", message, finish_pub)


def finish_pub(conn, data):
print(data)


writer = nsq.Writer(["47.101.161.124:9912"])
tornado.ioloop.PeriodicCallback(pub_message, 1000).start()
nsq.run()

0 comments on commit ff02051

Please sign in to comment.