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 Web.Core.DB;
using FLocal.Common; using FLocal.Common;
using FLocal.Common.dataobjects; using FLocal.Common.dataobjects;
using System.IO;
using System.Threading;
namespace FLocal.IISHandler { namespace FLocal.IISHandler {
class Initializer { class Initializer {
@ -37,14 +39,20 @@ namespace FLocal.IISHandler {
private void DoInitialize() { private void DoInitialize() {
Config.Init(ConfigurationManager.AppSettings); 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) { 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 { 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<Machichara>(Machichara.LoadByIds, Machichara.TableSpec.instance);
yield return this.GetTableCacher<ModernSkin>(ModernSkin.LoadByIds, ModernSkin.TableSpec.instance); yield return this.GetTableCacher<ModernSkin>(ModernSkin.LoadByIds, ModernSkin.TableSpec.instance);
yield return this.GetTableCacher<PostLayer>(PostLayer.LoadByIds, PostLayer.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<QuickLink>(QuickLink.LoadByIds, QuickLink.TableSpec.instance);
yield return this.GetTableCacher<Skin>(Skin.LoadByIds, Skin.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.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 { try {
cacher(); cacher.Value();
} catch(FLocalException) { } 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() where SqlObjectType : SqlObject<SqlObjectType>, new()
{ {
return () => { return new KeyValuePair<string,Action>(
typeof(SqlObjectType).FullName,
() => {
foreach( foreach(
SqlObjectType sqlObject SqlObjectType sqlObject
in in
@ -82,7 +123,8 @@ namespace FLocal.IISHandler {
) { ) {
sqlObject.LoadIfNotLoaded(); sqlObject.LoadIfNotLoaded();
} }
}; }
);
} }
private void CacheCategories() { private void CacheCategories() {
@ -101,10 +143,10 @@ namespace FLocal.IISHandler {
if(_maxPostId.HasValue) { if(_maxPostId.HasValue) {
int maxPostId = _maxPostId.Value; int maxPostId = _maxPostId.Value;
int minPostId = Math.Min(maxPostId - 10000, 0); int minPostId = Math.Max(maxPostId - 10000, 0);
for(int i=maxPostId; i>minPostId; i++) { for(int i=maxPostId; i>minPostId; i--) {
try { try {
var post = Post.LoadById(i); CachePost(Post.LoadById(i));
} catch(NotFoundInDBException) { } catch(NotFoundInDBException) {
} }
} }
@ -116,9 +158,18 @@ namespace FLocal.IISHandler {
post.LoadIfNotLoaded(); post.LoadIfNotLoaded();
post.thread.LoadIfNotLoaded(); post.thread.LoadIfNotLoaded();
post.poster.LoadIfNotLoaded(); post.poster.LoadIfNotLoaded();
if(post.poster.avatarId != null) {
post.poster.avatar.LoadIfNotLoaded();
}
if(post.revision != null) {
post.latestRevision.LoadIfNotLoaded(); post.latestRevision.LoadIfNotLoaded();
}
if(post.parentPostId != null) {
post.parentPost.LoadIfNotLoaded(); post.parentPost.LoadIfNotLoaded();
if(post.parentPost.revision != null) {
post.parentPost.latestRevision.LoadIfNotLoaded(); post.parentPost.latestRevision.LoadIfNotLoaded();
}
}
foreach(var punishment in post.punishments) { foreach(var punishment in post.punishments) {
punishment.LoadIfNotLoaded(); punishment.LoadIfNotLoaded();
punishment.moderator.LoadIfNotLoaded(); punishment.moderator.LoadIfNotLoaded();

Loading…
Cancel
Save