Skip to content

Commit

Permalink
forum indexing runs on separate thread to not block saving, it can gr…
Browse files Browse the repository at this point in the history
…acefully fail and also word saving now retries in case there is a concurrency issue
  • Loading branch information
Licho1 committed Dec 3, 2023
1 parent 42da1de commit 9242fbe
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions Zero-K.info/AppCode/ForumPostIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using PlasmaShared;
Expand All @@ -29,18 +31,31 @@ public void IndexAll() {
int GetWordID(string word) {
int id;
if (wordIDs.TryGetValue(word, out id)) return id;
using (var db = new ZkDataContext())

for (int i = 0; i < 3; i++) // try 3 times
{
var entry = db.IndexWords.FirstOrDefault(x => x.Text == word);
if (entry == null)
try
{
using (var db = new ZkDataContext())
{
var entry = db.IndexWords.FirstOrDefault(x => x.Text == word);
if (entry == null)
{
entry = new Word { Text = word };
db.IndexWords.Add(entry);
db.SaveChanges();
}

wordIDs[entry.Text] = entry.WordID;
return entry.WordID;
}
}
catch (SqlException ex)
{
entry = new Word { Text = word };
db.IndexWords.Add(entry);
db.SaveChanges();
Trace.TraceWarning("Problem indexing word {0}: {1}", word, ex.Message);
}
wordIDs[entry.Text] = entry.WordID;
return entry.WordID;
}
throw new Exception("Failed to index word " + word);
}

public IQueryable<ForumPost> FilterPosts(IQueryable<ForumPost> input, string term) {
Expand All @@ -55,7 +70,21 @@ public List<int> SplitTermToWordIDs(string term) {

void ZkDataContextOnAfterEntityChange(object sender, ZkDataContext.EntityEntry dbEntityEntry) {
var post = dbEntityEntry.Entity as ForumPost;
if (post != null && (dbEntityEntry.State == EntityState.Added || dbEntityEntry.State == EntityState.Modified)) IndexPost(post);
if (post != null && (dbEntityEntry.State == EntityState.Added || dbEntityEntry.State == EntityState.Modified))
{
Task.Run(() =>
{
try
{
IndexPost(post);
}
catch (Exception ex)
{
Trace.TraceWarning("Problem indexing post {0}: {1}", post.ForumPostID, ex.Message);
}
});

}
}

public static string SanitizeWord(string word) {
Expand Down

0 comments on commit 9242fbe

Please sign in to comment.