feat(config): Docker公開時・起動時の処理モード(最初から/続きから)切り替え設定ファイル(run_config.json)を追加

This commit is contained in:
armonica committed 2026-09-14 04:28:53 +09:00
1 parent 3c2fffdb68
commit be66964981
5 files changed
+141 -5

No files matched your search

@@ -0,0 +1,110 @@
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MusicDownloader.Class.Downloader
{
/// <summary>
/// Docker公開時・起動時の処理モード(最初から / 続きから)を制御する設定クラス
/// </summary>
public class RunConfig
{
/// <summary>
/// 初回起動時の動作モード: "resume" (続きから / 差分) または "from_scratch" (最初から / 全曲)
/// </summary>
[JsonPropertyName("initial_mode")]
public string InitialMode { get; set; } = "resume";
/// <summary>
/// 読み込み元の設定ファイルパス
/// </summary>
[JsonIgnore]
public string? LoadedPath { get; set; }
/// <summary>
/// 初回処理を続きから行う(差分ダウンロード)かどうかを判定する
/// </summary>
/// <returns>true: 続きから(差分), false: 最初から(全曲再取得)</returns>
public bool IsResumeMode()
{
// 環境変数 INITIAL_DOWNLOAD_MODE による上書きチェック
var envMode = Environment.GetEnvironmentVariable("INITIAL_DOWNLOAD_MODE");
var modeToCheck = !string.IsNullOrWhiteSpace(envMode) ? envMode : InitialMode;
if (string.IsNullOrWhiteSpace(modeToCheck))
{
return true; // デフォルトは続きから
}
var val = modeToCheck.Trim().ToLowerInvariant();
if (val is "from_scratch" or "scratch" or "reset" or "full" or "最初から" or "全曲" or "false")
{
return false;
}
return true;
}
/// <summary>
/// 設定ファイル(run_config.json)を探索して読み込む
/// </summary>
/// <param name="outputDir">ベース出力先ディレクトリ</param>
/// <returns>読み込まれた設定オブジェクト</returns>
public static RunConfig Load(string? outputDir = null)
{
// 探索対象のパス候補
var candidates = new List<string>();
// 1. 環境変数による指定パス
var envPath = Environment.GetEnvironmentVariable("RUN_CONFIG_PATH");
if (!string.IsNullOrWhiteSpace(envPath)) candidates.Add(envPath);
// 2. コンテナ内の /app/run_config.json または実行フォルダ直下
var appDir = AppDomain.CurrentDomain.BaseDirectory;
candidates.Add(Path.Combine(appDir, "run_config.json"));
// 3. /config/run_config.json
candidates.Add("/config/run_config.json");
// 4. 出力フォルダ配下(/music/run_config.json 等)
if (!string.IsNullOrWhiteSpace(outputDir))
{
candidates.Add(Path.Combine(outputDir, "run_config.json"));
}
// 5. ホスト開発環境の既定パス(C:\VS2026\MusicDownloader\run_config.json)
candidates.Add(@"C:\VS2026\MusicDownloader\run_config.json");
foreach (var candidate in candidates)
{
try
{
if (File.Exists(candidate))
{
var json = File.ReadAllText(candidate);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true
};
var config = JsonSerializer.Deserialize<RunConfig>(json, options);
if (config != null)
{
config.LoadedPath = candidate;
return config;
}
}
}
catch (Exception ex)
{
LogManager.AddWarning($"設定ファイル '{candidate}' の読み込み中に例外: {ex.Message}");
}
}
// 見つからなかった場合はデフォルト(続きから)
return new RunConfig { InitialMode = "resume", LoadedPath = null };
}
}
}
+2 -2
View File
@@ -33,8 +33,8 @@
<Exec Command="powershell -Command &quot;if (!(Test-Path 'C:\docker\MusicDownloader\app')) { New-Item -ItemType Directory -Force -Path 'C:\docker\MusicDownloader\app' }&quot;" />
<!-- 発行済みファイルを C:\docker\MusicDownloader\app に同期(Visual Studio発行プロファイル・CLI共通対応) -->
<Exec Command="robocopy &quot;$(PublishDir).&quot; &quot;C:\docker\MusicDownloader\app&quot; /MIR /NP /NFL /NDL" IgnoreExitCode="true" />
<!-- Dockerfile と compose.yaml を最新同期 -->
<Exec Command="powershell -Command &quot;Copy-Item -Path '$(SolutionDir)Dockerfile', '$(SolutionDir)compose.yaml' -Destination 'C:\docker\MusicDownloader\' -Force&quot;" />
<!-- Dockerfile と compose.yaml と run_config.json を最新同期 -->
<Exec Command="powershell -Command &quot;Copy-Item -Path '$(SolutionDir)Dockerfile', '$(SolutionDir)compose.yaml', '$(SolutionDir)run_config.json' -Destination 'C:\docker\MusicDownloader\' -Force&quot;" />
<!-- ローカルDocker Desktopでコンテナをビルド・起動(常駐) -->
<Exec Command="docker compose -f C:\docker\MusicDownloader\compose.yaml up -d --build" />
<Message Text="=== [Local Docker Deploy] All processes completed successfully! ===" Importance="high" />
+22 -3
View File
@@ -194,10 +194,22 @@ int loopCount = 0;
bool isFirstRun = true;
var playlistsFile = Path.Combine(outputDir, "playlists.txt");
// 【設定読み込み】起動時・Docker公開時の初回処理モード(最初から / 続きから)
var runConfig = RunConfig.Load(outputDir);
bool isResumeOnStartup = runConfig.IsResumeMode();
if (!string.IsNullOrEmpty(runConfig.LoadedPath))
{
LogManager.AddLog($"[起動設定検出] 設定ファイル: {runConfig.LoadedPath}");
}
LogManager.AddLog($"[起動時モード判定] 初回処理: {(isResumeOnStartup ? "「続きから行う (差分ダウンロード)」" : "「最初から行う (全曲再ダウンロード)」")}");
while (true)
{
loopCount++;
bool isIncremental = !isFirstRun;
// 初回実行時: 設定ファイルが resume なら差分ダウンロード (true)、from_scratch なら全曲ダウンロード (false)
// 2回目以降 (loopCount > 1): 常に差分ダウンロード (true)
bool isIncremental = isFirstRun ? isResumeOnStartup : true;
var targetPlaylists = ResolveTargetPlaylists(outputDir);
var processedInCycle = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
@@ -205,13 +217,20 @@ while (true)
LogManager.AddLog($"[第 {loopCount} 回実行] 対象プレイリスト総数: {targetPlaylists.Count} 件のチェックを開始します (モード: {(isIncremental ? "差分ダウンロード" : "全曲ダウンロード")})");
LogManager.AddLog($"================================================================================");
// 初回起動時(コンテナ再起動含む)はダウンロード完了状態(archive.txt)をリセットして全曲ダウンロード
// 初回起動時の初期化処理
if (isFirstRun)
{
LogManager.AddLog($"[起動時初期化] 起動・再起動時のため、ダウンロード完了履歴 (archive.txt) をリセットして全曲ダウンロードを行います。");
if (isResumeOnStartup)
{
LogManager.AddSuccess($"[起動時モード: 続きから行う] 設定に基づき、既存のダウンロード履歴 (archive.txt) を保持して続きから差分取得します。");
}
else
{
LogManager.AddLog($"[起動時モード: 最初から行う] 設定に基づき、ダウンロード完了履歴 (archive.txt) をリセットして全曲ダウンロードを行います。");
var youtubeFolder = Path.Combine(outputDir, CmnDefine.YoutubeFolderName);
downloader.ResetDownloadArchive(youtubeFolder);
}
}
for (int p = 0; p < targetPlaylists.Count; p++)
{
+2
View File
@@ -28,6 +28,8 @@ services:
- C:\MyMusic:/music
# ホストのログディレクトリ C:\feeds\Log へ出力するためのマウント
- C:\feeds:/feeds
# 初回起動時の動作設定(最初から行う: from_scratch / 続きから行う: resume)
- C:\VS2026\MusicDownloader\run_config.json:/app/run_config.json:ro
networks:
- common-network
+5
View File
@@ -0,0 +1,5 @@
{
"// 説明": "Docker公開時・起動時の初回処理モード設定(最初から行う: 'from_scratch' / 続きから行う: 'resume')",
"// 選択肢": "'resume' (続きから / 差分ダウンロード - 既存のarchive.txtを保持) または 'from_scratch' (最初から / 全曲再ダウンロード - 起動時にarchive.txtをリセット)",
"initial_mode": "resume"
}