hwgilbert16 / scholarsome

Web-based interactive flashcard learning software
http://scholarsome.com
GNU Affero General Public License v3.0
492 stars 26 forks source link

Redis Connection Error #161

Closed evenevan closed 2 months ago

evenevan commented 2 months ago

When using the app with the Docker compose setup, it sometimes crashes after some usage. I tried a bunch of things like trying an IP in place of the Redis hostname, among other things. Running the Redis server with --loglevel verbose, I can see no connection attempt to it. I'm glossing over what I tried because its been a while since I started poking around and also because I think I found a fix.

> scholarsome@1.2.0 serve:node
> npm run migrate && node dist/apps/api/main.js

> scholarsome@1.2.0 migrate
> npx prisma migrate deploy

Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "scholarsome" at "scholarsome_mariadb:3306"

12 migrations found in prisma/migrations

No pending migrations to apply.
[Scholarsome] Info      6/28/2024, 1:33:33 AM Scholarsome has started! +0ms
[Scholarsome] Error     6/28/2024, 1:49:41 AM uncaughtException: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
    at Socket.<anonymous> (/usr/src/app/node_modules/ioredis/built/redis/event_handler.js:182:37)
    at Object.onceWrapper (node:events:633:26)
    at Socket.emit (node:events:518:28)
    at TCP.<anonymous> (node:net:337:12) - {
  error: {},
  stack: 'MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.\n' +
    '    at Socket.<anonymous> (/usr/src/app/node_modules/ioredis/built/redis/event_handler.js:182:37)\n' +
    '    at Object.onceWrapper (node:events:633:26)\n' +
    '    at Socket.emit (node:events:518:28)\n' +
    '    at TCP.<anonymous> (node:net:337:12)',
  exception: true,
  date: 'Fri Jun 28 2024 01:49:41 GMT+0000 (Coordinated Universal Time)',
  process: {
    pid: 18,
    uid: 0,
    gid: 0,
    cwd: '/usr/src/app',
    execPath: '/usr/local/bin/node',
    version: 'v20.11.1',
    argv: [ '/usr/local/bin/node', '/usr/src/app/dist/apps/api/main.js' ],
    memoryUsage: {
      rss: 129327104,
      heapTotal: 53673984,
      heapUsed: 51399080,
      external: 2822225,
      arrayBuffers: 334413
    }
  },
  os: { loadavg: [ 0.99, 1.35, 1.99 ], uptime: 2416781 },
  trace: [
    {
      column: 37,
      file: '/usr/src/app/node_modules/ioredis/built/redis/event_handler.js',
      function: null,
      line: 182,
      method: null,
      native: false
    },
    {
      column: 26,
      file: 'node:events',
      function: 'Object.onceWrapper',
      line: 633,
      method: 'onceWrapper',
      native: false
    },
    {
      column: 28,
      file: 'node:events',
      function: 'Socket.emit',
      line: 518,
      method: 'emit',
      native: false
    },
    {
      column: 12,
      file: 'node:net',
      function: null,
      line: 337,
      method: null,
      native: false
    }
  ]
} +16m

Running the development environment, I could replicate the same thing.

ENV file:

# This is the full environment file, intended for use with development installations

# Use production if deploying for use
NODE_ENV=production

# Required if using compose
# Password to provide for internal database
DATABASE_PASSWORD=

# Complete connection URI to MariaDB
DATABASE_URL=mysql://mysql:pIYNFnoxcSOxAIdwayX6Xif0cqbZK7vS@tower:3306/scholarsome

# Random secret used to validate tokens
JWT_SECRET=testing

# Port to expose Scholarsome on
HTTP_PORT=3333

# Domain that Scholarsome will be running on
HOST=localhost:4200

# Redis server details
REDIS_HOST=192.168.1.69
REDIS_PORT=6379
REDIS_USERNAME=
REDIS_PASSWORD=nxcXSfA1hlE3yfWIU0qCH8dKgTLgHBp4

# Data storage configuration
STORAGE_TYPE=local

# Required if storage type is local
# Absolute filepath
STORAGE_LOCAL_DIR=/scholarsome

# Required if storage type is s3
S3_STORAGE_ENDPOINT=
S3_STORAGE_ACCESS_KEY=
S3_STORAGE_SECRET_KEY=
S3_STORAGE_REGION=
S3_STORAGE_BUCKET=

# ---
# Everything past this line is optional
# ---

# Self-explanatory SMTP fields
# Necessary for emails to be sent - otherwise, users are verified by default
SMTP_HOST=localhost
SMTP_PORT=25
SMTP_USERNAME=test
SMTP_PASSWORD=test

# SSL key and cert encoded as base64
SSL_KEY_BASE64=
SSL_CERT_BASE64=

# Recaptcha site key and secret key
SCHOLARSOME_RECAPTCHA_SITE=
SCHOLARSOME_RECAPTCHA_SECRET=

