Make retry count into a field, sync clients

This commit is contained in:
Matt Nadareski
2024-06-27 00:05:53 -04:00
parent 93873ea204
commit ec045448c5
2 changed files with 81 additions and 123 deletions

View File

@@ -28,6 +28,11 @@ namespace SabreTools.RedumpLib.Web
/// </summary>
public bool IsStaff { get; private set; } = false;
/// <summary>
/// Maximum retry count for any operation
/// </summary>
public int RetryCount { get; private set; } = 3;
#endregion
/// <summary>
@@ -152,8 +157,8 @@ namespace SabreTools.RedumpLib.Web
{
List<int> ids = [];
// Try up to 3 times to retrieve the data
string? dumpsPage = await DownloadString(url, retries: 3);
// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
@@ -200,8 +205,8 @@ namespace SabreTools.RedumpLib.Web
/// <returns>True if the page could be downloaded, false otherwise</returns>
public async Task<bool> CheckSingleSitePage(string url, string? outDir, bool failOnSingle)
{
// Try up to 3 times to retrieve the data
string? dumpsPage = await DownloadString(url, retries: 3);
// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
@@ -256,8 +261,8 @@ namespace SabreTools.RedumpLib.Web
{
List<int> ids = [];
// Try up to 3 times to retrieve the data
string? dumpsPage = await DownloadString(url, retries: 3);
// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
@@ -294,8 +299,8 @@ namespace SabreTools.RedumpLib.Web
/// <returns>True if the page could be downloaded, false otherwise</returns>
public async Task<bool> CheckSingleWIPPage(string url, string? outDir, bool failOnSingle)
{
// Try up to 3 times to retrieve the data
string? dumpsPage = await DownloadString(url, retries: 3);
// Try to retrieve the data
string? dumpsPage = await DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
@@ -391,9 +396,9 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
// Try up to 3 times to retrieve the data
// Try to retrieve the data
string discPageUri = string.Format(Constants.DiscPageUrl, +id);
string? discPage = await DownloadString(discPageUri, retries: 3);
string? discPage = await DownloadStringWithRetries(discPageUri);
if (discPage == null || discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
{
@@ -429,9 +434,9 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
// Try up to 3 times to retrieve the data
// Try to retrieve the data
string discPageUri = string.Format(Constants.DiscPageUrl, +id);
string? discPage = await DownloadString(discPageUri, retries: 3);
string? discPage = await DownloadStringWithRetries(discPageUri);
if (discPage == null || discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
{
@@ -553,9 +558,9 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
// Try up to 3 times to retrieve the data
// Try to retrieve the data
string discPageUri = string.Format(Constants.WipDiscPageUrl, +id);
string? discPage = await DownloadString(discPageUri, retries: 3);
string? discPage = await DownloadStringWithRetries(discPageUri);
if (discPage == null || discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
{
@@ -591,9 +596,9 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
// Try up to 3 times to retrieve the data
// Try to retrieve the data
string discPageUri = string.Format(Constants.WipDiscPageUrl, +id);
string? discPage = await DownloadString(discPageUri, retries: 3);
string? discPage = await DownloadStringWithRetries(discPageUri);
if (discPage == null || discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
{
@@ -764,15 +769,14 @@ namespace SabreTools.RedumpLib.Web
/// Download from a URI to a string
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <param name="retries">Number of times to retry on error</param>
/// <returns>String from the URI, null on error</returns>
private async Task<string?> DownloadString(string uri, int retries = 3)
private async Task<string?> DownloadStringWithRetries(string uri)
{
// Only retry a positive number of times
if (retries <= 0)
if (RetryCount <= 0)
return null;
for (int i = 0; i < retries; i++)
for (int i = 0; i < RetryCount; i++)
{
try
{

View File

@@ -25,6 +25,11 @@ namespace SabreTools.RedumpLib.Web
/// </summary>
public bool IsStaff { get; private set; } = false;
/// <summary>
/// Maximum retry count for any operation
/// </summary>
public int RetryCount { get; private set; } = 3;
/// <summary>
/// Get the last downloaded filename, if possible
/// </summary>
@@ -111,7 +116,7 @@ namespace SabreTools.RedumpLib.Web
try
{
// Get the current token from the login page
var loginPage = DownloadString(Constants.LoginUrl);
var loginPage = DownloadStringWithRetries(Constants.LoginUrl);
string token = Constants.TokenRegex.Match(loginPage).Groups[1].Value;
// Construct the login request
@@ -155,21 +160,12 @@ namespace SabreTools.RedumpLib.Web
public List<int> CheckSingleSitePage(string url)
{
List<int> ids = [];
string dumpsPage = string.Empty;
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
dumpsPage = DownloadString(url);
break;
}
catch { }
}
// Try to retrieve the data
string? dumpsPage = DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage.Contains("No discs found."))
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
return ids;
// If we have a single disc page already
@@ -210,21 +206,11 @@ namespace SabreTools.RedumpLib.Web
/// <returns>True if the page could be downloaded, false otherwise</returns>
public bool CheckSingleSitePage(string url, string? outDir, bool failOnSingle)
{
string dumpsPage = string.Empty;
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
dumpsPage = DownloadString(url);
break;
}
catch { }
}
// Try to retrieve the data
string? dumpsPage = DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage.Contains("No discs found."))
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
return false;
// If we have a single disc page already
@@ -272,21 +258,12 @@ namespace SabreTools.RedumpLib.Web
public List<int> CheckSingleWIPPage(string url)
{
List<int> ids = [];
string dumpsPage = string.Empty;
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
dumpsPage = DownloadString(url);
break;
}
catch { }
}
// Try to retrieve the data
string? dumpsPage = DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage.Contains("No discs found."))
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
return ids;
// Otherwise, traverse each dump on the page
@@ -317,21 +294,11 @@ namespace SabreTools.RedumpLib.Web
/// <returns>True if the page could be downloaded, false otherwise</returns>
public bool CheckSingleWIPPage(string url, string? outDir, bool failOnSingle)
{
string dumpsPage = string.Empty;
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
dumpsPage = DownloadString(url);
break;
}
catch { }
}
// Try to retrieve the data
string? dumpsPage = DownloadStringWithRetries(url);
// If we have no dumps left
if (dumpsPage.Contains("No discs found."))
if (dumpsPage == null || dumpsPage.Contains("No discs found."))
return false;
// Otherwise, traverse each dump on the page
@@ -416,20 +383,11 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
string discPage = string.Empty;
// Try to retrieve the data
string discPageUri = string.Format(Constants.DiscPageUrl, +id);
string? discPage = DownloadStringWithRetries(discPageUri);
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
discPage = DownloadString(string.Format(Constants.DiscPageUrl, +id));
break;
}
catch { }
}
if (discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
if (discPage == null || discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
{
Console.WriteLine($"ID {paddedId} could not be found!");
return null;
@@ -463,20 +421,11 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
string discPage = string.Empty;
// Try to retrieve the data
string discPageUri = string.Format(Constants.DiscPageUrl, +id);
string? discPage = DownloadStringWithRetries(discPageUri);
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
discPage = DownloadString(string.Format(Constants.DiscPageUrl, +id));
break;
}
catch { }
}
if (discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
if (discPage == null || discPage.Contains($"Disc with ID \"{id}\" doesn't exist"))
{
try
{
@@ -596,20 +545,11 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
string discPage = string.Empty;
// Try to retrieve the data
string discPageUri = string.Format(Constants.WipDiscPageUrl, +id);
string? discPage = DownloadStringWithRetries(discPageUri);
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
discPage = DownloadString(string.Format(Constants.WipDiscPageUrl, +id));
break;
}
catch { }
}
if (discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
if (discPage == null || discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
{
Console.WriteLine($"ID {paddedId} could not be found!");
return null;
@@ -643,20 +583,11 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine($"Processing ID: {paddedId}");
try
{
string discPage = string.Empty;
// Try to retrieve the data
string discPageUri = string.Format(Constants.WipDiscPageUrl, +id);
string? discPage = DownloadStringWithRetries(discPageUri);
// Try up to 3 times to retrieve the data
for (int i = 0; i < 3; i++)
{
try
{
discPage = DownloadString(string.Format(Constants.WipDiscPageUrl, +id));
break;
}
catch { }
}
if (discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
if (discPage == null || discPage.Contains($"WIP disc with ID \"{id}\" doesn't exist"))
{
try
{
@@ -794,6 +725,29 @@ namespace SabreTools.RedumpLib.Web
Console.WriteLine();
}
/// <summary>
/// Download from a URI to a string
/// </summary>
/// <param name="uri">Remote URI to retrieve</param>
/// <returns>String from the URI, null on error</returns>
private string? DownloadStringWithRetries(string uri)
{
// Only retry a positive number of times
if (RetryCount <= 0)
return null;
for (int i = 0; i < RetryCount; i++)
{
try
{
return DownloadString(uri);
}
catch { }
}
return null;
}
/// <summary>
/// Move a tempfile to a new name unless it aleady exists, in which case, delete the tempfile
/// </summary>