[2026-05-27] 포맷 후 복구 설치 스크립트 추가
This commit is contained in:
+53
-70
@@ -1,92 +1,75 @@
|
||||
# StockBot 작업 스케줄러 등록 (전체 재등록용)
|
||||
# 실행: powershell -ExecutionPolicy Bypass -File scripts\setup_scheduler.ps1
|
||||
# 주의: 이 파일은 UTF-8 BOM으로 저장해야 한글 경로가 올바르게 등록됨
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
# 콘솔 인코딩 UTF-8 강제 (한글 경로 깨짐 방지)
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
chcp 65001 | Out-Null
|
||||
|
||||
$PROJECT = "C:\Users\whdwo\OneDrive\바탕 화면\stockbot_v3"
|
||||
$weekdays = @(
|
||||
[DayOfWeek]::Monday, [DayOfWeek]::Tuesday, [DayOfWeek]::Wednesday,
|
||||
[DayOfWeek]::Thursday, [DayOfWeek]::Friday
|
||||
$Project = Split-Path -Parent $PSScriptRoot
|
||||
$TaskPath = "\StockBot\"
|
||||
$Weekdays = @(
|
||||
[DayOfWeek]::Monday,
|
||||
[DayOfWeek]::Tuesday,
|
||||
[DayOfWeek]::Wednesday,
|
||||
[DayOfWeek]::Thursday,
|
||||
[DayOfWeek]::Friday
|
||||
)
|
||||
|
||||
function Register-StockTask($name, $time, $script, $limitMin) {
|
||||
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $weekdays -At $time
|
||||
$action = New-ScheduledTaskAction `
|
||||
function Register-StockTask {
|
||||
param(
|
||||
[string]$Name,
|
||||
[string]$Time,
|
||||
[string]$Script,
|
||||
[int]$LimitMinutes
|
||||
)
|
||||
|
||||
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $Weekdays -At $Time
|
||||
$Action = New-ScheduledTaskAction `
|
||||
-Execute "powershell.exe" `
|
||||
-Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$PROJECT\scripts\$script`"" `
|
||||
-WorkingDirectory $PROJECT
|
||||
$settings = New-ScheduledTaskSettingsSet `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes $limitMin) `
|
||||
-Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$Project\scripts\$Script`"" `
|
||||
-WorkingDirectory $Project
|
||||
$Settings = New-ScheduledTaskSettingsSet `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes $LimitMinutes) `
|
||||
-StartWhenAvailable `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-RunOnlyIfNetworkAvailable:$false
|
||||
$settings.DisallowStartIfOnBatteries = $false # 배터리 시작 제한 해제
|
||||
Register-ScheduledTask -TaskName $name -TaskPath "\StockBot\" `
|
||||
-Trigger $trigger -Action $action -Settings $settings -RunLevel Limited -Force | Out-Null
|
||||
$Settings.DisallowStartIfOnBatteries = $false
|
||||
|
||||
# PowerShell 5.1 버그: Register-ScheduledTask가 한글 경로를 ANSI로 저장
|
||||
# → XML export → 경로 수정 → 재import로 교정
|
||||
$xml = Export-ScheduledTask -TaskName $name -TaskPath "\StockBot\"
|
||||
$stored = (Get-ScheduledTask -TaskName $name -TaskPath "\StockBot\").Actions[0].Arguments
|
||||
if (-not ($stored -match [regex]::Escape("바탕 화면"))) {
|
||||
$garbled = ($stored -replace '.*OneDrive\\(.+?)\\stockbot.*', '$1')
|
||||
if ($garbled -ne $stored) {
|
||||
$fixedXml = $xml.Replace($garbled, "바탕 화면")
|
||||
Register-ScheduledTask -TaskName $name -TaskPath "\StockBot\" -Xml $fixedXml -Force | Out-Null
|
||||
}
|
||||
}
|
||||
Write-Host "[OK] $name 등록 완료 (평일 $time)" -ForegroundColor Green
|
||||
Register-ScheduledTask `
|
||||
-TaskName $Name `
|
||||
-TaskPath $TaskPath `
|
||||
-Trigger $Trigger `
|
||||
-Action $Action `
|
||||
-Settings $Settings `
|
||||
-RunLevel Limited `
|
||||
-Force | Out-Null
|
||||
|
||||
Write-Host "[OK] $Name registered at $Time" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# ── 태스크 등록 ────────────────────────────────────────────────────────────────
|
||||
# 08:15 claude /morning → 뉴스+KIS 수집 → daily_context.json → /start-bot 호출
|
||||
Register-StockTask "StockBot_Morning" "08:15" "run_morning.ps1" 20
|
||||
|
||||
# 11:20 claude /midday → 장중 스냅샷 → midday_context.json → 점심 세션 시작
|
||||
Register-StockTask "StockBot_Midday" "11:20" "run_midday.ps1" 20
|
||||
|
||||
# 15:30 claude /evening → 결과 분석 → 리포트 → Discord
|
||||
Register-StockTask "StockBot_Evening" "15:30" "run_evening.ps1" 30
|
||||
|
||||
# 워치독: 5분마다 봇 생존 확인 → 죽어있으면 Discord 알림 + 자동 재시작
|
||||
function Register-WatchdogTask {
|
||||
$times = @("09:00","09:05","09:10","09:15","09:20","09:25","09:30","09:35","09:40","09:45","09:50","09:55",
|
||||
"10:00","10:05","10:10","10:15","10:20","10:25","10:30","10:35","10:40","10:45","10:50","10:55",
|
||||
"11:00","11:05","11:10","11:15","11:20","11:25","11:30","11:35","11:40","11:45","11:50","11:55",
|
||||
"12:00","12:05","12:10","12:15","12:20","12:25","12:30","12:35","12:40","12:45","12:50","12:55",
|
||||
"13:00","13:05","13:10","13:15","13:20","13:25","13:30","13:35","13:40","13:45","13:50","13:55",
|
||||
"14:00","14:05","14:10","14:15","14:20","14:25","14:30","14:35","14:40","14:45","14:50","14:55",
|
||||
"15:00","15:05","15:10")
|
||||
$triggers = $times | ForEach-Object {
|
||||
New-ScheduledTaskTrigger -Weekly -DaysOfWeek $weekdays -At $_
|
||||
$TaskName = "\StockBot\StockBot_Watchdog"
|
||||
$ScriptPath = Join-Path $Project "scripts\run_watchdog.ps1"
|
||||
$Command = 'schtasks /Create /TN "\StockBot\StockBot_Watchdog" /TR "\"powershell.exe\" -NonInteractive -ExecutionPolicy Bypass -File \"' + $ScriptPath + '\"" /SC MINUTE /MO 5 /ST 09:00 /ET 15:10 /F'
|
||||
cmd.exe /c $Command | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "StockBot_Watchdog registration failed"
|
||||
}
|
||||
$action = New-ScheduledTaskAction `
|
||||
-Execute "powershell.exe" `
|
||||
-Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$PROJECT\scripts\run_watchdog.ps1`"" `
|
||||
-WorkingDirectory $PROJECT
|
||||
$settings = New-ScheduledTaskSettingsSet `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes 3) `
|
||||
-StartWhenAvailable `
|
||||
-MultipleInstances IgnoreNew `
|
||||
-DontStopIfGoingOnBatteries
|
||||
$settings.DisallowStartIfOnBatteries = $false
|
||||
Register-ScheduledTask -TaskName "StockBot_Watchdog" -TaskPath "\StockBot\" `
|
||||
-Trigger $triggers -Action $action -Settings $settings -RunLevel Limited -Force | Out-Null
|
||||
Write-Host "[OK] StockBot_Watchdog 등록 완료 (평일 09:00~15:10, 5분 간격)" -ForegroundColor Green
|
||||
|
||||
Write-Host "[OK] StockBot_Watchdog registered at 09:00-15:10 every 5 minutes" -ForegroundColor Green
|
||||
}
|
||||
|
||||
Register-StockTask "StockBot_Morning" "08:15" "run_morning.ps1" 20
|
||||
Register-StockTask "StockBot_Midday" "11:20" "run_midday.ps1" 20
|
||||
Register-StockTask "StockBot_Evening" "15:30" "run_evening.ps1" 30
|
||||
Register-StockTask "StockBot_Training" "16:00" "run_training_pipeline.ps1" 60
|
||||
Register-WatchdogTask
|
||||
|
||||
# StockBot_Bot 비활성화 유지 (이미 존재할 경우)
|
||||
$botTask = Get-ScheduledTask -TaskName "StockBot_Bot" -TaskPath "\StockBot\" -ErrorAction SilentlyContinue
|
||||
if ($botTask) {
|
||||
Disable-ScheduledTask -TaskName "StockBot_Bot" -TaskPath "\StockBot\" | Out-Null
|
||||
Write-Host "[OK] StockBot_Bot 비활성화 유지" -ForegroundColor Yellow
|
||||
$BotTask = Get-ScheduledTask -TaskName "StockBot_Bot" -TaskPath $TaskPath -ErrorAction SilentlyContinue
|
||||
if ($BotTask) {
|
||||
Disable-ScheduledTask -TaskName "StockBot_Bot" -TaskPath $TaskPath | Out-Null
|
||||
Write-Host "[OK] StockBot_Bot disabled" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "등록된 작업:" -ForegroundColor Cyan
|
||||
Get-ScheduledTask -TaskPath "\StockBot\" | Format-Table TaskName, State
|
||||
Write-Host "Registered StockBot tasks:" -ForegroundColor Cyan
|
||||
Get-ScheduledTask -TaskPath $TaskPath | Format-Table TaskName, State
|
||||
|
||||
Reference in New Issue
Block a user