feat(数据库): 添加数据库写入开关配置
添加 DB_WRITE_ENABLED 和 G5_DB_WRITE_ENABLED 环境变量配置 在数据库操作逻辑中增加写入开关检查
This commit is contained in:
@@ -50,6 +50,10 @@ REDIS_DB=15
|
|||||||
REDIS_CONNECT_TIMEOUT_MS=5000
|
REDIS_CONNECT_TIMEOUT_MS=5000
|
||||||
REDIS_PROJECT_NAME=bls-onoffline
|
REDIS_PROJECT_NAME=bls-onoffline
|
||||||
|
|
||||||
|
# DB write switches
|
||||||
|
DB_WRITE_ENABLED=true
|
||||||
|
G5_DB_WRITE_ENABLED=true
|
||||||
|
|
||||||
# G5 room_status.moment sync
|
# G5 room_status.moment sync
|
||||||
# false: do not write room_status.room_status_moment_g5
|
# false: do not write room_status.room_status_moment_g5
|
||||||
G5_ROOM_STATUS_MOMENT_SYNC_ENABLED=false
|
G5_ROOM_STATUS_MOMENT_SYNC_ENABLED=false
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ export const config = {
|
|||||||
} : undefined
|
} : undefined
|
||||||
},
|
},
|
||||||
db: {
|
db: {
|
||||||
|
writeEnabled: parseBoolean(process.env.DB_WRITE_ENABLED, true),
|
||||||
host: process.env.DB_HOST || process.env.POSTGRES_HOST || 'localhost',
|
host: process.env.DB_HOST || process.env.POSTGRES_HOST || 'localhost',
|
||||||
port: parseNumber(process.env.DB_PORT || process.env.POSTGRES_PORT, 5432),
|
port: parseNumber(process.env.DB_PORT || process.env.POSTGRES_PORT, 5432),
|
||||||
user: process.env.DB_USER || process.env.POSTGRES_USER || 'postgres',
|
user: process.env.DB_USER || process.env.POSTGRES_USER || 'postgres',
|
||||||
@@ -56,6 +57,7 @@ export const config = {
|
|||||||
table: process.env.DB_TABLE || 'onoffline_record'
|
table: process.env.DB_TABLE || 'onoffline_record'
|
||||||
},
|
},
|
||||||
g5db: {
|
g5db: {
|
||||||
|
writeEnabled: parseBoolean(process.env.G5_DB_WRITE_ENABLED, true),
|
||||||
enabled: !!process.env.POSTGRES_HOST_G5,
|
enabled: !!process.env.POSTGRES_HOST_G5,
|
||||||
roomStatusMomentSyncEnabled: parseBoolean(process.env.G5_ROOM_STATUS_MOMENT_SYNC_ENABLED, false),
|
roomStatusMomentSyncEnabled: parseBoolean(process.env.G5_ROOM_STATUS_MOMENT_SYNC_ENABLED, false),
|
||||||
host: process.env.POSTGRES_HOST_G5,
|
host: process.env.POSTGRES_HOST_G5,
|
||||||
|
|||||||
@@ -19,10 +19,12 @@ const bootstrap = async () => {
|
|||||||
port: config.db.port,
|
port: config.db.port,
|
||||||
user: config.db.user,
|
user: config.db.user,
|
||||||
database: config.db.database,
|
database: config.db.database,
|
||||||
schema: config.db.schema
|
schema: config.db.schema,
|
||||||
|
writeEnabled: config.db.writeEnabled
|
||||||
},
|
},
|
||||||
g5db: {
|
g5db: {
|
||||||
enabled: config.g5db.enabled,
|
enabled: config.g5db.enabled,
|
||||||
|
writeEnabled: config.g5db.writeEnabled,
|
||||||
roomStatusMomentSyncEnabled: config.g5db.roomStatusMomentSyncEnabled
|
roomStatusMomentSyncEnabled: config.g5db.roomStatusMomentSyncEnabled
|
||||||
},
|
},
|
||||||
kafka: {
|
kafka: {
|
||||||
@@ -180,10 +182,11 @@ const bootstrap = async () => {
|
|||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
const promises = [
|
const promises = [];
|
||||||
dbManager.insertRows({ schema: config.db.schema, table: config.db.table, rows })
|
if (config.db.writeEnabled) {
|
||||||
];
|
promises.push(dbManager.insertRows({ schema: config.db.schema, table: config.db.table, rows }));
|
||||||
if (config.g5db.enabled) {
|
}
|
||||||
|
if (config.g5db.writeEnabled && config.g5db.enabled) {
|
||||||
promises.push(g5DbManager.insertRows({ schema: config.g5db.schema, table: config.g5db.table, rows }).catch(e => {
|
promises.push(g5DbManager.insertRows({ schema: config.g5db.schema, table: config.g5db.table, rows }).catch(e => {
|
||||||
logger.error('G5 Database insert failed but non-blocking', { error: e.message });
|
logger.error('G5 Database insert failed but non-blocking', { error: e.message });
|
||||||
}));
|
}));
|
||||||
@@ -215,10 +218,11 @@ const bootstrap = async () => {
|
|||||||
|
|
||||||
const insertRowsOnce = async (rows) => {
|
const insertRowsOnce = async (rows) => {
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
const promises = [
|
const promises = [];
|
||||||
dbManager.insertRows({ schema: config.db.schema, table: config.db.table, rows })
|
if (config.db.writeEnabled) {
|
||||||
];
|
promises.push(dbManager.insertRows({ schema: config.db.schema, table: config.db.table, rows }));
|
||||||
if (config.g5db.enabled) {
|
}
|
||||||
|
if (config.g5db.writeEnabled && config.g5db.enabled) {
|
||||||
promises.push(g5DbManager.insertRows({ schema: config.g5db.schema, table: config.g5db.table, rows }).catch(e => {
|
promises.push(g5DbManager.insertRows({ schema: config.g5db.schema, table: config.g5db.table, rows }).catch(e => {
|
||||||
logger.error('G5 Database insert failed in insertOnce', { error: e.message });
|
logger.error('G5 Database insert failed in insertOnce', { error: e.message });
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user