-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
功能变动: - 将简书积分兑换平台数据迁移到 PostgreSQL,保留必要的双写逻辑和迁移脚本 - 添加独立的 jpep 数据库配置项 - 添加 README 文件 依赖变动: - 升级依赖库
- Loading branch information
Showing
22 changed files
with
746 additions
and
255 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,99 @@ | ||
# JFetcher | ||
# 部署 | ||
|
||
## 环境 | ||
|
||
- Python 3.9+ | ||
- MongoDB | ||
- PostgreSQL | ||
- Prefect 2 | ||
|
||
## 数据库准备 | ||
|
||
创建用户: | ||
|
||
```sql | ||
CREATE ROLE jfetcher LOGIN PASSWORD 'jfetcher'; | ||
``` | ||
|
||
创建数据库: | ||
|
||
```sql | ||
CREATE DATABASE jianshu WITH OWNER = jfetcher; | ||
CREATE DATABASE jpep WITH OWNER = jfetcher; | ||
CREATE DATABASE logs; | ||
``` | ||
|
||
## 配置 | ||
|
||
复制 `config.example.toml` 文件,将其重命名为 `config.toml`。 | ||
|
||
```shell | ||
cp config.example.toml config.toml | ||
``` | ||
|
||
如果您使用 Docker 进行部署: | ||
|
||
- mongo.host 填写 `mongodb` | ||
- jianshu_postgres.host 填写 `postgres` | ||
- jpep_postgres.host 填写 `postgres` | ||
- logging.host 填写 `postgres` | ||
- notify.host 填写 `gotify` | ||
|
||
同时,您需要填写正确的 `postgres.user` 和 `postgres.password`。 | ||
|
||
## 使用 Docker 部署 | ||
|
||
创建 Docker 网络: | ||
|
||
```shell | ||
docker network create gotify | ||
docker network create mongodb | ||
docker network create postgres | ||
docker network create prefect | ||
``` | ||
|
||
您需要在 `gotify` 网络的 `27017` 端口上运行一个 Gotify 服务。 | ||
|
||
您需要在 `mongodb` 网络的 `27017` 端口上运行一个 MongoDB 服务,该服务不开启身份验证。 | ||
|
||
您需要在 `postgres` 网络的 `5173` 端口上运行一个 PostgreSQL 服务,身份验证相关信息请参考 `部署 - 数据库准备` 一节。 | ||
|
||
您需要在 `prefect` 网络的 `4200` 端口上运行一个 Prefect 服务。 | ||
|
||
如您希望更换 Docker 网络名称或服务端口号,请同时调整 `config.toml` 中的相关配置。 | ||
|
||
启动服务: | ||
|
||
```shell | ||
docker compose up -d | ||
``` | ||
|
||
## 传统部署(不推荐) | ||
|
||
下载 Python 项目管理工具 [uv](https://github.com/astral-sh/uv): | ||
|
||
```shell | ||
pip install uv | ||
``` | ||
|
||
安装依赖库(将自动创建虚拟环境): | ||
|
||
```shell | ||
uv install | ||
``` | ||
|
||
您需要在 `8701` 端口上运行一个 Gotify 服务。 | ||
|
||
您需要在 `27017` 端口上运行一个 MongoDB 服务,该服务不开启身份验证。 | ||
|
||
您需要在 `5173` 端口上运行一个 PostgreSQL 服务,身份验证相关信息请参考 `部署 - 数据库准备` 一节。 | ||
|
||
您需要在 `4200` 端口上运行一个 Prefect 服务。 | ||
|
||
如您希望更换服务端口号,请同时调整 `config.toml` 中的相关配置。 | ||
|
||
启动服务: | ||
|
||
```shell | ||
uv run main.py | ||
``` |
This file contains 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 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 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 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 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,26 @@ | ||
from asyncio import run as asyncio_run | ||
|
||
from sshared.logging import Logger | ||
|
||
from models.jpep.credit_record import CreditRecord | ||
from utils.mongo import JPEP_DB | ||
|
||
OLD_COLLECTION = JPEP_DB.credit_history | ||
logger = Logger() | ||
|
||
|
||
async def main() -> None: | ||
await CreditRecord.init() | ||
|
||
logger.info("开始执行数据迁移") | ||
async for item in OLD_COLLECTION.find().sort({"time": 1, "userId": 1}): | ||
await CreditRecord( | ||
time=item["time"], user_id=item["userId"], credit=item["value"] | ||
).create() | ||
|
||
logger.debug(f"已迁移 {item['time']} - {item['userId']}") | ||
|
||
logger.info("数据迁移完成") | ||
|
||
|
||
asyncio_run(main()) |
This file contains 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,51 @@ | ||
from asyncio import run as asyncio_run | ||
|
||
from sshared.logging import Logger | ||
|
||
from models.jpep.new.ftn_macket_record import FTNMacketRecord | ||
from utils.mongo import JPEP_DB | ||
|
||
OLD_COLLECTION = JPEP_DB.ftn_trade_orders | ||
logger = Logger() | ||
|
||
|
||
async def main() -> None: | ||
await FTNMacketRecord.init() | ||
|
||
batch: list[FTNMacketRecord] = [] | ||
|
||
logger.info("开始执行数据迁移") | ||
async for item in OLD_COLLECTION.find().sort({"fetchTime": 1}): | ||
batch.append( | ||
FTNMacketRecord( | ||
fetch_time=item["fetchTime"], | ||
id=item["id"], | ||
price=item["price"], | ||
traded_count=item["tradedCount"], | ||
total_amount=item["amount"]["total"], | ||
traded_amount=item["amount"]["traded"], | ||
remaining_amount=item["amount"]["tradable"], | ||
minimum_trade_amount=item["amount"]["minimumTrade"], | ||
) | ||
) | ||
|
||
if len(batch) == 10000: | ||
await FTNMacketRecord.insert_many(batch) | ||
logger.debug( | ||
f"已迁移 {batch[0].fetch_time}/{batch[0].id} - " | ||
f"{batch[-1].fetch_time}/{batch[-1].id}" | ||
) | ||
batch.clear() | ||
|
||
if batch: | ||
await FTNMacketRecord.insert_many(batch) | ||
logger.debug( | ||
f"已迁移 {batch[0].fetch_time}/{batch[0].id} - " | ||
f"{batch[-1].fetch_time}/{batch[-1].id}" | ||
) | ||
batch.clear() | ||
|
||
logger.info("数据迁移完成") | ||
|
||
|
||
asyncio_run(main()) |
This file contains 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,35 @@ | ||
from asyncio import run as asyncio_run | ||
|
||
from sshared.logging import Logger | ||
|
||
from models.jpep.new.ftn_order import FTNOrder, TypeEnum | ||
from utils.mongo import JPEP_DB | ||
|
||
OLD_COLLECTION = JPEP_DB.ftn_trade_orders | ||
logger = Logger() | ||
|
||
|
||
async def main() -> None: | ||
await FTNOrder.init() | ||
|
||
logger.info("开始执行数据迁移") | ||
for order_id in await OLD_COLLECTION.distinct("id"): | ||
last_record = await OLD_COLLECTION.find_one( | ||
{"id": order_id}, sort={"fetchTime": -1} | ||
) | ||
if not last_record: | ||
raise ValueError | ||
|
||
await FTNOrder( | ||
id=last_record["id"], | ||
type={"buy": TypeEnum.BUY, "sell": TypeEnum.SELL}[last_record["type"]], | ||
publisher_id=last_record["publisherId"], | ||
publish_time=last_record["publishedAt"], | ||
last_seen_time=last_record["fetchTime"], | ||
).create() | ||
logger.debug(f"已迁移订单 {order_id}") | ||
|
||
logger.info("数据迁移完成") | ||
|
||
|
||
asyncio_run(main()) |
Oops, something went wrong.