From be669649811a249acbf2b4ec3ede4c050563542a Mon Sep 17 00:00:00 2001 From: uduki Date: Mon, 14 Sep 2026 04:28:53 +0900 Subject: [PATCH] =?UTF-8?q?feat(config):=20Docker=E5=85=AC=E9=96=8B?= =?UTF-8?q?=E6=99=82=E3=83=BB=E8=B5=B7=E5=8B=95=E6=99=82=E3=81=AE=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=83=A2=E3=83=BC=E3=83=89=EF=BC=88=E6=9C=80=E5=88=9D?= =?UTF-8?q?=E3=81=8B=E3=82=89/=E7=B6=9A=E3=81=8D=E3=81=8B=E3=82=89?= =?UTF-8?q?=EF=BC=89=E5=88=87=E3=82=8A=E6=9B=BF=E3=81=88=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB(run=5Fconfig.json)?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MusicDownloader/Class/Downloader/RunConfig.cs | 110 ++++++++++++++++++ MusicDownloader/MusicDownloader.csproj | 4 +- MusicDownloader/Program.cs | 29 ++++- compose.yaml | 2 + run_config.json | 5 + 5 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 MusicDownloader/Class/Downloader/RunConfig.cs create mode 100644 run_config.json diff --git a/MusicDownloader/Class/Downloader/RunConfig.cs b/MusicDownloader/Class/Downloader/RunConfig.cs new file mode 100644 index 0000000..3d07fa8 --- /dev/null +++ b/MusicDownloader/Class/Downloader/RunConfig.cs @@ -0,0 +1,110 @@ +using System; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace MusicDownloader.Class.Downloader +{ + /// + /// Docker公開時・起動時の処理モード(最初から / 続きから)を制御する設定クラス + /// + public class RunConfig + { + /// + /// 初回起動時の動作モード: "resume" (続きから / 差分) または "from_scratch" (最初から / 全曲) + /// + [JsonPropertyName("initial_mode")] + public string InitialMode { get; set; } = "resume"; + + /// + /// 読み込み元の設定ファイルパス + /// + [JsonIgnore] + public string? LoadedPath { get; set; } + + /// + /// 初回処理を続きから行う(差分ダウンロード)かどうかを判定する + /// + /// true: 続きから(差分), false: 最初から(全曲再取得) + 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; + } + + /// + /// 設定ファイル(run_config.json)を探索して読み込む + /// + /// ベース出力先ディレクトリ + /// 読み込まれた設定オブジェクト + public static RunConfig Load(string? outputDir = null) + { + // 探索対象のパス候補 + var candidates = new List(); + + // 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(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 }; + } + } +} diff --git a/MusicDownloader/MusicDownloader.csproj b/MusicDownloader/MusicDownloader.csproj index 9b67175..f2f002b 100644 --- a/MusicDownloader/MusicDownloader.csproj +++ b/MusicDownloader/MusicDownloader.csproj @@ -33,8 +33,8 @@ - - + + diff --git a/MusicDownloader/Program.cs b/MusicDownloader/Program.cs index 1767f4e..9397e87 100644 --- a/MusicDownloader/Program.cs +++ b/MusicDownloader/Program.cs @@ -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(StringComparer.OrdinalIgnoreCase); @@ -205,12 +217,19 @@ while (true) LogManager.AddLog($"[第 {loopCount} 回実行] 対象プレイリスト総数: {targetPlaylists.Count} 件のチェックを開始します (モード: {(isIncremental ? "差分ダウンロード" : "全曲ダウンロード")})"); LogManager.AddLog($"================================================================================"); - // 初回起動時(コンテナ再起動含む)はダウンロード完了状態(archive.txt)をリセットして全曲ダウンロード + // 初回起動時の初期化処理 if (isFirstRun) { - LogManager.AddLog($"[起動時初期化] 起動・再起動時のため、ダウンロード完了履歴 (archive.txt) をリセットして全曲ダウンロードを行います。"); - var youtubeFolder = Path.Combine(outputDir, CmnDefine.YoutubeFolderName); - downloader.ResetDownloadArchive(youtubeFolder); + 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++) diff --git a/compose.yaml b/compose.yaml index f450089..75ab36f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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 diff --git a/run_config.json b/run_config.json new file mode 100644 index 0000000..43f5e6b --- /dev/null +++ b/run_config.json @@ -0,0 +1,5 @@ +{ + "// 説明": "Docker公開時・起動時の初回処理モード設定(最初から行う: 'from_scratch' / 続きから行う: 'resume')", + "// 選択肢": "'resume' (続きから / 差分ダウンロード - 既存のarchive.txtを保持) または 'from_scratch' (最初から / 全曲再ダウンロード - 起動時にarchive.txtをリセット)", + "initial_mode": "resume" +}