Skip to content

Commit

Permalink
* fix not invoking the action returned by `ReplyContentImageSaver.Sav…
Browse files Browse the repository at this point in the history
…e()` to release its locked images, by manually dispose it after `DbContext.SaveChanges()` @ `DoWork()`

* fix double freeing already released locks when get invoked more than once by reset field `_(newly|already)LockedImages` to null @ `Dispose()`
@ ProcessImagesInAllReplyContentsWorker.cs

* instead of throwing `InvalidOperationException` to interrupt any further releasing, logging about the failure when removing global locked image
@ `ReplyContentImageSaver.Dispose()`
@ crawler

+ param `onPostSaveChanges` @ `TransformEntityWorker.Transform()`
@ shared
@ c#
  • Loading branch information
n0099 committed Jun 12, 2024
1 parent 55c529f commit 92dfb6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
11 changes: 8 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/ReplyContentImageSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ public void Dispose()
{
try
{
if (_newlyLockedImages != null && _newlyLockedImages.Any(pair =>
!GlobalLockedImagesInReplyKeyByUrlFilename.TryRemove(pair)))
throw new InvalidOperationException();
_newlyLockedImages?.ForEach(pair =>
{
if (!GlobalLockedImagesInReplyKeyByUrlFilename.TryRemove(pair))
logger.LogError("Previously locked image {} already removed from the global locks",
SharedHelper.UnescapedJsonSerialize(pair));
});
}
finally
{
_newlyLockedImages?.Values().ForEach(Monitor.Exit);
_alreadyLockedImages?.Values().ForEach(Monitor.Exit);
_newlyLockedImages = null;
_alreadyLockedImages = null;
}
}

Expand Down
17 changes: 11 additions & 6 deletions c#/crawler/src/Worker/ProcessImagesInAllReplyContentsWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ await Transform(
var p = ee.Property(e => e.ProtoBufBytes);
p.IsModified = !ByteArrayEqualityComparer.Instance.Equals(p.OriginalValue, p.CurrentValue);
});
replyContentImageSaver.Save(writingDb, replyContentsKeyByPid.Select(pair => new ReplyPost
{
Pid = pair.Key,
Content = null!,
ContentsProtoBuf = pair.Value
}));
_ = replyContentImageSaver.Save(writingDb,
replyContentsKeyByPid.Select(pair => new ReplyPost
{
Pid = pair.Key,
Content = null!,
ContentsProtoBuf = pair.Value
}));
},
() =>
{
replyContentsKeyByPid.Clear();
replyContentImageSaver.Dispose();
},
stoppingToken);
logger.LogInformation("Simplify images in reply contents of fid {} finished after {:F2}s",
Expand Down
2 changes: 2 additions & 0 deletions c#/shared/src/TransformEntityWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected async Task Transform(
Func<TReadingEntity, TWritingEntity> writingEntityFactory,
Action<TReadingEntity, TWritingEntity> writingEntityMutator,
Action<TDbContext, IReadOnlyCollection<EntityEntry<TWritingEntity>>> writingEntityEntriesAction,
Action onPostSaveChanges,
CancellationToken stoppingToken = default)
{
var processedEntityCount = 0;
Expand All @@ -52,6 +53,7 @@ async Task SaveThenLog(int processedCount, Process currentProcess)
var updatedEntityCount = await writingDb.SaveChangesAsync(stoppingToken);
writingDb.ChangeTracker.Clear();
writingEntityEntries.Clear();
onPostSaveChanges();

logger.LogTrace("processedEntityCount:{} updatedEntityCount:{} elapsed:{}ms processMemory:{:F2}MiB exceptions:{}",
processedCount, updatedEntityCount,
Expand Down

0 comments on commit 92dfb6b

Please sign in to comment.