로컬(개발)용과 운영용 설정분리

This commit is contained in:
2025-12-28 13:45:41 +09:00
parent a871ec8008
commit 716f4f8791
15 changed files with 661 additions and 368 deletions

View File

@@ -1,4 +1,4 @@
import { getDb } from './db'
import { query, queryOne, execute } from './db'
// 상수 정의
const INTERVAL_SUCCESS = 5 * 60 * 1000 // 5분
@@ -84,10 +84,10 @@ class PrivnetScheduler {
const nextInterval = result.isSuccess ? INTERVAL_SUCCESS : INTERVAL_FAILURE
// 로그 저장
this.saveLog(result)
await this.saveLog(result)
// 상태 업데이트 (인덱스 증가)
this.updateStatus(result, nextInterval)
await this.updateStatus(result, nextInterval)
console.log(
`[PrivnetScheduler] ${result.targetName} (${result.url}) - ` +
@@ -109,21 +109,23 @@ class PrivnetScheduler {
* 현재 타겟 URL 체크
*/
private async checkCurrentTarget(): Promise<CheckResult> {
const db = getDb()
// 활성화된 타겟 목록 조회
const targets = db.prepare(`
const targets = await query<PrivnetTarget>(`
SELECT * FROM privnet_targets
WHERE is_active = 1
ORDER BY id ASC
`).all() as PrivnetTarget[]
`)
if (targets.length === 0) {
throw new Error('No active targets found')
}
// 현재 인덱스 조회
const status = db.prepare('SELECT * FROM privnet_status WHERE id = 1').get() as PrivnetStatus
const status = await queryOne<PrivnetStatus>('SELECT * FROM privnet_status WHERE id = 1')
if (!status) {
throw new Error('Status not found')
}
const currentIndex = status.current_index % targets.length
const target = targets[currentIndex]
@@ -160,60 +162,50 @@ class PrivnetScheduler {
/**
* 체크 결과 로그 저장
*/
private saveLog(result: CheckResult): void {
const db = getDb()
db.prepare(`
INSERT INTO privnet_logs (target_id, is_success)
VALUES (@targetId, @isSuccess)
`).run({
targetId: result.targetId,
isSuccess: result.isSuccess ? 1 : 0
})
private async saveLog(result: CheckResult): Promise<void> {
await execute(
`INSERT INTO privnet_logs (target_id, is_success) VALUES ($1, $2)`,
[result.targetId, result.isSuccess ? 1 : 0]
)
}
/**
* 상태 업데이트 (인덱스 순환)
*/
private updateStatus(result: CheckResult, nextInterval: number): void {
const db = getDb()
private async updateStatus(result: CheckResult, nextInterval: number): Promise<void> {
// 활성 타겟 수 조회
const countResult = db.prepare(`
SELECT COUNT(*) as cnt FROM privnet_targets WHERE is_active = 1
`).get() as { cnt: number }
const countResult = await queryOne<{ cnt: string }>(
`SELECT COUNT(*) as cnt FROM privnet_targets WHERE is_active = 1`
)
const totalCount = parseInt(countResult?.cnt || '0')
const status = db.prepare('SELECT current_index FROM privnet_status WHERE id = 1').get() as { current_index: number }
const nextIndex = (status.current_index + 1) % countResult.cnt
const status = await queryOne<{ current_index: number }>(
'SELECT current_index FROM privnet_status WHERE id = 1'
)
const nextIndex = ((status?.current_index || 0) + 1) % totalCount
db.prepare(`
await execute(`
UPDATE privnet_status SET
current_index = @nextIndex,
check_interval = @checkInterval,
is_healthy = @isHealthy,
last_target_id = @lastTargetId,
last_checked_at = datetime('now', 'localtime'),
updated_at = datetime('now', 'localtime')
current_index = $1,
check_interval = $2,
is_healthy = $3,
last_target_id = $4,
last_checked_at = to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS'),
updated_at = to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS')
WHERE id = 1
`).run({
nextIndex,
checkInterval: nextInterval,
isHealthy: result.isSuccess ? 1 : 0,
lastTargetId: result.targetId
})
`, [nextIndex, nextInterval, result.isSuccess ? 1 : 0, result.targetId])
}
/**
* 스케줄러 실행 상태 업데이트
*/
private updateSchedulerRunning(running: number): void {
const db = getDb()
db.prepare(`
private async updateSchedulerRunning(running: number): Promise<void> {
await execute(`
UPDATE privnet_status SET
scheduler_running = @running,
updated_at = datetime('now', 'localtime')
scheduler_running = $1,
updated_at = to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS')
WHERE id = 1
`).run({ running })
`, [running])
}
}