Skip to content

Commit 183166b

Browse files
committed
* now will stop retrying to update _latestReplyPostedAtCheckpoints[fid] and crawl these pages after the configured CrawlerLocks.Thread.MaxRetryTimes times @ Tieba.Crawl.CrawlPost.CrawlThreads()
* now will always decrease `RequesterTcs` when the caught exception is `TiebaException` @ `Tieba.Crawl.Facade.CrawlFacade.LogException()` * fix violations of Roslyn analyzer rule `SA1103` @ `Worker.PushAllPostContentsIntoSonicWorker.DoWork()` @ c#/crawler
1 parent f061932 commit 183166b

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

c#/crawler/src/Tieba/Crawl/CrawlPost.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace tbm.Crawler.Tieba.Crawl;
77
using SavedThreadsList = IReadOnlyCollection<SaverChangeSet<ThreadPost, ThreadPost.Parsed>>;
88

99
public class CrawlPost(
10+
IConfiguration config,
1011
Func<Owned<CrawlerDbContext.New>> dbContextFactory,
1112
Func<Owned<ThreadLateCrawlFacade.New>> threadLateCrawlFacadeFactory,
1213
Func<Owned<ThreadCrawlFacade.New>> threadCrawlFacadeFactory,
@@ -20,10 +21,6 @@ public async Task<SavedThreadsList> CrawlThreads
2021
(string forumName, Fid fid, CancellationToken stoppingToken = default)
2122
{
2223
stoppingToken.ThrowIfCancellationRequested();
23-
var savedThreads = new List<SaverChangeSet<ThreadPost, ThreadPost.Parsed>>();
24-
Time minLatestReplyPostedAt;
25-
Page crawlingPage = 0;
26-
2724
if (!_latestReplyPostedAtCheckpoints.TryGetValue(fid, out var maxLatestReplyPostedAtOccurInPreviousCrawl))
2825
{ // get the largest value of field latestReplyPostedAt in all stored threads of this forum
2926
// this approach is not as accurate as extracting the last thread in the response list
@@ -34,6 +31,11 @@ public async Task<SavedThreadsList> CrawlThreads
3431
.Where(t => t.Fid == fid)
3532
.Max(th => (Time?)th.LatestReplyPostedAt) ?? Time.MaxValue;
3633
}
34+
var savedThreads = new List<SaverChangeSet<ThreadPost, ThreadPost.Parsed>>();
35+
var minLatestReplyPostedAt = Time.MaxValue;
36+
Page crawlingPage = 0;
37+
FailureCount failureCount = 0;
38+
var maxRetry = config.GetSection("CrawlerLocks:Thread").GetValue("MaxRetryTimes", 5);
3739
do
3840
{
3941
crawlingPage++;
@@ -47,13 +49,19 @@ public async Task<SavedThreadsList> CrawlThreads
4749
var threadsLatestReplyPostedAt = currentPageChangeSet.AllParsed
4850
.Select(th => th.LatestReplyPostedAt).ToList();
4951
minLatestReplyPostedAt = threadsLatestReplyPostedAt.Min();
50-
if (crawlingPage == 1)
51-
_latestReplyPostedAtCheckpoints[fid] = threadsLatestReplyPostedAt.Max();
52+
var maxLatestReplyPostedAt = threadsLatestReplyPostedAt.Max();
53+
if (!_latestReplyPostedAtCheckpoints.TryGetValue(fid, out var checkpoint)
54+
|| checkpoint < maxLatestReplyPostedAt)
55+
_latestReplyPostedAtCheckpoints[fid] = maxLatestReplyPostedAt;
5256
}
53-
else
57+
else if (failureCount < maxRetry)
5458
{ // retry this page
5559
crawlingPage--;
56-
minLatestReplyPostedAt = Time.MaxValue;
60+
failureCount++;
61+
}
62+
else if (failureCount >= maxRetry)
63+
{
64+
minLatestReplyPostedAt = Time.MinValue;
5765
}
5866
} while (minLatestReplyPostedAt > maxLatestReplyPostedAtOccurInPreviousCrawl);
5967

c#/crawler/src/Tieba/Crawl/Facade/CrawlFacade.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,19 @@ private async Task<bool> LogException(
202202

203203
if (e is TiebaException te)
204204
{
205-
if (!te.ShouldSilent) Logger.LogWarning("TiebaException: {} {}",
206-
string.Join(' ', e.GetInnerExceptions().Select(ex => ex.Message)),
207-
SharedHelper.UnescapedJsonSerialize(e.Data));
205+
RequesterTcs.Decrease();
206+
if (!te.ShouldSilent)
207+
Logger.LogWarning("TiebaException: {} {}",
208+
string.Join(' ', e.GetInnerExceptions().Select(ex => ex.Message)),
209+
SharedHelper.UnescapedJsonSerialize(e.Data));
208210
}
209211
else
210212
{
211213
Logger.LogError(e, "Exception");
212214
}
213215

214216
if (e is not TiebaException {ShouldRetry: false})
215-
{
216217
locks.AcquireFailed(lockId, page, (FailureCount)(previousFailureCount + 1));
217-
RequesterTcs.Decrease();
218-
}
219218

220219
try
221220
{

c#/crawler/src/Worker/PushAllPostContentsIntoSonicWorker.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,20 @@ protected override async Task DoWork(CancellationToken stoppingToken)
3636
var forumIndex = (index + 1) * 2; // counting from one, including both reply and sub reply
3737

3838
_ = await pusher.Ingest.FlushBucketAsync($"{pusher.CollectionPrefix}replies_content", $"f{fid}");
39-
var replyContents = from r in db.ReplyContents.AsNoTracking()
40-
where db.Replies.Where(e => e.Fid == fid).Select(e => e.Pid).Contains(r.Pid) select r;
39+
var replyContents =
40+
from r in db.ReplyContents.AsNoTracking()
41+
where db.Replies.Where(e => e.Fid == fid).Select(e => e.Pid).Contains(r.Pid)
42+
select r;
4143
pushedPostCount += PushPostContentsWithTiming(fid, forumIndex - 1, forumCount, "replies",
4244
replyCount, totalPostCount, pushedPostCount, replyContents,
4345
r => pusher.PushPost(fid, "replies", r.Pid, Helper.ParseThenUnwrapPostContent(r.ProtoBufBytes)), stoppingToken);
4446
await TriggerConsolidate();
4547

4648
_ = await pusher.Ingest.FlushBucketAsync($"{pusher.CollectionPrefix}subReplies_content", $"f{fid}");
47-
var subReplyContents = from sr in db.SubReplyContents.AsNoTracking()
48-
where db.SubReplies.Where(e => e.Fid == fid).Select(e => e.Spid).Contains(sr.Spid) select sr;
49+
var subReplyContents =
50+
from sr in db.SubReplyContents.AsNoTracking()
51+
where db.SubReplies.Where(e => e.Fid == fid).Select(e => e.Spid).Contains(sr.Spid)
52+
select sr;
4953
pushedPostCount += PushPostContentsWithTiming(fid, forumIndex, forumCount, "sub replies",
5054
subReplyCount, totalPostCount, pushedPostCount, subReplyContents,
5155
sr => pusher.PushPost(fid, "subReplies", sr.Spid, Helper.ParseThenUnwrapPostContent(sr.ProtoBufBytes)), stoppingToken);

0 commit comments

Comments
 (0)