feat: ストレージ障害検知を強化(IsStorageFailureException 追加)
This commit is contained in:
1 parent
6bc2d065e4
commit
8f6ab47363
3 files changed
+114
-31
No files matched your search
@@ -488,9 +488,47 @@ namespace MusicDownloader.Class
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 例外がストレージ障害(Google Drive切断、WSL2マウント切れ、読み取り専用化、I/Oエラーなど)に起因するか判定する
|
||||||
|
/// </summary>
|
||||||
|
public static bool IsStorageFailureException(Exception? ex)
|
||||||
|
{
|
||||||
|
if (ex == null) return false;
|
||||||
|
|
||||||
|
var msg = ex.ToString();
|
||||||
|
|
||||||
|
// Linux/WSL2/DrvFs/Docker でマウント先が切断・無効になった際のエラーメッセージ・条件
|
||||||
|
if (msg.Contains("Invalid argument", StringComparison.OrdinalIgnoreCase) || // errno 22: EINVAL (マウント切断・ジャンクション破損時に発生)
|
||||||
|
msg.Contains("Read-only", StringComparison.OrdinalIgnoreCase) || // EROFS
|
||||||
|
msg.Contains("Input/output error", StringComparison.OrdinalIgnoreCase) || // errno 5: EIO
|
||||||
|
msg.Contains("Transport endpoint is not connected", StringComparison.OrdinalIgnoreCase) || // errno 107: ENOTCONN
|
||||||
|
msg.Contains("Stale file handle", StringComparison.OrdinalIgnoreCase) || // errno 116: ESTALE
|
||||||
|
msg.Contains("No such device", StringComparison.OrdinalIgnoreCase) || // ENODEV
|
||||||
|
msg.Contains("Host is down", StringComparison.OrdinalIgnoreCase) || // EHOSTDOWN
|
||||||
|
msg.Contains("Network is down", StringComparison.OrdinalIgnoreCase) || // ENETDOWN
|
||||||
|
msg.Contains("Connection reset", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
msg.Contains("Device not configured", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ex is DriveNotFoundException || ex is DirectoryNotFoundException)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 内部例外も再帰的に確認
|
||||||
|
if (ex.InnerException != null && IsStorageFailureException(ex.InnerException))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 指定されたストレージディレクトリへの書き込みが可能か(読み取り専用や切断になっていないか)を検証する。
|
/// 指定されたストレージディレクトリへの書き込みが可能か(読み取り専用や切断になっていないか)を検証する。
|
||||||
/// Google Drive や WSL2 DrvFs マウント切断時に発生する Read-only file system を早期検知する。
|
/// Google Drive や WSL2 DrvFs マウント切断時に発生する Read-only / Invalid argument などの障害を早期検知する。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dirPath">検証対象ディレクトリパス</param>
|
/// <param name="dirPath">検証対象ディレクトリパス</param>
|
||||||
/// <param name="maxRetries">リトライ回数(既定3回)</param>
|
/// <param name="maxRetries">リトライ回数(既定3回)</param>
|
||||||
@@ -516,7 +554,7 @@ namespace MusicDownloader.Class
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
bool isReadOnly = ex.Message.Contains("Read-only", StringComparison.OrdinalIgnoreCase);
|
bool isStorageFail = IsStorageFailureException(ex);
|
||||||
if (attempt < maxRetries)
|
if (attempt < maxRetries)
|
||||||
{
|
{
|
||||||
LogManager.AddWarning($"[ストレージ疎通確認] ディレクトリ書き込みテストに失敗しました (試行 {attempt}/{maxRetries}): {dirPath} - {ex.Message}。{retryDelayMs / 1000}秒後に再試行します...");
|
LogManager.AddWarning($"[ストレージ疎通確認] ディレクトリ書き込みテストに失敗しました (試行 {attempt}/{maxRetries}): {dirPath} - {ex.Message}。{retryDelayMs / 1000}秒後に再試行します...");
|
||||||
@@ -524,9 +562,9 @@ namespace MusicDownloader.Class
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (isReadOnly)
|
if (isStorageFail)
|
||||||
{
|
{
|
||||||
LogManager.AddError($"[ストレージ疎通異常] 出力先が読み取り専用 (Read-only) になっています: {dirPath} - Google Drive (G:) のマウント切断またはリンク切れの可能性があります。", ex);
|
LogManager.AddError($"[ストレージ疎通異常] 出力先ストレージが切断・無効化または読み取り専用になっています: {dirPath} - Google Drive (G:) のマウント切断またはリンク切れの可能性があります ({ex.Message})。", ex);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -541,43 +541,82 @@ namespace MusicDownloader.Class.Downloader
|
|||||||
var sanitizedBaseName = CmnUtil.SanitizeFileName(rawBaseName);
|
var sanitizedBaseName = CmnUtil.SanitizeFileName(rawBaseName);
|
||||||
var cleanFileName = $"{sanitizedBaseName}{ext}";
|
var cleanFileName = $"{sanitizedBaseName}{ext}";
|
||||||
|
|
||||||
// ① 同名別動画(another vocal等)の衝突を検知し、ユニークな保存名(曲名 (2).opus等)を決定
|
// 保存先へのファイル転送を安全に試行(Google Drive切断・マウント一時切れ時の自動待機&リトライ付き)
|
||||||
var finalFileName = GetUniqueTargetFileName(youtubeFolder, cleanFileName, videoId, downloadedTracks);
|
bool transferSuccess = false;
|
||||||
var destOpusPath = Path.Combine(youtubeFolder, finalFileName);
|
string targetFinalFileName = cleanFileName;
|
||||||
|
const int maxTransferRetries = 6; // 15秒 x 6回 = 最大90秒間待機
|
||||||
|
const int transferRetryDelayMs = 15000;
|
||||||
|
|
||||||
try
|
for (int attempt = 1; attempt <= maxTransferRetries; attempt++)
|
||||||
{
|
{
|
||||||
File.Copy(targetOpus, destOpusPath, overwrite: true);
|
try
|
||||||
LogManager.AddLog($"ファイルを保存先に配置しました: {finalFileName}");
|
|
||||||
|
|
||||||
// 歌詞ファイル (.lrc) も完成していれば転送(音声ファイル名に合わせた名前にする)
|
|
||||||
var lrcPath = Path.ChangeExtension(targetOpus, ".lrc");
|
|
||||||
if (File.Exists(lrcPath))
|
|
||||||
{
|
{
|
||||||
var targetLrcName = Path.ChangeExtension(finalFileName, ".lrc");
|
// ① 同名別動画(another vocal等)の衝突を検知し、ユニークな保存名(曲名 (2).opus等)を決定
|
||||||
var destLrcPath = Path.Combine(youtubeFolder, targetLrcName);
|
var finalFileName = GetUniqueTargetFileName(youtubeFolder, cleanFileName, videoId, downloadedTracks);
|
||||||
File.Copy(lrcPath, destLrcPath, overwrite: true);
|
var destOpusPath = Path.Combine(youtubeFolder, finalFileName);
|
||||||
|
|
||||||
|
File.Copy(targetOpus, destOpusPath, overwrite: true);
|
||||||
|
LogManager.AddLog($"ファイルを保存先に配置しました: {finalFileName}");
|
||||||
|
|
||||||
|
// 歌詞ファイル (.lrc) も完成していれば転送(音声ファイル名に合わせた名前にする)
|
||||||
|
var lrcPath = Path.ChangeExtension(targetOpus, ".lrc");
|
||||||
|
if (File.Exists(lrcPath))
|
||||||
|
{
|
||||||
|
var targetLrcName = Path.ChangeExtension(finalFileName, ".lrc");
|
||||||
|
var destLrcPath = Path.Combine(youtubeFolder, targetLrcName);
|
||||||
|
File.Copy(lrcPath, destLrcPath, overwrite: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ダウンロード完了履歴 (archive.txt) に動画IDを記録
|
||||||
|
AddToArchiveFile(archiveFile, videoId);
|
||||||
|
|
||||||
|
transferSuccess = true;
|
||||||
|
targetFinalFileName = finalFileName;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
bool isStorageFail = CmnUtil.IsStorageFailureException(ex);
|
||||||
|
if (isStorageFail)
|
||||||
|
{
|
||||||
|
LogManager.AddWarning($"[保存先転送待機] Google Drive/ストレージ切断を検知しました (試行 {attempt}/{maxTransferRetries}): {ex.Message}。{transferRetryDelayMs / 1000}秒後に再試行します...");
|
||||||
|
if (attempt < maxTransferRetries)
|
||||||
|
{
|
||||||
|
await Task.Delay(transferRetryDelayMs);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogManager.AddError($"[ストレージ切断エラー] Google Driveへのファイル転送が復旧しませんでした ({attempt}回試行): {ex.Message}", ex);
|
||||||
|
// 致命的ストレージ切断例外を上位にスローして後続曲の誤完了・破棄ループを即座に停止
|
||||||
|
throw new IOException($"Google Driveストレージ切断のため処理を安全に中断します: {ex.Message}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 通常のエラー(権限不足や特定ファイル名不整合等)
|
||||||
|
LogManager.AddError($"保存先へのファイル転送に失敗しました: {ex.Message}", ex);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ダウンロード完了履歴 (archive.txt) に動画IDを記録
|
// 転送が失敗した場合は、絶対に成功扱いせず作業フォルダを掃除して処理を中断
|
||||||
AddToArchiveFile(archiveFile, videoId);
|
if (!transferSuccess)
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
LogManager.AddError($"保存先へのファイル転送に失敗しました: {ex.Message}", ex);
|
RemoveFromArchiveFile(archiveFile, videoId);
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
// 作業フォルダ内の完成ファイルを掃除
|
|
||||||
CleanupDirectory(stagingDir);
|
CleanupDirectory(stagingDir);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogManager.AddSuccess($"[{index}/{totalCount}] 完了: {finalFileName}");
|
// 転送成功時のみ作業フォルダを掃除し、完了ログを出力
|
||||||
|
CleanupDirectory(stagingDir);
|
||||||
|
LogManager.AddSuccess($"[{index}/{totalCount}] 完了: {targetFinalFileName}");
|
||||||
|
|
||||||
return new DownloadedTrackInfo
|
return new DownloadedTrackInfo
|
||||||
{
|
{
|
||||||
VideoId = videoId,
|
VideoId = videoId,
|
||||||
FileName = finalFileName,
|
FileName = targetFinalFileName,
|
||||||
Title = finalTitle,
|
Title = finalTitle,
|
||||||
Artist = finalArtist,
|
Artist = finalArtist,
|
||||||
Duration = entryInfo?.Duration,
|
Duration = entryInfo?.Duration,
|
||||||
@@ -1072,6 +1111,11 @@ namespace MusicDownloader.Class.Downloader
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LogManager.AddWarning($"downloaded_tracks.json の保存に失敗しました: {ex.Message}");
|
LogManager.AddWarning($"downloaded_tracks.json の保存に失敗しました: {ex.Message}");
|
||||||
|
if (CmnUtil.IsStorageFailureException(ex))
|
||||||
|
{
|
||||||
|
// ストレージ切断時は上位へスローしてプロセス終了・自己復旧へ促す
|
||||||
|
throw new IOException($"downloaded_tracks.json 保存中にストレージ切断を検知しました: {ex.Message}", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -284,10 +284,11 @@ while (true)
|
|||||||
{
|
{
|
||||||
LogManager.AddError($"プレイリスト処理中にエラーが発生しました: {ex.Message}", ex);
|
LogManager.AddError($"プレイリスト処理中にエラーが発生しました: {ex.Message}", ex);
|
||||||
|
|
||||||
// ストレージ切断や読み取り専用 (Read-only) が発生した場合はループを継続せずプロセスを終了し、コンテナの自動再起動に委ねる
|
// ストレージ切断(Invalid argument, Read-only, I/O error 等)が発生した場合はループを継続せずプロセスを終了し、コンテナの自動再起動に委ねる
|
||||||
if (ex.ToString().Contains("Read-only", StringComparison.OrdinalIgnoreCase))
|
if (CmnUtil.IsStorageFailureException(ex))
|
||||||
{
|
{
|
||||||
LogManager.AddError($"ストレージの読み取り専用化 (Read-only) を検知しました。残りのプレイリスト処理を中止し、コンテナ自動再起動による自己復旧のためプロセスを終了します。");
|
LogManager.AddError($"[ストレージ障害検知] 出力先ストレージ(Google Drive / WSL2マウント)の切断または障害を検知しました: {ex.Message}");
|
||||||
|
LogManager.AddError($"残りのプレイリスト処理を安全に中断し、コンテナ自動再起動による自己復旧のためプロセスを終了します。");
|
||||||
Environment.Exit(1);
|
Environment.Exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user