Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

986 B
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { REDIS_URL } from "@carbon/env";
import Redis from "ioredis";
import { logUnavailable, reconnectStrategy, withResilience } from "./resilient";

declare global {
  var __redis: Redis | undefined;
}

if (!REDIS_URL) {
  throw new Error("REDIS_URL is not defined");
}

if (!global.__redis) {
  const client = new Redis(REDIS_URL, {
    maxRetriesPerRequest: 3,
    lazyConnect: true, // don't connect until first command
    enableOfflineQueue: true, // buffer commands while connecting
    retryStrategy: reconnectStrategy
  });
  // Registered once; without a listener ioredis re-emits errors as unhandled process events.
  client.on("error", logUnavailable);
  global.__redis = client;
}

const redis = withResilience(global.__redis);

export default redis;