Initializer fixed and improved

main
Inga 🏳‍🌈 13 years ago
parent 5946a121e2
commit ac61771ce6
  1. 2
      Builder/IISMainHandler/build.txt
  2. 77
      FLocal.IISHandler/Initializer.cs

@ -7,6 +7,8 @@ using Web.Core;
using Web.Core.DB;
using FLocal.Common;
using FLocal.Common.dataobjects;
using System.IO;
using System.Threading;
namespace FLocal.IISHandler {
class Initializer {
@ -37,14 +39,20 @@ namespace FLocal.IISHandler {
private void DoInitialize() {
Config.Init(ConfigurationManager.AppSettings);
string dir = FLocal.Common.Config.instance.dataDir + "Logs\\";
using(StreamWriter writer = new StreamWriter(dir + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".INITIALIZE.txt")) {
writer.WriteLine("###INITIALIZE###");
foreach(var cacher in this.cachers) {
System.Threading.ThreadPool.QueueUserWorkItem(state => this.WrapCacher(cacher));
System.Threading.ThreadPool.QueueUserWorkItem(this.GetCacheWrapper(cacher));
writer.WriteLine("Pending " + cacher.Key);
}
}
}
private IEnumerable<Action> cachers {
private IEnumerable<KeyValuePair<string, Action>> cachers {
get {
yield return this.GetTableCacher<LocalNetwork>(LocalNetwork.LoadByIds, LocalNetwork.TableSpec.instance);
yield return this.GetTableCacher<Machichara>(Machichara.LoadByIds, Machichara.TableSpec.instance);
yield return this.GetTableCacher<ModernSkin>(ModernSkin.LoadByIds, ModernSkin.TableSpec.instance);
yield return this.GetTableCacher<PostLayer>(PostLayer.LoadByIds, PostLayer.TableSpec.instance);
@ -52,21 +60,54 @@ namespace FLocal.IISHandler {
yield return this.GetTableCacher<QuickLink>(QuickLink.LoadByIds, QuickLink.TableSpec.instance);
yield return this.GetTableCacher<Skin>(Skin.LoadByIds, Skin.TableSpec.instance);
yield return this.GetTableCacher<UserGroup>(UserGroup.LoadByIds, UserGroup.TableSpec.instance);
yield return this.CacheCategories;
yield return new KeyValuePair<string, Action>("categories", this.CacheCategories);
}
}
private void WrapCacher(Action cacher) {
private WaitCallback GetCacheWrapper(KeyValuePair<string, Action> cacher) {
return state => {
string dir = FLocal.Common.Config.instance.dataDir + "Logs\\";
DateTime start = DateTime.Now;
/*using(StreamWriter writer = new StreamWriter(dir + start.ToString("yyyy-MM-dd_HH-mm-ss") + ".init." + cacher.Key + ".txt")) {
writer.WriteLine("###INITIALIZER/CACHER###");
writer.WriteLine("info: " + cacher.Value.Method.Name);
writer.WriteLine();
}*/
FLocalException error = null;
try {
cacher();
} catch(FLocalException) {
cacher.Value();
} catch(FLocalException e) {
error = e;
}
DateTime end = DateTime.Now;
using(StreamWriter writer = new StreamWriter(dir + end.ToString("yyyy-MM-dd_HH-mm-ss") + ".initend." + cacher.Key + ".txt")) {
writer.WriteLine("###INITIALIZER/CACHER###");
writer.WriteLine("info: " + cacher.Value.Method.Name);
writer.WriteLine("Start: " + start);
writer.WriteLine();
if(error != null) {
writer.WriteLine("Exception: " + error.GetType().FullName);
writer.WriteLine("Guid: " + error.GetGuid().ToString());
writer.WriteLine(error.Message);
if(error is FLocalException) {
writer.WriteLine(((FLocalException)error).FullStackTrace);
} else {
writer.WriteLine(error.StackTrace);
}
}
}
};
}
private Action GetTableCacher<SqlObjectType>(Func<IEnumerable<int>, List<SqlObjectType>> objectsLoader, ITableSpec tableSpec)
private KeyValuePair<string, Action> GetTableCacher<SqlObjectType>(Func<IEnumerable<int>, List<SqlObjectType>> objectsLoader, ITableSpec tableSpec)
where SqlObjectType : SqlObject<SqlObjectType>, new()
{
return () => {
return new KeyValuePair<string,Action>(
typeof(SqlObjectType).FullName,
() => {
foreach(
SqlObjectType sqlObject
in
@ -82,7 +123,8 @@ namespace FLocal.IISHandler {
) {
sqlObject.LoadIfNotLoaded();
}
};
}
);
}
private void CacheCategories() {
@ -101,10 +143,10 @@ namespace FLocal.IISHandler {
if(_maxPostId.HasValue) {
int maxPostId = _maxPostId.Value;
int minPostId = Math.Min(maxPostId - 10000, 0);
for(int i=maxPostId; i>minPostId; i++) {
int minPostId = Math.Max(maxPostId - 10000, 0);
for(int i=maxPostId; i>minPostId; i--) {
try {
var post = Post.LoadById(i);
CachePost(Post.LoadById(i));
} catch(NotFoundInDBException) {
}
}
@ -116,9 +158,18 @@ namespace FLocal.IISHandler {
post.LoadIfNotLoaded();
post.thread.LoadIfNotLoaded();
post.poster.LoadIfNotLoaded();
if(post.poster.avatarId != null) {
post.poster.avatar.LoadIfNotLoaded();
}
if(post.revision != null) {
post.latestRevision.LoadIfNotLoaded();
}
if(post.parentPostId != null) {
post.parentPost.LoadIfNotLoaded();
if(post.parentPost.revision != null) {
post.parentPost.latestRevision.LoadIfNotLoaded();
}
}
foreach(var punishment in post.punishments) {
punishment.LoadIfNotLoaded();
punishment.moderator.LoadIfNotLoaded();

Loading…
Cancel
Save