Usage guide
Creating an agent
In order to create an agent the following parameters need to be provided.
url
: String formatted with user and password.node
: RabbitMQ topic to bind the agentexchange
: RabbitMQ exchange to listen to messagesmsg_type
: Whether the message should be parsed to JSON or bytes.
Creation of an agent
from fenneq import Agent
url = "amqp://user:pass@localhost"
node = "agent.test"
exchange = "Name of RabbitMQ exchange"
msg_type = Agent.JSON
agent = Agent(url, exchange, node)
Registering a callback
In order to add functionality to an agent, callbacks need to be registered on a handler. A callback is registered by using the on
method.
@agent.on(True) # Run on any message
@agent.on("baz") # Run if string `baz` inside the message
@agent.on({"foo": "bar"}) # Run if key `foo` has value `bar`
@agent.on({"foo": str}) # Run if key foo `present` and is string
def hello()
return "hello world"
Multiple agents can register the same callback for a same handler. The same agent can register a callback multiple times under different handlers.
Running an agent
Tu run an agent, the method run()
has to be called. If there are no handlers registered, the agent returns a RuntimeError
. The agent runs in a blocking loop.
Sending a message
A message can be sent easily to a running agent by using the same configuring another agent. If the agent is configured to use JSON, the message is serialized before sending.