|
1
|
import NotRunningWorkerMode from "../../_sections/subscribers/not_running_workermode.mdx" |
|
2
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
3
|
|
|
4
|
export const metadata = { |
|
5
|
title: `Subscribers Not Working`, |
|
6
|
} |
|
7
|
|
|
8
|
# {metadata.title} |
|
9
|
|
|
10
|
This troubleshooting guide helps you troubleshoot issues when subscribers in your Medusa application are not running. |
|
11
|
|
|
12
|
## Confirm Event is Emitted |
|
13
|
|
|
14
|
Make sure that the event you're trying to listen to is actually being emitted. You can do this by adding a console log or using a debugger in the part of your code where the event is emitted. |
|
15
|
|
|
16
|
For example: |
|
17
|
|
|
18
|
<CodeTabs group="emit_event"> |
|
19
|
<CodeTab label="In a Workflow" value="workflow"> |
|
20
|
```ts title="src/workflows/your-workflow.ts" |
|
21
|
import { |
|
22
|
createWorkflow, |
|
23
|
transform |
|
24
|
} from "@medusajs/framework/workflows-sdk" |
|
25
|
import { |
|
26
|
emitEventStep, |
|
27
|
} from "@medusajs/medusa/core-flows" |
|
28
|
|
|
29
|
const helloWorldWorkflow = createWorkflow( |
|
30
|
"hello-world", |
|
31
|
() => { |
|
32
|
// ... |
|
33
|
|
|
34
|
emitEventStep({ |
|
35
|
eventName: "custom.created", |
|
36
|
data: { |
|
37
|
id: "123", |
|
38
|
// other data payload |
|
39
|
}, |
|
40
|
}) |
|
41
|
|
|
42
|
transform({}, () => { |
|
43
|
// For debugging purposes |
|
44
|
console.log("Event emitted: custom.created") |
|
45
|
}) |
|
46
|
} |
|
47
|
) |
|
48
|
``` |
|
49
|
|
|
50
|
</CodeTab> |
|
51
|
<CodeTab label="Using Event Module's Service" value="service"> |
|
52
|
|
|
53
|
```ts |
|
54
|
eventBusService_.emit({ |
|
55
|
name: "custom.event", |
|
56
|
data: { |
|
57
|
id: "123", |
|
58
|
// other data payload |
|
59
|
}, |
|
60
|
}) |
|
61
|
|
|
62
|
console.log("Event emitted: custom.event") |
|
63
|
``` |
|
64
|
|
|
65
|
</CodeTab> |
|
66
|
</CodeTabs> |
|
67
|
|
|
68
|
If the event is not being emitted, check the logic in your code to ensure that the event emission is correctly implemented. |
|
69
|
|
|
70
|
--- |
|
71
|
|
|
72
|
## Confirm Subscriber is Set Up Correctly |
|
73
|
|
|
74
|
Make sure that your subscriber is correctly set up to listen to the event you're emitting. Check the following: |
|
75
|
|
|
76
|
1. The subscriber is created under the `src/subscribers` directory. |
|
77
|
2. The subscriber exports an asynchronous function that handles the event. For example: |
|
78
|
|
|
79
|
```ts title="src/subscribers/your-subscriber.ts" |
|
80
|
import { SubscriberArgs, type SubscriberConfig } from "@medusajs/framework" |
|
81
|
import { |
|
82
|
ContainerRegistrationKeys, |
|
83
|
} from "@medusajs/framework/utils" |
|
84
|
|
|
85
|
export default async function orderPlacedHandler({ |
|
86
|
event: { data }, |
|
87
|
container, |
|
88
|
}: SubscriberArgs<{ id: string }>) { |
|
89
|
const logger = container.resolve( |
|
90
|
ContainerRegistrationKeys.LOGGER |
|
91
|
) |
|
92
|
|
|
93
|
logger.info("Sending confirmation email...") |
|
94
|
|
|
95
|
// ... |
|
96
|
} |
|
97
|
``` |
|
98
|
|
|
99
|
3. The subscriber exports a configuration object with the correct event name. For example: |
|
100
|
|
|
101
|
```ts title="src/subscribers/your-subscriber.ts" |
|
102
|
export const config: SubscriberConfig = { |
|
103
|
event: `order.placed`, // Confirm this name is correct |
|
104
|
} |
|
105
|
``` |
|
106
|
|
|
107
|
If your subscriber is set up correctly, you'll see the following log in the terminal when the event is emitted: |
|
108
|
|
|
109
|
```bash |
|
110
|
info: Processing order.placed which has 1 subscribers |
|
111
|
``` |
|
112
|
|
|
113
|
--- |
|
114
|
|
|
115
|
## Confirm Worker Mode is Set Up Correctly |
|
116
|
|
|
117
|
<NotRunningWorkerMode /> |