# Additional scripts to be placed on the head of every page encoded in base64
SCHOLARSOME_HEAD_SCRIPTS_BASE64=
NestJS MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
NestJS     at Socket.<anonymous> (C:\Users\User\Documents\code\scholarsome\node_modules\ioredis\built\redis\event_handler.js:182:37)
NestJS     at Object.onceWrapper (node:events:629:26)
NestJS     at Socket.emit (node:events:514:28)
NestJS     at TCP.<anonymous> (node:net:337:12) - {
NestJS   error: {},
NestJS   stack: 'MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.\n' +
NestJS     '    at Socket.<anonymous> (C:\\Users\\User\\Documents\\code\\scholarsome\\node_modules\\ioredis\\built\\redis\\event_handler.js:182:37)\n' +
NestJS     '    at Object.onceWrapper (node:events:629:26)\n' +
NestJS     '    at Socket.emit (node:events:514:28)\n' +
NestJS     '    at TCP.<anonymous> (node:net:337:12)',
NestJS   exception: true,
NestJS   date: 'Thu Jun 27 2024 23:36:53 GMT-0700 (Pacific Daylight Saving Time)',
NestJS   process: {
NestJS     pid: 11384,
NestJS     uid: null,
NestJS     gid: null,
NestJS     cwd: 'C:\\Users\\User\\Documents\\code\\scholarsome',
NestJS     execPath: 'C:\\Program Files\\nodejs\\node.exe',
NestJS     version: 'v20.10.0',
NestJS     argv: [
NestJS       'C:\\Program Files\\nodejs\\node.exe',
NestJS       'C:\\Users\\User\\Documents\\code\\scholarsome\\node_modules\\@nx\\js\\src\\executors\\node\\node-with-require-overrides'
NestJS     ],
NestJS     memoryUsage: {
NestJS       rss: 116367360,
NestJS       heapTotal: 61001728,
NestJS       heapUsed: 57396176,
NestJS       external: 4196053,
NestJS       arrayBuffers: 117773
NestJS     }
NestJS   },
NestJS   os: { loadavg: [ 0, 0, 0 ], uptime: 109593.875 },
NestJS   trace: [
NestJS     {
NestJS       column: 37,
NestJS       file: 'C:\\Users\\User\\Documents\\code\\scholarsome\\node_modules\\ioredis\\built\\redis\\event_handler.js',
NestJS       function: null,
NestJS       line: 182,
NestJS       method: null,
NestJS       native: false
NestJS     },
NestJS     {
NestJS       column: 26,
NestJS       file: 'node:events',
NestJS       function: 'Object.onceWrapper',
NestJS       line: 629,
NestJS       method: 'onceWrapper',
NestJS       native: false
NestJS     },
NestJS     {
NestJS       column: 28,
NestJS       file: 'node:events',
NestJS       function: 'Socket.emit',
NestJS       line: 514,
NestJS       method: 'emit',
NestJS       native: false
NestJS     },
NestJS     {
NestJS       column: 12,
NestJS       file: 'node:net',
NestJS       function: null,
NestJS       line: 337,
NestJS       method: null,
NestJS       native: false
NestJS     }
NestJS   ]
NestJS } +1ms

I changed a line in https://github.com/hwgilbert16/scholarsome/blob/a3754d457dbab4969975b4fd6f131651b856470c/apps/api/src/app/app.module.ts#L69

- defaultOptions: {
+ commonOptions: {
    host: configService.get<string>("REDIS_HOST"),
    port: configService.get<number>("REDIS_PORT"),
    username: configService.get<string>("REDIS_USERNAME"),
    password: configService.get<string>("REDIS_PASSWORD")
},

Successful connection to Redis and no more issues afaik

1:C 28 Jun 2024 06:47:04.663 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 28 Jun 2024 06:47:04.663 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 28 Jun 2024 06:47:04.663 * Configuration loaded
1:M 28 Jun 2024 06:47:04.664 * monotonic clock: POSIX clock_gettime
1:M 28 Jun 2024 06:47:04.665 * Running mode=standalone, port=6379.
1:M 28 Jun 2024 06:47:04.665 * Server initialized
1:M 28 Jun 2024 06:47:04.668 * Reading RDB base file on AOF loading...
1:M 28 Jun 2024 06:47:04.668 * Loading RDB produced by version 7.2.5
1:M 28 Jun 2024 06:47:04.668 * RDB age 2250548 seconds
1:M 28 Jun 2024 06:47:04.668 * RDB memory usage when created 0.83 Mb
1:M 28 Jun 2024 06:47:04.668 * RDB is base AOF
1:M 28 Jun 2024 06:47:04.668 * Done loading RDB, keys loaded: 0, keys expired: 0.
1:M 28 Jun 2024 06:47:04.668 * DB loaded from base file appendonly.aof.1.base.rdb: 0.001 seconds
1:M 28 Jun 2024 06:47:04.689 * DB loaded from incr file appendonly.aof.1.incr.aof: 0.021 seconds
1:M 28 Jun 2024 06:47:04.689 * DB loaded from append only file: 0.023 seconds
1:M 28 Jun 2024 06:47:04.690 * Opening AOF incr file appendonly.aof.1.incr.aof on server start
1:M 28 Jun 2024 06:47:04.690 * Ready to accept connections tcp
1:M 28 Jun 2024 06:47:09.607 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:14.617 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:19.636 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:24.665 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:29.680 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:34.703 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:39.752 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:44.830 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:49.862 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:54.899 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:47:59.939 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:04.981 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:09.994 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:10.671 - Accepted 192.168.1.69:47640
1:M 28 Jun 2024 06:48:10.671 - Accepted 192.168.1.69:47656
1:M 28 Jun 2024 06:48:15.464 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:20.508 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:25.526 - DB 0: 1 keys (1 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:30.554 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:35.652 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:40.672 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:45.686 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:50.786 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:48:55.858 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:49:00.888 - DB 0: 2 keys (2 volatile) in 4 slots HT.
1:M 28 Jun 2024 06:49:05.919 - DB 0: 2 keys (2 volatile) in 4 slots HT.

Please let me know if you are able to replicate this issue and fix.

hwgilbert16 commented 2 months ago

This is an issue I have been trying to track down for weeks - thank you so much for finding the fix! Cannot believe that the fix was that simple.

evenevan commented 2 months ago

Awesome!