44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
# python fastsream_publisher.py
|
|
import asyncio
|
|
import os
|
|
import sys
|
|
import uuid
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, Field
|
|
from faststream import FastStream
|
|
from faststream.nats import NatsBroker
|
|
|
|
#SUBJECT="mtr_test.request"
|
|
SUBJECT="mtr_client.discover"
|
|
|
|
NATS_SERVERS = os.getenv('NATS_URL', 'nats://localhost:4222').split(',')
|
|
NATS_TOKEN = os.getenv('NATS_TOKEN', '')
|
|
|
|
broker = NatsBroker(servers=NATS_SERVERS, token=NATS_TOKEN)
|
|
app = FastStream(broker)
|
|
|
|
class JobCreated(BaseModel):
|
|
job_id: str = Field(..., description="Job identifier")
|
|
name: str = Field(..., description="Job name")
|
|
|
|
|
|
class JobResponse(BaseModel):
|
|
job_id: str = Field(..., description="Job identifier")
|
|
status: str = Field(..., description="Job status")
|
|
|
|
@broker.subscriber(f"{SUBJECT}.response")
|
|
async def handle_job(event: Any):
|
|
#print(f"{SUBJECT} received job_id={event.job_id} name={event.name}")
|
|
print(f"{event=}")
|
|
# result = uuid.uuid4()
|
|
# await asyncio.sleep(1)
|
|
# print(f"{result=}")
|
|
# return result
|
|
|
|
async def main():
|
|
await app.run()
|
|
|
|
if __name__ == "__main__":
|
|
#SUBJECT=sys.argv[1]
|
|
asyncio.run(main()) |