|
1
|
import { Table } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Event Processing Priority`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
<Note> |
|
10
|
|
|
11
|
Priority-based event processing is available since [Medusa v2.13.0](https://github.com/medusajs/medusa/releases/tag/v2.13.0). |
|
12
|
|
|
13
|
</Note> |
|
14
|
|
|
15
|
## How Event Priority Works |
|
16
|
|
|
17
|
Medusa uses a priority queue system for event processing to ensure that critical business events are processed first. This improves system responsiveness and prevents internal operations from blocking customer-facing functionality. |
|
18
|
|
|
19
|
For example, if you're adding a large number of products to the catalog, and a user places an order at the same time, Medusa will prioritize processing the `order.placed` event and its subscribers before handling the product addition events. |
|
20
|
|
|
21
|
```mermaid |
|
22
|
graph TD |
|
23
|
D["1. order.placed emitted<br/>(Priority: CRITICAL - 10)"] --> B[Event Queue] |
|
24
|
E["2. product.created emitted<br/>(Priority: DEFAULT - 100)"] --> B |
|
25
|
|
|
26
|
B --> C{Priority Check} |
|
27
|
|
|
28
|
C -->|"Step 1: Process CRITICAL first"| F[order.placed Subscriber] |
|
29
|
C -->|"Step 2: Process DEFAULT after"| G[product.created Subscriber] |
|
30
|
|
|
31
|
F --> H[Send confirmation email] |
|
32
|
G --> I[Update search index] |
|
33
|
|
|
34
|
style G fill:#F4F4F5 |
|
35
|
style I fill:#F4F4F5 |
|
36
|
``` |
|
37
|
|
|
38
|
In this example, even though both events are emitted around the same time, Medusa processes the `order.placed` event (priority 10) before the `product.created` event (priority 100), ensuring the customer receives their order confirmation promptly. |
|
39
|
|
|
40
|
--- |
|
41
|
|
|
42
|
## Event Priority Levels |
|
43
|
|
|
44
|
Events are assigned a priority level that determines their processing order. Lower numbers indicate higher priority and are processed first. |
|
45
|
|
|
46
|
<Table> |
|
47
|
<Table.Header> |
|
48
|
<Table.Row> |
|
49
|
<Table.HeaderCell> |
|
50
|
Priority Level (Numeric Value) |
|
51
|
</Table.HeaderCell> |
|
52
|
<Table.HeaderCell> |
|
53
|
Description |
|
54
|
</Table.HeaderCell> |
|
55
|
</Table.Row> |
|
56
|
</Table.Header> |
|
57
|
<Table.Body> |
|
58
|
<Table.Row> |
|
59
|
<Table.Cell> |
|
60
|
`CRITICAL` (10) |
|
61
|
</Table.Cell> |
|
62
|
<Table.Cell> |
|
63
|
Highest priority for critical business events. Medusa uses this for the order placement event. |
|
64
|
</Table.Cell> |
|
65
|
</Table.Row> |
|
66
|
<Table.Row> |
|
67
|
<Table.Cell> |
|
68
|
`HIGH` (50) |
|
69
|
</Table.Cell> |
|
70
|
<Table.Cell> |
|
71
|
High priority for important operations. For example, you can assign this priority to customer account-related events. |
|
72
|
</Table.Cell> |
|
73
|
</Table.Row> |
|
74
|
<Table.Row> |
|
75
|
<Table.Cell> |
|
76
|
`DEFAULT` (100) |
|
77
|
</Table.Cell> |
|
78
|
<Table.Cell> |
|
79
|
The default priority used if no specific priority is assigned. All Medusa business events except for order placement use this priority. |
|
80
|
</Table.Cell> |
|
81
|
</Table.Row> |
|
82
|
<Table.Row> |
|
83
|
<Table.Cell> |
|
84
|
`LOW` (500) |
|
85
|
</Table.Cell> |
|
86
|
<Table.Cell> |
|
87
|
Low priority for background operations. For example, you can assign this priority to third-party sync events. |
|
88
|
</Table.Cell> |
|
89
|
</Table.Row> |
|
90
|
<Table.Row> |
|
91
|
<Table.Cell> |
|
92
|
`LOWEST` (2,097,152) |
|
93
|
</Table.Cell> |
|
94
|
<Table.Cell> |
|
95
|
Lowest priority. Medusa assigns this to internal system events. |
|
96
|
</Table.Cell> |
|
97
|
</Table.Row> |
|
98
|
</Table.Body> |
|
99
|
</Table> |
|
100
|
|
|
101
|
--- |
|
102
|
|
|
103
|
## Emit Events with Custom Priority |
|
104
|
|
|
105
|
When emitting custom events in your application, you can specify a priority level to control when the event is processed relative to other events in the queue. |
|
106
|
|
|
107
|
For example, to emit a `payment.processed` event with `CRITICAL` priority in a custom workflow: |
|
108
|
|
|
109
|
export const emitHighlights = [ |
|
110
|
["14", "priority", "Set the event priority to CRITICAL."] |
|
111
|
] |
|
112
|
|
|
113
|
```ts title="src/workflows/handle-payment.ts" highlights={emitHighlights} |
|
114
|
import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk" |
|
115
|
import { emitEventStep } from "@medusajs/medusa/core-flows" |
|
116
|
import { EventPriority } from "@medusajs/framework/utils" |
|
117
|
|
|
118
|
export const handlePaymentWorkflow = createWorkflow( |
|
119
|
"handle-payment", |
|
120
|
(input: { paymentId: string }) => { |
|
121
|
emitEventStep({ |
|
122
|
eventName: "payment.processed", |
|
123
|
data: { |
|
124
|
id: input.paymentId, |
|
125
|
}, |
|
126
|
options: { |
|
127
|
priority: EventPriority.CRITICAL, |
|
128
|
}, |
|
129
|
}) |
|
130
|
|
|
131
|
return new WorkflowResponse({ success: true }) |
|
132
|
} |
|
133
|
) |
|
134
|
``` |
|
135
|
|
|
136
|
You pass the priority level in the `options` parameter of the `emitEventStep`. You can use the `EventPriority` enum to specify the desired priority: |
|
137
|
|
|
138
|
- `EventPriority.CRITICAL` (10): For critical business events. |
|
139
|
- `EventPriority.HIGH` (50): For high-priority operations. |
|
140
|
- `EventPriority.DEFAULT` (100): The default priority level. If you don't specify a priority, this level is used. |
|
141
|
- `EventPriority.LOW` (500): For low-priority background tasks. |
|
142
|
- `EventPriority.LOWEST` (2,097,152): For the lowest priority events like internal system operations. |
|
143
|
|
|
144
|
--- |
|
145
|
|
|
146
|
## When to Emit Events with Custom Priority |
|
147
|
|
|
148
|
Specify a custom priority for your custom events based on their importance to your business operations: |
|
149
|
|
|
150
|
- **Critical business operations**: Events that directly impact customer experience. For example, payment processing or order fulfillment. |
|
151
|
- **High-priority integrations**: Real-time syncs with external systems that customers depend on, such as inventory management or CRM updates. |
|
152
|
- **Low-priority background tasks**: Cleanup operations, analytics processing, or batch jobs that can be deferred. For example, syncing data to a data warehouse. |
|
153
|
|
|
154
|
If you're unsure about the priority level to assign, don't assign a custom priority. Medusa will use the `DEFAULT` priority level, which is suitable for most use cases. |