From a51d80ba3d8cf0207f2fc6e6248e82f9481d484b Mon Sep 17 00:00:00 2001 From: maslow Date: Wed, 29 Mar 2023 10:37:01 +0800 Subject: [PATCH] fix(server): rm minio alias init in boot, fix multi region limit; --- server/src/account/account.service.ts | 2 +- server/src/app.module.ts | 2 +- server/src/database/database.service.ts | 27 ++++++++++++------- server/src/initializer/initializer.service.ts | 14 ---------- server/src/instance/instance.service.ts | 2 +- server/src/main.ts | 1 - server/src/storage/minio/minio.service.ts | 19 +++++++++++-- 7 files changed, 38 insertions(+), 29 deletions(-) diff --git a/server/src/account/account.service.ts b/server/src/account/account.service.ts index b8467b9bf3..affb080c52 100644 --- a/server/src/account/account.service.ts +++ b/server/src/account/account.service.ts @@ -81,7 +81,7 @@ export class AccountService { orderNumber: string, amount: number, currency: Currency, - description = 'Account charge', + description = 'laf account charge', ) { // webchat pay if (channel === PaymentChannelType.WeChat) { diff --git a/server/src/app.module.ts b/server/src/app.module.ts index d772c4aba7..b45d78994e 100644 --- a/server/src/app.module.ts +++ b/server/src/app.module.ts @@ -19,7 +19,7 @@ import { RegionModule } from './region/region.module' import { GatewayModule } from './gateway/gateway.module' import { PrismaModule } from './prisma/prisma.module' import { SubscriptionModule } from './subscription/subscription.module' -import { AccountModule } from './account/account.module'; +import { AccountModule } from './account/account.module' @Module({ imports: [ diff --git a/server/src/database/database.service.ts b/server/src/database/database.service.ts index 343b4e9895..72c24924e2 100644 --- a/server/src/database/database.service.ts +++ b/server/src/database/database.service.ts @@ -55,9 +55,7 @@ export class DatabaseService { async findOne(appid: string) { const database = await this.prisma.database.findUnique({ - where: { - appid, - }, + where: { appid }, }) return database @@ -72,15 +70,14 @@ export class DatabaseService { // delete app database in database const doc = await this.prisma.database.delete({ - where: { - appid: database.appid, - }, + where: { appid: database.appid }, }) return doc } - getConnectionUri(region: Region, database: Database) { + // Get application internal database connection uri + getInternalConnectionUri(region: Region, database: Database) { // build app db connection uri from config const parsed = mongodb_uri.parse(region.databaseConf.connectionUri) parsed.database = database.name @@ -91,6 +88,18 @@ export class DatabaseService { return mongodb_uri.format(parsed) } + // Get application control database connection uri + getControlConnectionUri(region: Region, database: Database) { + // build app db connection uri from config + const parsed = mongodb_uri.parse(region.databaseConf.controlConnectionUri) + parsed.database = database.name + parsed.username = database.user + parsed.password = database.password + parsed.options['authSource'] = database.name + + return mongodb_uri.format(parsed) + } + /** * Get database accessor that used for `database-proxy` */ @@ -100,7 +109,7 @@ export class DatabaseService { assert(database, 'Database not found') const dbName = database.name - const connectionUri = this.getConnectionUri(region, database) + const connectionUri = this.getControlConnectionUri(region, database) assert(connectionUri, 'Database connection uri not found') const accessor = new MongoAccessor(dbName, connectionUri) @@ -116,7 +125,7 @@ export class DatabaseService { const database = await this.findOne(appid) assert(database, 'Database not found') - const connectionUri = this.getConnectionUri(region, database) + const connectionUri = this.getControlConnectionUri(region, database) const client = await this.mongoService.connectDatabase( connectionUri, diff --git a/server/src/initializer/initializer.service.ts b/server/src/initializer/initializer.service.ts index d5ef10802e..a2547da013 100644 --- a/server/src/initializer/initializer.service.ts +++ b/server/src/initializer/initializer.service.ts @@ -163,20 +163,6 @@ export class InitializerService { return res } - async initMinioAlias() { - const regions = await this.regionService.findAll() - - for (const region of regions) { - const res = await this.minioService.setMinioClientTarget(region) - if (res.status === 'success') { - this.logger.verbose('MinioService init - ' + region.name + ' success') - } else { - this.logger.error('MinioService init - ' + region.name + ' failed', res) - throw new Error('set minio client target failed ' + region.name) - } - } - } - async createDefaultAuthProvider() { // check if exists const existed = await this.prisma.authProvider.count() diff --git a/server/src/instance/instance.service.ts b/server/src/instance/instance.service.ts index 11351c7bba..55324d6bf0 100644 --- a/server/src/instance/instance.service.ts +++ b/server/src/instance/instance.service.ts @@ -77,7 +77,7 @@ export class InstanceService { // db connection uri const database = await this.databaseService.findOne(appid) - const dbConnectionUri = this.databaseService.getConnectionUri( + const dbConnectionUri = this.databaseService.getInternalConnectionUri( app.region, database, ) diff --git a/server/src/main.ts b/server/src/main.ts index f815eab168..0271bf2952 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -51,7 +51,6 @@ async function bootstrap() { await initService.createDefaultBundle() await initService.createDefaultRuntime() await initService.createDefaultAuthProvider() - await initService.initMinioAlias() } catch (error) { console.error(error) process.exit(1) diff --git a/server/src/storage/minio/minio.service.ts b/server/src/storage/minio/minio.service.ts index e809c1e8a8..01ce8fae24 100644 --- a/server/src/storage/minio/minio.service.ts +++ b/server/src/storage/minio/minio.service.ts @@ -14,6 +14,7 @@ import * as cp from 'child_process' import { promisify } from 'util' import { MinioCommandExecOutput } from './types' import { MINIO_COMMON_USER_GROUP } from 'src/constants' +import { RegionService } from 'src/region/region.service' const exec = promisify(cp.exec) @@ -21,6 +22,20 @@ const exec = promisify(cp.exec) export class MinioService { private readonly logger = new Logger(MinioService.name) + constructor(private readonly regionService: RegionService) { + this.regionService.findAll().then(async (regions) => { + for (const region of regions) { + const res = await this.setMinioClientTarget(region) + if (res.status === 'success') { + this.logger.log('minio alias init - ' + region.name + ' success') + } else { + this.logger.log('minio alias init ' + region.name + ' failed') + this.logger.debug(res) + } + } + }) + } + /** * Create s3 client * @returns @@ -29,7 +44,7 @@ export class MinioService { const conf = region.storageConf return new S3({ - endpoint: conf.externalEndpoint, + endpoint: conf.controlEndpoint, credentials: { accessKeyId: conf.accessKey, secretAccessKey: conf.secretKey, @@ -242,7 +257,7 @@ export class MinioService { const conf = region.storageConf const access_key = conf.accessKey const access_secret = conf.secretKey - const endpoint = conf.externalEndpoint + const endpoint = conf.controlEndpoint const target = region.name const cmd = `alias set ${target} ${endpoint} ${access_key} ${access_secret}`