Subscribers Not Working
This guide helps you troubleshoot issues when subscribers in your Medusa application are not running.
Confirm Event is Emitted#
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.
For example:
If the event is not being emitted, check the logic in your code to ensure that the event emission is correctly implemented.
Confirm Subscriber is Set Up Correctly#
Make sure that your subscriber is correctly set up to listen to the event you're emitting. Check the following:
- The subscriber is created under the
src/subscribers
directory. - The subscriber exports an asynchronus function that handles the event. For example:
1import { SubscriberArgs, type SubscriberConfig } from "@medusajs/framework"2 3export default async function orderPlacedHandler({4 event: { data },5 container,6}: SubscriberArgs<{ id: string }>) {7 const logger = container.resolve("logger")8 9 logger.info("Sending confirmation email...")10 11 // ...12}
- The subscriber exports a configuration object with the correct event name. For example:
If your subscriber is set up correctly, you'll see the following log in the terminal when the event is emitted:
Confirm Worker Mode is Set Up Correctly#
If you emit an event, but subscribers listening to that event don't run, it may be because your Medusa application is not running in worker mode.
Why this Happens#
By default, the Medusa application runs both the server, which handles all incoming requests, and the worker, which processes background tasks, in a single process.
In production, you should deploy two separate instances of your Medusa application: one for the server and one for the worker.
However, if you deploy only a server instance, or if you accidentally set the worker mode to server
, the subscribers won't run because there is no worker process to handle them.
How to Fix it#
Deploy a Worker Instance
If you haven't deployed a worker instance of your Medusa application, do so by following the steps in the Worker Mode of Medusa Instance guide.
For example, you should have the following configuration in your medusa-config.ts
file:
In your deployed server Medusa instance, set WORKER_MODE
to server
, and in your deployed worker Medusa instance, set WORKER_MODE
to worker
.
Set the Correct Worker Mode
If this issue is happening locally, ensure that you set the correct worker mode in your medusa-config.ts
file.
When running the server locally, either don't set the workerMode
configuration or set it to shared
in medusa-config.ts
: