|
| 1 | +--- |
| 2 | +# 0.5 - API |
| 3 | +# 2 - Release |
| 4 | +# 3 - Contributing |
| 5 | +# 5 - Template Page |
| 6 | +# 10 - Default |
| 7 | +search: |
| 8 | + boost: 10 |
| 9 | +--- |
| 10 | + |
| 11 | +# Scheduling messages |
| 12 | + |
| 13 | +JetStream supports scheduling messages to be delivered at a specific time in the future. This is useful for delayed task execution, reminder systems, or any scenario where you need to defer message processing. |
| 14 | + |
| 15 | +## Enabling Message Scheduling |
| 16 | + |
| 17 | +To use message scheduling, you need to create a JetStream with the `allow_msg_schedules=True` parameter: |
| 18 | + |
| 19 | +```python |
| 20 | +from faststream import FastStream |
| 21 | +from faststream.nats import NatsBroker, JStream, NatsMessage, Schedule |
| 22 | + |
| 23 | +broker = NatsBroker() |
| 24 | + |
| 25 | +@broker.subscriber( |
| 26 | + "test_stream.*", |
| 27 | + stream=JStream("test_stream", allow_msg_schedules=True) |
| 28 | +) |
| 29 | +async def handle_scheduled_message(msg: NatsMessage) -> None: |
| 30 | + # Process the scheduled message when it arrives |
| 31 | + print(f"Received scheduled message: {msg}") |
| 32 | +``` |
| 33 | + |
| 34 | +## Publishing Scheduled Messages |
| 35 | + |
| 36 | +To schedule a message for future delivery, use the `Schedule` object when publishing: |
| 37 | + |
| 38 | +```python |
| 39 | +from datetime import UTC, datetime, timedelta |
| 40 | +from uuid import uuid4 |
| 41 | + |
| 42 | +async def publish_scheduled_message() -> None: |
| 43 | + # Connect to the broker |
| 44 | + await broker.connect() |
| 45 | + |
| 46 | + # Calculate the delivery time (e.g., 3 seconds from now) |
| 47 | + current_time = datetime.now(tz=UTC) |
| 48 | + schedule_time = current_time + timedelta(seconds=3) |
| 49 | + |
| 50 | + # Define the target subject for the scheduled message |
| 51 | + schedule_target = f"test_stream.{uuid4()}" |
| 52 | + |
| 53 | + # Publish the message with a schedule |
| 54 | + await broker.publish( |
| 55 | + message={"type": "do_something"}, |
| 56 | + subject="test_stream.subject", |
| 57 | + schedule=Schedule(schedule_time, schedule_target), |
| 58 | + stream="test_stream", |
| 59 | + timeout=10, |
| 60 | + ) |
| 61 | +``` |
| 62 | + |
| 63 | +## Complete Example |
| 64 | + |
| 65 | +Here's a full working example that demonstrates scheduled message publishing: |
| 66 | + |
| 67 | +```python |
| 68 | +from datetime import UTC, datetime, timedelta |
| 69 | +from uuid import uuid4 |
| 70 | + |
| 71 | +from faststream import FastStream |
| 72 | +from faststream.nats import JStream, NatsBroker, NatsMessage, Schedule |
| 73 | + |
| 74 | +broker = NatsBroker() |
| 75 | + |
| 76 | + |
| 77 | +@broker.subscriber( |
| 78 | + "test_stream.*", |
| 79 | + stream=JStream("test_stream", allow_msg_schedules=True) |
| 80 | +) |
| 81 | +async def handle_scheduled_message(msg: NatsMessage) -> None: |
| 82 | + print(f"Message received at {datetime.now(tz=UTC)}") |
| 83 | + print(msg) |
| 84 | + |
| 85 | + |
| 86 | +async def on_startup() -> None: |
| 87 | + current_time = datetime.now(tz=UTC) |
| 88 | + schedule_time = current_time + timedelta(seconds=3) |
| 89 | + await broker.connect() |
| 90 | + |
| 91 | + schedule_target = f"test_stream.{uuid4()}" |
| 92 | + await broker.publish( |
| 93 | + message={"type": "do_something"}, |
| 94 | + subject="test_stream.subject", |
| 95 | + schedule=Schedule(schedule_time, schedule_target), |
| 96 | + stream="test_stream", |
| 97 | + timeout=10, |
| 98 | + ) |
| 99 | + print(f"Message scheduled for delivery at {schedule_time}") |
| 100 | + |
| 101 | + |
| 102 | +app = FastStream(broker) |
| 103 | +app.on_startup(on_startup) |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + import asyncio |
| 107 | + asyncio.run(app.run()) |
| 108 | +``` |
| 109 | + |
| 110 | +## Key Points |
| 111 | + |
| 112 | +- **Stream Configuration**: The JetStream must be created with `allow_msg_schedules=True` to enable scheduling |
| 113 | +- **Schedule Object**: Takes two parameters: |
| 114 | + - `schedule_time`: A `datetime` object (preferably with UTC timezone) indicating when the message should be delivered |
| 115 | + - `schedule_target`: The subject where the scheduled message will be published, should be unique for every message. |
| 116 | +- **Subject Pattern**: The subscriber should use a wildcard pattern (e.g., `"test_stream.*"`) to match the scheduled target subjects |
| 117 | +- **Timezone**: Always use timezone-aware datetime objects, preferably UTC, to avoid scheduling issues |
0 commit comments