How to emit and use custom events
Learn how to define specific trigger conditions based on custom event grammar.
Motivating custom events
Imagine you are running an e-commerce platform and you want to trigger a deployment when a customer completes an order.
There might be a number of events that occur during an order on your platform, for example:
order.created
order.item.added
order.payment-method.confirmed
order.shipping-method.added
order.complete
Event grammar
The above choices of event names are arbitrary. With Prefect events, you’re free to select any event grammar that best represents your use case.
In this case, we want to trigger a deployment when a user completes an order, so our trigger should:
expect
anorder.complete
eventafter
anorder.created
event- evaluate these conditions
for_each
user id
Finally, it should pass the user_id
as a parameter to the deployment.
Define the trigger
Here’s how this looks in code:
Specify multiple events or resources
The expect
and after
fields accept a set
of event names, so you can specify multiple events for each condition.
Similarly, the for_each
field accepts a set
of resource ids.
Simulate events
To simulate users causing order status events, run the following in a Python shell or script:
In the above example:
user_id_1
creates and then completes an order, triggering a run of our deployment.user_id_2
creates an order, but no completed event is emitted so no deployment is triggered.