move retry count and delay for waiting to database into config

This commit is contained in:
einhornimmond 2025-05-14 16:43:38 +02:00
parent 3943157002
commit c9434a88c7
13 changed files with 59 additions and 9 deletions

View File

@ -34,8 +34,14 @@ const server = {
}
const database = {
DB_CONNECT_RETRY_COUNT: process.env.DB_CONNECT_RETRY_COUNT
? Number.parseInt(process.env.DB_CONNECT_RETRY_COUNT)
: 15,
DB_CONNECT_RETRY_DELAY_MS: process.env.DB_CONNECT_RETRY_DELAY_MS
? Number.parseInt(process.env.DB_CONNECT_RETRY_DELAY_MS)
: 500,
DB_HOST: process.env.DB_HOST ?? 'localhost',
DB_PORT: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306,
DB_PORT: process.env.DB_PORT ? Number.parseInt(process.env.DB_PORT) : 3306,
DB_USER: process.env.DB_USER ?? 'root',
DB_PASSWORD: process.env.DB_PASSWORD ?? '',
DB_DATABASE: process.env.DB_DATABASE ?? 'gradido_community',

View File

@ -3,6 +3,8 @@ import {
COMMUNITY_NAME,
COMMUNITY_SUPPORT_MAIL,
COMMUNITY_URL,
DB_CONNECT_RETRY_COUNT,
DB_CONNECT_RETRY_DELAY_MS,
DB_DATABASE,
DB_HOST,
DB_PASSWORD,
@ -38,6 +40,8 @@ export const schema = Joi.object({
DB_USER,
DB_VERSION,
DB_DATABASE,
DB_CONNECT_RETRY_COUNT,
DB_CONNECT_RETRY_DELAY_MS,
DECAY_START_TIME,
GDT_API_URL,
GDT_ACTIVE,

View File

@ -36,9 +36,11 @@ export const createServer = async (
// open mariadb connection, retry connecting with mariadb
// check for correct database version
// retry max 15 times, wait 500 ms between tries
// TODO: move variables into config
const con = await checkDBVersionUntil(15, 500)
// retry max CONFIG.DB_CONNECT_RETRY_COUNT times, wait CONFIG.DB_CONNECT_RETRY_DELAY ms between tries
const con = await checkDBVersionUntil(
CONFIG.DB_CONNECT_RETRY_COUNT,
CONFIG.DB_CONNECT_RETRY_DELAY_MS,
)
// Express Server
const app = express()

View File

@ -6,7 +6,7 @@ import { CONFIG } from '@/config'
import { Connection } from '@/typeorm/connection'
import { Connection as DbConnection } from 'typeorm'
async function checkDBVersionUntil(maxRetries = 15, delayMs = 500): Promise<DbConnection> {
async function checkDBVersionUntil(maxRetries: number, delayMs: number): Promise<DbConnection> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const connection = await Connection.getInstance()

View File

@ -37,6 +37,20 @@ export const DB_VERSION = Joi.string()
)
.required()
export const DB_CONNECT_RETRY_COUNT = Joi.number()
.default(15)
.min(1)
.max(1000)
.description('Number of retries to connect to the database')
.optional()
export const DB_CONNECT_RETRY_DELAY_MS = Joi.number()
.default(500)
.min(100)
.max(10000)
.description('Delay in milliseconds between retries to connect to the database')
.optional()
export const COMMUNITY_URL = Joi.string()
.uri({ scheme: ['http', 'https'] })
.custom((value: string, helpers: Joi.CustomHelpers<string>) => {

View File

@ -18,6 +18,12 @@ const server = {
}
const database = {
DB_CONNECT_RETRY_COUNT: process.env.DB_CONNECT_RETRY_COUNT
? Number.parseInt(process.env.DB_CONNECT_RETRY_COUNT)
: 15,
DB_CONNECT_RETRY_DELAY_MS: process.env.DB_CONNECT_RETRY_DELAY_MS
? Number.parseInt(process.env.DB_CONNECT_RETRY_DELAY_MS)
: 500,
DB_HOST: process.env.DB_HOST ?? 'localhost',
DB_PORT: process.env.DB_PORT ? Number.parseInt(process.env.DB_PORT) : 3306,
DB_USER: process.env.DB_USER ?? 'root',

View File

@ -1,6 +1,8 @@
import {
COMMUNITY_DESCRIPTION,
COMMUNITY_NAME,
DB_CONNECT_RETRY_COUNT,
DB_CONNECT_RETRY_DELAY_MS,
DB_DATABASE,
DB_HOST,
DB_PASSWORD,
@ -19,6 +21,8 @@ export const schema = Joi.object({
COMMUNITY_NAME,
COMMUNITY_DESCRIPTION,
DB_DATABASE,
DB_CONNECT_RETRY_COUNT,
DB_CONNECT_RETRY_DELAY_MS,
DB_HOST,
DB_PASSWORD,
DB_PORT,

View File

@ -6,7 +6,7 @@ import { checkDBVersionUntil } from './typeorm/DBVersion'
async function main() {
// open mysql connection
await checkDBVersionUntil()
await checkDBVersionUntil(CONFIG.DB_CONNECT_RETRY_COUNT, CONFIG.DB_CONNECT_RETRY_DELAY_MS)
logger.debug(`dhtseed set by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`)
logger.info(
`starting Federation on ${CONFIG.FEDERATION_DHT_TOPIC} ${

View File

@ -6,7 +6,7 @@ import { CONFIG } from '@/config'
import { Connection as DbConnection } from 'typeorm'
import { connection as connectionFunc } from './connection'
async function checkDBVersionUntil(maxRetries = 15, delayMs = 500): Promise<DbConnection> {
async function checkDBVersionUntil(maxRetries: number, delayMs: number): Promise<DbConnection> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const connection = await connectionFunc()

View File

@ -30,6 +30,12 @@ const server = {
PRODUCTION: process.env.NODE_ENV === 'production',
}
const database = {
DB_CONNECT_RETRY_COUNT: process.env.DB_CONNECT_RETRY_COUNT
? Number.parseInt(process.env.DB_CONNECT_RETRY_COUNT)
: 15,
DB_CONNECT_RETRY_DELAY_MS: process.env.DB_CONNECT_RETRY_DELAY_MS
? Number.parseInt(process.env.DB_CONNECT_RETRY_DELAY_MS)
: 500,
DB_HOST: process.env.DB_HOST ?? 'localhost',
DB_PORT: process.env.DB_PORT ? Number.parseInt(process.env.DB_PORT) : 3306,
DB_USER: process.env.DB_USER ?? 'root',

View File

@ -1,4 +1,6 @@
import {
DB_CONNECT_RETRY_COUNT,
DB_CONNECT_RETRY_DELAY_MS,
DB_DATABASE,
DB_HOST,
DB_PASSWORD,
@ -17,6 +19,8 @@ import Joi from 'joi'
export const schema = Joi.object({
DB_DATABASE,
DB_CONNECT_RETRY_COUNT,
DB_CONNECT_RETRY_DELAY_MS,
DB_HOST,
DB_PASSWORD,
DB_PORT,

View File

@ -17,6 +17,7 @@ import { schema } from '@/graphql/schema'
// import { elopageWebhook } from '@/webhook/elopage'
import { Connection } from 'typeorm'
import { CONFIG } from '@/config'
import { slowDown } from 'express-slow-down'
import helmet from 'helmet'
import { Logger } from 'log4js'
@ -39,7 +40,10 @@ export const createServer = async (
logger.debug('createServer...')
// open mysql connection
const con = await checkDBVersionUntil()
const con = await checkDBVersionUntil(
CONFIG.DB_CONNECT_RETRY_COUNT,
CONFIG.DB_CONNECT_RETRY_DELAY_MS,
)
// Express Server
const app = express()

View File

@ -4,7 +4,7 @@ import { Migration } from 'database'
import { Connection as DbConnection } from 'typeorm'
import { connection as connectionFunc } from './connection'
async function checkDBVersionUntil(maxRetries = 15, delayMs = 500): Promise<DbConnection> {
async function checkDBVersionUntil(maxRetries: number, delayMs: number): Promise<DbConnection> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const connection = await connectionFunc()