Skip to content

Commit

Permalink
fix(server): rm minio alias init in boot, fix multi region limit; (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Mar 29, 2023
1 parent 97acba6 commit 251a3c4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion server/src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
27 changes: 18 additions & 9 deletions server/src/database/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ export class DatabaseService {

async findOne(appid: string) {
const database = await this.prisma.database.findUnique({
where: {
appid,
},
where: { appid },
})

return database
Expand All @@ -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
Expand All @@ -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`
*/
Expand All @@ -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)
Expand All @@ -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,
Expand Down
14 changes: 0 additions & 14 deletions server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion server/src/instance/instance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
1 change: 0 additions & 1 deletion server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions server/src/storage/minio/minio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@ 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)

@Injectable()
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
Expand All @@ -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,
Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit 251a3c4

Please sign in to comment.