medusa icon indicating copy to clipboard operation
medusa copied to clipboard

Subscriber is not stable

Open minhchu opened this issue 9 months ago • 4 comments

Describe the bug

Sometimes subscriber does not work.

I create a subscriber:

  Order,
  OrderService,
  type SubscriberArgs,
  type SubscriberConfig,
} from "@medusajs/medusa";
import { render } from "@react-email/render";
import { Mailer } from "src/types/mail-service";

export default async function sendEmailWhenOrderPlaced({
  data,
  container,
}: SubscriberArgs<Record<string, string>>) {
  const mailService: Mailer = container.resolve("mailService")

  const orderService: OrderService = container.resolve("orderService");

  const order: Order = await orderService.retrieve(data.id, {
    relations: ["shipping_address", "billing_address"],
  });
  
  mailer.send .... 
}

export const config: SubscriberConfig = {
  event: OrderService.Events.PLACED,
  context: {
    subscriberId: "send-email-when-order-placed",
  },
};

It works at first, but after some time, it stops working. This issue occurs not only on my local machine but also on the production server (docker + k8s). To resolve the issue, I need to delete the node_modules folder and then reinstall everything.

System information

Medusa version (including plugins): 1.20.4

    "@medusajs/admin": "7.1.11",
    "@medusajs/cache-inmemory": "^1.8.9",
    "@medusajs/cache-redis": "^1.8.9",
    "@medusajs/event-bus-local": "^1.9.8",
    "@medusajs/event-bus-redis": "^1.8.11",
    "@medusajs/file-local": "^1.0.3",
    "@medusajs/medusa": "^1.20.4",
    "@medusajs/ui": "^2.4.1",

Node.js version: v18.17.1 Database: pgsql 16.2 Operating system: Ubuntu 20.04 Browser (if relevant):

Steps to reproduce the behavior

It is hard to reproduce. Sometimes it happens.

Expected behavior

Subscriber must work stably.

minhchu avatar Apr 26 '24 10:04 minhchu

Quick question but are you using Redis in prod ?

adevinwild avatar Apr 26 '24 13:04 adevinwild

@adevinwild yes. I'm using Redis in local and prod

minhchu avatar Apr 27 '24 06:04 minhchu

We are facing the same issue while trying order placed subscriber.

We tested redis on local, railway and redis website. However same problem.

In our case medusa prod deployment on railway often fails to trigger the subscribers whereas local deployment of medusa generally works (not always).

Attaching the log when it doesn't trigger the subscriber. There is no error and no mention of the order.placed subscriber. When it works successfully I get the following logs

Processing cart.updated which has 0 subscribers
Processing product-variant.updated which has 0 subscribers
Processing payment.updated which has 0 subscribers
Processing order.placed which has 3 subscribers

Updating all the package versions helped. Earlier prod was not working at all when event-bus-redis is enabled. It was only working 60-70% of the time with event-bus-local. Here is current dependencies in my package.json

    "@medusajs/admin": "latest",
    "@medusajs/cache-inmemory": "latest",
    "@medusajs/cache-redis": "latest",
    "@medusajs/event-bus-local": "latest",
    "@medusajs/event-bus-redis": "latest",
    "@medusajs/file-local": "latest",
    "@medusajs/medusa": "latest",
    "@tanstack/react-query": "4.22.0",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "16.3.1",
    "express": "^4.17.2",
    "medusa-fulfillment-manual": "latest",
    "medusa-interfaces": "latest",
    "medusa-payment-manual": "latest",
    "medusa-payment-stripe": "latest",
    "medusa-plugin-sendgrid": "latest",
    "prism-react-renderer": "^2.0.4",
    "typeorm": "^0.3.16"

Please share any thoughts on fixing this. Currently our migration to medusa is blocked on this.

Thank you, Som

railway.txt

som-greetwell avatar Apr 30 '24 19:04 som-greetwell

Forgot to mention. FLUSH ALL in redis then restart medusa server fix the problem. It is a destructive command, only run it in local env.

minhchu avatar May 01 '24 03:05 minhchu

we have the same problem. Order.placed is extremly unreliable and we can't figure out why

WoodlegDev avatar May 28 '24 15:05 WoodlegDev

A lot of people are having the same issue on v1.2x.x

adevinwild avatar May 28 '24 19:05 adevinwild

image image

A solution has been found here by Jono on the Medusa.js Discord : https://discord.com/channels/876835651130097704/1245069522810048532

I think this issue can be closed now

adevinwild avatar May 28 '24 20:05 adevinwild

Yes, not sharing the redis instance across different environments solved the problem for us.

If you are deploying on Railway App then you should create the redis within the environment itself (e.g. redis within production env) and that will avoid the issue and keep things clean.

som-greetwell avatar May 29 '24 00:05 som-greetwell

yeah it solved the problem for us. Thanks again for the quick help in the discord!

WoodlegDev avatar May 30 '24 20:05 WoodlegDev