make it more parallel

This commit is contained in:
Gervasio Marchand 2022-12-05 08:56:11 -03:00
parent 54828b9469
commit 28fe45a075
No known key found for this signature in database
GPG Key ID: B7736CB188DD0A38

View File

@ -17,21 +17,26 @@ if (!File.Exists(importedPath))
File.WriteAllText(importedPath, ""); File.WriteAllText(importedPath, "");
} }
var imported = File.ReadAllLines(importedPath).ToHashSet(); var importedList = File.ReadAllLines(importedPath).ToList();
var imported = importedList.ToHashSet();
var statusesToLoadBag = new ConcurrentBag<string>(); var statusesToLoadBag = new ConcurrentBag<string>();
var sitesTags = Config.Instance.Sites
.SelectMany(s => Config.Instance.Tags.Select(tag => (s.Host, tag)))
.Concat(Config.Instance.Sites.SelectMany(s => s.SiteSpecificTags.Select(tag => (s.Host, tag))))
.OrderBy(t => t.tag)
.ToList();
ParallelOptions parallelOptions = new() ParallelOptions parallelOptions = new()
{ {
MaxDegreeOfParallelism = 8 MaxDegreeOfParallelism = 8
}; };
await Parallel.ForEachAsync(Config.Instance.Sites, parallelOptions, async (site, _) => await Parallel.ForEachAsync(sitesTags, parallelOptions, async (st, _) =>
{ {
var tags = site.SiteSpecificTags.Concat(Config.Instance.Tags).ToList(); var (site, tag) = st;
foreach (var tag in tags) Console.WriteLine($"Fetching tag #{tag} from {site}");
{ var response = await client.GetAsync($"https://{site}/tags/{tag}.json");
Console.WriteLine($"Fetching tag #{tag} from {site.Host}");
var response = await client.GetAsync($"https://{site.Host}/tags/{tag}.json");
try try
{ {
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
@ -39,7 +44,7 @@ await Parallel.ForEachAsync(Config.Instance.Sites, parallelOptions, async (site,
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"Error fetching tag, status code: {response.StatusCode}. Error: {e.Message}"); Console.WriteLine($"Error fetching tag, status code: {response.StatusCode}. Error: {e.Message}");
continue; return;
} }
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
@ -49,11 +54,9 @@ await Parallel.ForEachAsync(Config.Instance.Sites, parallelOptions, async (site,
{ {
statusesToLoadBag.Add(statusLink); statusesToLoadBag.Add(statusLink);
} }
}
}); });
var statusesToLoad = statusesToLoadBag.ToHashSet(); var statusesToLoad = statusesToLoadBag.ToHashSet();
var importedOnThisRun = new List<string>();
foreach (var statusLink in statusesToLoad) foreach (var statusLink in statusesToLoad)
{ {
Console.WriteLine($"Bringing in {statusLink}"); Console.WriteLine($"Bringing in {statusLink}");
@ -64,7 +67,7 @@ foreach (var statusLink in statusesToLoad)
var res = await authClient.PostAsync("index", new FormUrlEncodedContent(content)); var res = await authClient.PostAsync("index", new FormUrlEncodedContent(content));
res.EnsureSuccessStatusCode(); res.EnsureSuccessStatusCode();
importedOnThisRun.Add(statusLink); importedList.Add(statusLink);
} }
catch (Exception e) catch (Exception e)
{ {
@ -72,7 +75,14 @@ foreach (var statusLink in statusesToLoad)
} }
} }
File.AppendAllLines(importedPath, importedOnThisRun); if (importedList.Count > 1000)
{
importedList = importedList
.Skip(importedList.Count - 1000)
.ToList();
}
File.WriteAllLines(importedPath, importedList);
public class TagResponse public class TagResponse
{ {