diff --git a/config/redis.config.ts b/config/redis.config.ts index 554062b..47b87d9 100644 --- a/config/redis.config.ts +++ b/config/redis.config.ts @@ -1,11 +1,30 @@ -import { createClient } from "redis"; +// Import the Redis library +import Redis from "ioredis"; -const pubClient = createClient({ - url: process.env.REDIS_URI || 'redis://localhost:6379' +// Environment variable for Redis URI +const redisURI = process.env.REDIS_URI || "redis://localhost:6379"; +console.log(`process.env.REDIS_URI is ${process.env.REDIS_URI}`) +// Creating the publisher client +const pubClient = new Redis(redisURI); + +// Creating the subscriber client +const subClient = new Redis(redisURI); + +// Handle connection events for the publisher +pubClient.on("connect", () => { + console.log("Publisher client connected to Redis."); +}); +pubClient.on("error", (err) => { + console.error("Publisher client failed to connect to Redis:", err); }); -(async () => { - await pubClient.connect(); -})(); -const subClient = pubClient.duplicate(); -export { subClient, pubClient }; +// Handle connection events for the subscriber +subClient.on("connect", () => { + console.log("Subscriber client connected to Redis."); +}); +subClient.on("error", (err) => { + console.error("Subscriber client failed to connect to Redis:", err); +}); + +// Export the clients for use in other parts of the application +export { pubClient, subClient }; diff --git a/docker-compose.env b/docker-compose.env index 1197ca2..f70a873 100644 --- a/docker-compose.env +++ b/docker-compose.env @@ -3,12 +3,11 @@ LOGGER=true LOGLEVEL=info SERVICEDIR=dist/services - COMICS_DIRECTORY=/Users/rishi/work/threetwo-core-service/comics USERDATA_DIRECTORY=/Users/rishi/work/threetwo-core-service/userdata REDIS_URI=redis://redis:6379 ELASTICSEARCH_URI=http://elasticsearch:9200 -MONGO_URI=mongodb://mongo:27017/threetwo +MONGO_URI=mongodb://db:27017/threetwo UNRAR_BIN_PATH=/opt/homebrew/bin/unrar SEVENZ_BINARY_PATH=/opt/homebrew/bin/7za diff --git a/docker-compose.yml b/docker-compose.yml index 4f780e7..a521b4a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,7 +11,9 @@ x-comics-volume: &comics-volume services: core-services: build: - context: https://github.com/rishighan/threetwo-core-service.git + # context: https://github.com/rishighan/threetwo-core-service.git + context: ./ + dockerfile: Dockerfile image: frishi/threetwo-core-service container_name: core-services ports: @@ -25,7 +27,7 @@ services: - zookeeper environment: name: core-services - SERVICES: api,library,jobqueue,settings,search,socket,imagetransformation,torrentjobs,opds + SERVICES: api,library,imagetransformation,opds,search,settings,jobqueue,socket,torrentjobs env_file: docker-compose.env volumes: - *comics-volume @@ -59,7 +61,7 @@ services: - proxy db: - image: "bitnami/mongodb:latest" + image: "mongo:latest" container_name: database networks: - proxy diff --git a/moleculer.config.ts b/moleculer.config.ts index 9a0ca78..d0a0b79 100644 --- a/moleculer.config.ts +++ b/moleculer.config.ts @@ -5,6 +5,7 @@ import { MetricRegistry, ServiceBroker, } from "moleculer"; +const RedisTransporter = require("moleculer").Transporters.Redis; /** * Moleculer ServiceBroker configuration file @@ -90,7 +91,7 @@ const brokerConfig: BrokerOptions = { // More info: https://moleculer.services/docs/0.14/networking.html // Note: During the development, you don't need to define it because all services will be loaded locally. // In production you can set it via `TRANSPORTER=nats://localhost:4222` environment variable. - transporter: process.env.REDIS_URI, + transporter: new RedisTransporter(process.env.REDIS_URI), // Define a cacher. // More info: https://moleculer.services/docs/0.14/caching.html diff --git a/package-lock.json b/package-lock.json index d978e66..77abcd6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "http-response-stream": "^1.0.9", "image-js": "^0.34.0", "imghash": "^0.0.9", + "ioredis": "^5.4.1", "jsdom": "^21.1.0", "klaw": "^4.1.0", "leven": "^3.1.0", @@ -40,14 +41,13 @@ "moleculer-db": "^0.8.23", "moleculer-db-adapter-mongoose": "^0.9.4", "moleculer-io": "^2.2.0", - "moleculer-web": "^0.10.5", + "moleculer-web": "^0.10.7", "mongoosastic-ts": "^6.0.3", "mongoose": "^6.10.4", "mongoose-paginate-v2": "^1.3.18", "nats": "^1.3.2", "opds-extra": "^3.0.10", "p7zip-threetwo": "^1.0.4", - "redis": "^4.6.5", "sanitize-filename-ts": "^1.0.2", "sharp": "^0.33.3", "threetwo-ui-typings": "^1.0.14", @@ -2656,64 +2656,6 @@ "resolved": "https://registry.npmjs.org/@npcz/magic/-/magic-1.3.14.tgz", "integrity": "sha512-Jt+fjEVAVoDJh9N+nrQ/IQSC6MFLpIDag8VXxvdVGGG5mrGK2HH4X5KqC9zgzb20fqk2vBM9g2QzyczylKVvqg==" }, - "node_modules/@redis/bloom": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.2.0.tgz", - "integrity": "sha512-HG2DFjYKbpNmVXsa0keLHp/3leGJz1mjh09f2RLGGLQZzSHpkmZWuwJbAvo3QcRY8p80m5+ZdXZdYOSBLlp7Cg==", - "peerDependencies": { - "@redis/client": "^1.0.0" - } - }, - "node_modules/@redis/client": { - "version": "1.5.9", - "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.9.tgz", - "integrity": "sha512-SffgN+P1zdWJWSXBvJeynvEnmnZrYmtKSRW00xl8pOPFOMJjxRR9u0frSxJpPR6Y4V+k54blJjGW7FgxbTI7bQ==", - "dependencies": { - "cluster-key-slot": "1.1.2", - "generic-pool": "3.9.0", - "yallist": "4.0.0" - }, - "engines": { - "node": ">=14" - } - }, - "node_modules/@redis/client/node_modules/yallist": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" - }, - "node_modules/@redis/graph": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.1.0.tgz", - "integrity": "sha512-16yZWngxyXPd+MJxeSr0dqh2AIOi8j9yXKcKCwVaKDbH3HTuETpDVPcLujhFYVPtYrngSco31BUcSa9TH31Gqg==", - "peerDependencies": { - "@redis/client": "^1.0.0" - } - }, - "node_modules/@redis/json": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.4.tgz", - "integrity": "sha512-LUZE2Gdrhg0Rx7AN+cZkb1e6HjoSKaeeW8rYnt89Tly13GBI5eP4CwDVr+MY8BAYfCg4/N15OUrtLoona9uSgw==", - "peerDependencies": { - "@redis/client": "^1.0.0" - } - }, - "node_modules/@redis/search": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.3.tgz", - "integrity": "sha512-4Dg1JjvCevdiCBTZqjhKkGoC5/BcB7k9j99kdMnaXFXg8x4eyOIVg9487CMv7/BUVkFLZCaIh8ead9mU15DNng==", - "peerDependencies": { - "@redis/client": "^1.0.0" - } - }, - "node_modules/@redis/time-series": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.5.tgz", - "integrity": "sha512-IFjIgTusQym2B5IZJG3XKr5llka7ey84fw/NOYqESP5WUfQs9zz1ww/9+qoz4ka/S6KcGBodzlCeZ5UImKbscg==", - "peerDependencies": { - "@redis/client": "^1.0.0" - } - }, "node_modules/@root/walk": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@root/walk/-/walk-1.1.0.tgz", @@ -7160,14 +7102,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/generic-pool": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.9.0.tgz", - "integrity": "sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==", - "engines": { - "node": ">= 4" - } - }, "node_modules/gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -7806,9 +7740,9 @@ "integrity": "sha512-kO3CjNfLZ9t+tHxAMd+Xk4v3D/31E91rMs1dHrm7ikEQrlZ8mLDbQ4z3tZfDM48zOkReas2jx8MWSAmN9+c8Fw==" }, "node_modules/ioredis": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.3.2.tgz", - "integrity": "sha512-1DKMMzlIHM02eBBVOFQ1+AolGjs6+xEcM4PDL7NqOS6szq7H9jSaEkIUH6/a5Hl241LzW6JLSiAbNvTQjUupUA==", + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.4.1.tgz", + "integrity": "sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==", "dependencies": { "@ioredis/commands": "^1.1.1", "cluster-key-slot": "^1.1.0", @@ -10232,9 +10166,9 @@ } }, "node_modules/moleculer-web": { - "version": "0.10.6", - "resolved": "https://registry.npmjs.org/moleculer-web/-/moleculer-web-0.10.6.tgz", - "integrity": "sha512-MGNIH6mXLU2Wj63bAgoVzdhMKXALp99F5UHuiBgS2ywakdWEUl/q7GlMblvscioCCkXuUWezId85J0yioYxedg==", + "version": "0.10.7", + "resolved": "https://registry.npmjs.org/moleculer-web/-/moleculer-web-0.10.7.tgz", + "integrity": "sha512-/UJtV+O7iQ3aSg/xi/sw3ZswhvzkigzGPjKOR5R97sm2FSihKuLTftUpXlk4dYls7/8c8WSz6H/M/40BenEx9Q==", "dependencies": { "@fastify/busboy": "^1.0.0", "body-parser": "^1.19.0", @@ -14142,19 +14076,6 @@ "recursive-watch": "bin.js" } }, - "node_modules/redis": { - "version": "4.6.8", - "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.8.tgz", - "integrity": "sha512-S7qNkPUYrsofQ0ztWlTHSaK0Qqfl1y+WMIxrzeAGNG+9iUZB4HGeBgkHxE6uJJ6iXrkvLd1RVJ2nvu6H1sAzfQ==", - "dependencies": { - "@redis/bloom": "1.2.0", - "@redis/client": "1.5.9", - "@redis/graph": "1.1.0", - "@redis/json": "1.0.4", - "@redis/search": "1.1.3", - "@redis/time-series": "1.0.5" - } - }, "node_modules/redis-errors": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", diff --git a/package.json b/package.json index b558960..1620123 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "http-response-stream": "^1.0.9", "image-js": "^0.34.0", "imghash": "^0.0.9", + "ioredis": "^5.4.1", "jsdom": "^21.1.0", "klaw": "^4.1.0", "leven": "^3.1.0", @@ -70,14 +71,13 @@ "moleculer-db": "^0.8.23", "moleculer-db-adapter-mongoose": "^0.9.4", "moleculer-io": "^2.2.0", - "moleculer-web": "^0.10.5", + "moleculer-web": "^0.10.7", "mongoosastic-ts": "^6.0.3", "mongoose": "^6.10.4", "mongoose-paginate-v2": "^1.3.18", "nats": "^1.3.2", "opds-extra": "^3.0.10", "p7zip-threetwo": "^1.0.4", - "redis": "^4.6.5", "sanitize-filename-ts": "^1.0.2", "sharp": "^0.33.3", "threetwo-ui-typings": "^1.0.14", diff --git a/services/jobqueue.service.ts b/services/jobqueue.service.ts index 830e18c..1c323ad 100644 --- a/services/jobqueue.service.ts +++ b/services/jobqueue.service.ts @@ -11,10 +11,10 @@ import { } from "../utils/uncompression.utils"; import { isNil, isUndefined } from "lodash"; import { pubClient } from "../config/redis.config"; +import IORedis from 'ioredis'; import path from "path"; const { MoleculerError } = require("moleculer").Errors; -console.log(process.env.REDIS_URI); export default class JobQueueService extends Service { public constructor(public broker: ServiceBroker) { super(broker); @@ -24,7 +24,7 @@ export default class JobQueueService extends Service { mixins: [DbMixin("comics", Comic), BullMqMixin], settings: { bullmq: { - client: process.env.REDIS_URI, + client: new IORedis(process.env.REDIS_URI, { maxRetriesPerRequest: null }), }, }, actions: { diff --git a/services/socket.service.ts b/services/socket.service.ts index 6871b11..3e3a4f7 100644 --- a/services/socket.service.ts +++ b/services/socket.service.ts @@ -1,7 +1,6 @@ "use strict"; import { Service, ServiceBroker, ServiceSchema, Context } from "moleculer"; import { JobType } from "moleculer-bullmq"; -import { createClient } from "redis"; import { createAdapter } from "@socket.io/redis-adapter"; import Session from "../models/session.model"; import { pubClient, subClient } from "../config/redis.config"; @@ -325,6 +324,22 @@ export default class SocketService extends Service { }, }, async started() { + this.logger.info("Starting Socket Service..."); + this.logger.debug("pubClient:", pubClient); + this.logger.debug("subClient:", subClient); + if (!pubClient || !subClient) { + this.logger.error("Redis clients are not initialized!"); + throw new Error("Redis clients are not initialized!"); + } + + // Additional checks or logic if necessary + if (pubClient.status !== "ready") { + await pubClient.connect(); + } + + if (subClient.status !== "ready") { + await subClient.connect(); + } this.io.on("connection", async (socket) => { console.log( `socket.io server connected to client with session ID: ${socket.id}` diff --git a/services/torrentjobs.service.ts b/services/torrentjobs.service.ts index c9cdf73..0802206 100644 --- a/services/torrentjobs.service.ts +++ b/services/torrentjobs.service.ts @@ -10,6 +10,7 @@ import { DbMixin } from "../mixins/db.mixin"; import Comic from "../models/comic.model"; import BullMqMixin from "moleculer-bullmq"; const { MoleculerError } = require("moleculer").Errors; +import IORedis from 'ioredis'; export default class ImageTransformation extends Service { // @ts-ignore @@ -23,7 +24,7 @@ export default class ImageTransformation extends Service { mixins: [DbMixin("comics", Comic), BullMqMixin], settings: { bullmq: { - client: process.env.REDIS_URI, + client: new IORedis(process.env.REDIS_URI), }, }, hooks: {},