Skip to content

Commit 94c836e

Browse files
committed
update
1 parent c322310 commit 94c836e

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

packages/try/try-faststream/src/try_faststream/run_pub_cron.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@
1515

1616
@group.task(trigger=Every(seconds=5))
1717
async def every():
18-
msg = f"cron task: at {time.time()}"
19-
logger.debug(f"Every 5 seconds, Publish msg to Nats, msg: {msg}")
18+
msg = f"cron one: at {int(time.time())}"
19+
logger.debug(f"cron one: pub msg: {msg}")
2020

2121
await broker.publish(
2222
msg,
2323
subject="test-cron",
2424
)
2525

2626

27+
@group.task(trigger=Every(seconds=5))
28+
async def every():
29+
msg = f"cron batch: at {int(time.time())}"
30+
logger.debug(f"cron batch, pub msg: {msg}")
31+
32+
await broker.publish(
33+
msg,
34+
subject="test-cron2",
35+
)
36+
37+
2738
@group.task(trigger=Every(weeks=50))
2839
def even_sync_works():
2940
logger.debug(f"synchronous task")

packages/try/try-faststream/src/try_faststream/run_sub.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import anyio
24
from faststream import FastStream
35
from faststream.nats import NatsBroker, PullSub
@@ -6,6 +8,11 @@
68

79
host = "nats://localhost:4222"
810

11+
# logger file, 进程 id
12+
13+
14+
logger.add(f"tmp/nats_pub_{os.getpid()}.log", rotation="10 MB", retention="10 days")
15+
916
broker = NatsBroker(host)
1017

1118
app = FastStream(broker)
@@ -20,13 +27,20 @@ async def to_batch(body: str, msg: NatsMessage):
2027
logger.debug(f"subscriber batch: msg: {msg}")
2128

2229

23-
@broker.subscriber("test-cron")
30+
@broker.subscriber("test-cron", "cron")
2431
async def to_cron(body: str, msg: NatsMessage):
2532
"""
2633
TODO X: 定时任务
2734
"""
28-
logger.debug(f"subscriber batch: {body}")
29-
logger.debug(f"subscriber batch: msg id: {msg.message_id}")
35+
logger.debug(f"subscriber cron one: {body}, {msg.message_id}")
36+
37+
38+
@broker.subscriber("test-cron2")
39+
async def to_cron(body: str, msg: NatsMessage):
40+
"""
41+
TODO X: 定时任务
42+
"""
43+
logger.debug(f"subscriber cron batch: {body}, {msg.message_id}")
3044

3145

3246
@broker.subscriber("test-workers", "workers")

0 commit comments

Comments
 (0)