Skip to content

Commit

Permalink
#49 기능 구현 - 병렬 루프 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rkttu committed Feb 6, 2024
1 parent 50cf11e commit acf623f
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions src/TableCloth.CatalogBuilder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.IO.Compression;
using System.Xml.Schema;
using System.Xml;
using BitMiracle.LibTiff.Classic;
using System.Data.SqlTypes;
using System.Reflection.PortableExecutable;

AppMain(args);

Expand Down Expand Up @@ -55,9 +58,10 @@ static void AppMain(string[] args)
return positionText;
}

const string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.3";

static void ValidatingCatalogSchemaFile(string targetDirectory)
{
const string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36";
var catalogXmlPath = Path.Combine(targetDirectory, "Catalog.xml");
var schemaXsdPath = Path.Combine(targetDirectory, "Catalog.xsd");

Expand Down Expand Up @@ -100,7 +104,7 @@ static void ValidatingCatalogSchemaFile(string targetDirectory)
}

Console.Out.WriteLine($"Info: Validating `{catalogXmlPath}` file.");
var testedUrl = new Dictionary<string, bool>();
var testTargets = new Dictionary<string, Tuple<string, int>>();

// Get the XmlReader object with the configured settings.
using (XmlReader reader = XmlReader.Create(catalogXmlPath, config))
Expand Down Expand Up @@ -135,29 +139,24 @@ static void ValidatingCatalogSchemaFile(string targetDirectory)
continue;
}

if (testedUrl.ContainsKey(urlString))
if (testTargets.ContainsKey(urlString))
{
Console.Out.WriteLine($"Skipping visited `{reader.Name}` URL `{urlString}`...");
continue;
}

var response = httpClient.GetAsync(result, HttpCompletionOption.ResponseHeadersRead).Result;

if (response == null || !response.IsSuccessStatusCode)
{
testedUrl[urlString] = false;
errorCount++;
Console.Out.WriteLine($"Warning: Cannot fetch the URI `{urlString}` (Remote response code: {(int?)response?.StatusCode}) - {GetPositionInfo(reader as IXmlLineInfo)}");
continue;
}
else
{
Console.Out.WriteLine($"Test succeed on `{reader.Name}` URL `{urlString}`...");
testedUrl[urlString] = true;
}
var lineInfo = reader as IXmlLineInfo;
testTargets[urlString] = new Tuple<string, int>(reader.Name, lineInfo?.LineNumber ?? 0);
break;
}
}
catch (AggregateException aex)
{
errorCount++;
var ex = aex.InnerException ?? aex;
Console.Error.WriteLine($"Error: {ex.GetType().Name} / {ex.InnerException?.GetType().Name} throwed. ({ex.InnerException?.Message ?? ex.Message}) - {GetPositionInfo(reader as IXmlLineInfo)}");
continue;
}
catch (Exception ex)
{
errorCount++;
Expand All @@ -167,6 +166,35 @@ static void ValidatingCatalogSchemaFile(string targetDirectory)
}
}

foreach (var eachTestTarget in testTargets.AsParallel())
{
var urlString = eachTestTarget.Key;

try
{
var response = httpClient.GetAsync(urlString, HttpCompletionOption.ResponseHeadersRead).Result;

if (response == null || !response.IsSuccessStatusCode)
{
errorCount++;
Console.Out.WriteLine($"Warning: Cannot fetch the URI `{urlString}` (Remote response code: {(int?)response?.StatusCode}) - (Line: `{eachTestTarget.Value.Item2}`)");
}
}
catch (AggregateException aex)
{
errorCount++;
var ex = aex.InnerException ?? aex;
Console.Out.WriteLine($"Warning: Cannot fetch the URI `{urlString}` ({ex.GetType().Name} throwed. {ex.InnerException?.Message ?? ex.Message}) - (Line: `{eachTestTarget.Value.Item2}`)");
continue;
}
catch (Exception ex)
{
errorCount++;
Console.Out.WriteLine($"Warning: Cannot fetch the URI `{urlString}` ({ex.GetType().Name} throwed. {ex.InnerException?.Message ?? ex.Message}) - (Line: `{eachTestTarget.Value.Item2}`)");
continue;
}
}

if (errorCount + warningCount < 1)
Console.Out.WriteLine("Success: No XML warnings or errors found.");
else if (errorCount < 1 && warningCount > 0)
Expand Down

0 comments on commit acf623f

Please sign in to comment.