auto commit
This commit is contained in:
parent
15c76e0e92
commit
1419114d1e
@ -66,11 +66,11 @@ services:
|
|||||||
- ./fems-timescaledb/pg_hba.conf:/etc/postgresql/pg_hba.conf:ro
|
- ./fems-timescaledb/pg_hba.conf:/etc/postgresql/pg_hba.conf:ro
|
||||||
- ./fems-timescaledb/init-scripts:/docker-entrypoint-initdb.d/:ro
|
- ./fems-timescaledb/init-scripts:/docker-entrypoint-initdb.d/:ro
|
||||||
command: ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
|
command: ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
|
||||||
# healthcheck:
|
healthcheck:
|
||||||
# test:
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
||||||
# ["CMD-SHELL", "pg_isready -U ${TIMESCALEDB_USER} -d ${TIMESCALEDB_DB}"]
|
interval: 10s
|
||||||
networks:
|
timeout: 5s
|
||||||
- internal
|
retries: 5
|
||||||
|
|
||||||
fems-redis:
|
fems-redis:
|
||||||
image: redis:alpine
|
image: redis:alpine
|
||||||
@ -86,15 +86,15 @@ services:
|
|||||||
- ./backups/redis:/backups
|
- ./backups/redis:/backups
|
||||||
environment:
|
environment:
|
||||||
- NODE_ENV=${NODE_ENV:-development}
|
- NODE_ENV=${NODE_ENV:-development}
|
||||||
- REDIS_PASSWORD=${NODE_ENV:-development:-REDIS_PASSWORD}
|
- REDIS_PASSWORD=${REDIS_PASSWORD}
|
||||||
depends_on:
|
depends_on:
|
||||||
- fems-postgres
|
- fems-postgres
|
||||||
- fems-timescaledb
|
- fems-timescaledb
|
||||||
# healthcheck:
|
healthcheck:
|
||||||
# test: ["CMD", "redis-cli", "ping"]
|
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
|
||||||
# interval: 30s
|
interval: 10s
|
||||||
# timeout: 10s
|
timeout: 5s
|
||||||
# retries: 3
|
retries: 3
|
||||||
|
|
||||||
# fems-mqtt:
|
# fems-mqtt:
|
||||||
# build:
|
# build:
|
||||||
|
@ -6,12 +6,42 @@ const MainBackendService = require("./services/mainBackend.service");
|
|||||||
const MQTTService = require("./services/mqtt.service");
|
const MQTTService = require("./services/mqtt.service");
|
||||||
const SensorDataModel = require("./models/SensorData");
|
const SensorDataModel = require("./models/SensorData");
|
||||||
const createDataController = require("./controllers/data.controller");
|
const createDataController = require("./controllers/data.controller");
|
||||||
// 다른 서비스나 컨트롤러 파일들
|
|
||||||
const logger = require("./config/logger");
|
const logger = require("./config/logger");
|
||||||
const config = require("./config/config");
|
const config = require("./config/config");
|
||||||
|
|
||||||
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
async function waitForServices() {
|
||||||
|
const maxRetries = 30;
|
||||||
|
let retries = 0;
|
||||||
|
while (retries < maxRetries) {
|
||||||
|
try {
|
||||||
|
const pool = new Pool(config.timescaledb);
|
||||||
|
await pool.query("SELECT 1");
|
||||||
|
await pool.end();
|
||||||
|
const redis = new Redis(config.redis);
|
||||||
|
await redis.ping();
|
||||||
|
await redis.quit();
|
||||||
|
logger.info("Successfully connected to all services");
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
retries++;
|
||||||
|
logger.warn(
|
||||||
|
`Failed to connect to services, attempt ${retries}/${maxRetries}`
|
||||||
|
);
|
||||||
|
await sleep(2000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error("Failed to connect to required services");
|
||||||
|
}
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
|
let mqttService = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 서비스 연결 대기
|
||||||
|
await waitForServices();
|
||||||
|
|
||||||
// 데이터베이스 연결
|
// 데이터베이스 연결
|
||||||
const pool = new Pool(config.timescaledb);
|
const pool = new Pool(config.timescaledb);
|
||||||
const redis = new Redis(config.redis);
|
const redis = new Redis(config.redis);
|
||||||
@ -27,7 +57,7 @@ async function bootstrap() {
|
|||||||
config.mainBackend.adminApiKey
|
config.mainBackend.adminApiKey
|
||||||
);
|
);
|
||||||
|
|
||||||
const mqttService = new MQTTService(
|
mqttService = new MQTTService(
|
||||||
mainBackend,
|
mainBackend,
|
||||||
sensorData,
|
sensorData,
|
||||||
redis,
|
redis,
|
||||||
@ -43,6 +73,7 @@ async function bootstrap() {
|
|||||||
|
|
||||||
// 서비스 인스턴스를 app.locals에 저장
|
// 서비스 인스턴스를 app.locals에 저장
|
||||||
app.locals.mainBackend = mainBackend;
|
app.locals.mainBackend = mainBackend;
|
||||||
|
app.locals.mqttService = mqttService;
|
||||||
|
|
||||||
// 라우터 설정
|
// 라우터 설정
|
||||||
app.use("/api/v1/data", createDataController(pool, redis));
|
app.use("/api/v1/data", createDataController(pool, redis));
|
||||||
@ -54,6 +85,9 @@ async function bootstrap() {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Failed to start server:", error);
|
logger.error("Failed to start server:", error);
|
||||||
|
if (mqttService) {
|
||||||
|
await mqttService.disconnect();
|
||||||
|
}
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user