Add logging to RequestChain and TimeoutManager

This commit is contained in:
Nikolay Kochulin 2019-12-09 10:47:26 +00:00
parent 77735d8108
commit 8796c099de
3 changed files with 16 additions and 2 deletions

View File

@ -97,7 +97,7 @@ namespace JetKarmaBot
void InitChain(IContainer c) void InitChain(IContainer c)
{ {
Chain = new RequestChain(); Chain = c.ResolveObject(new RequestChain());
Chain.Add(c.GetInstance<TimeoutManager.PreDbThrowout>()); Chain.Add(c.GetInstance<TimeoutManager.PreDbThrowout>());
Chain.Add(c.GetInstance<DatabaseHandler>()); Chain.Add(c.GetInstance<DatabaseHandler>());
Chain.Add(Timeout); Chain.Add(Timeout);

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog;
using Perfusion;
namespace JetKarmaBot.Services.Handling namespace JetKarmaBot.Services.Handling
{ {
@ -10,6 +12,7 @@ namespace JetKarmaBot.Services.Handling
} }
public class RequestChain : IRequestHandler public class RequestChain : IRequestHandler
{ {
[Inject] private Logger log;
List<IRequestHandler> handlerStack = new List<IRequestHandler>(); List<IRequestHandler> handlerStack = new List<IRequestHandler>();
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next = null) public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next = null)
{ {
@ -17,8 +20,13 @@ namespace JetKarmaBot.Services.Handling
Func<RequestContext, Task> chainNext = null; Func<RequestContext, Task> chainNext = null;
chainNext = (newCtx) => chainNext = (newCtx) =>
{ {
if (i == handlerStack.Count) return Task.CompletedTask; if (i == handlerStack.Count)
{
log.ConditionalTrace("(next) End of request chain");
return Task.CompletedTask;
}
IRequestHandler handler = handlerStack[i++]; IRequestHandler handler = handlerStack[i++];
log.ConditionalTrace($"(next) Executing handler {handler.GetType().Name}");
return handler.Handle(newCtx, chainNext); return handler.Handle(newCtx, chainNext);
}; };
await chainNext(ctx); await chainNext(ctx);
@ -27,6 +35,7 @@ namespace JetKarmaBot.Services.Handling
} }
public void Add(IRequestHandler handler) public void Add(IRequestHandler handler)
{ {
log.ConditionalTrace($"Adding {handler.GetType().Name} to reqchain");
handlerStack.Add(handler); handlerStack.Add(handler);
} }
} }

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using JetKarmaBot.Models; using JetKarmaBot.Models;
using System.Threading; using System.Threading;
using System.Linq; using System.Linq;
using NLog;
namespace JetKarmaBot.Services.Handling namespace JetKarmaBot.Services.Handling
{ {
@ -39,6 +40,7 @@ namespace JetKarmaBot.Services.Handling
[Inject] private KarmaContextFactory Db; [Inject] private KarmaContextFactory Db;
[Inject] private Config cfg; [Inject] private Config cfg;
[Inject] private Localization Locale; [Inject] private Localization Locale;
[Inject] private Logger log;
public Dictionary<int, TimeoutStats> TimeoutCache = new Dictionary<int, TimeoutStats>(); public Dictionary<int, TimeoutStats> TimeoutCache = new Dictionary<int, TimeoutStats>();
private async Task ApplyCost(string name, bool succeded, int uid, KarmaContext db) private async Task ApplyCost(string name, bool succeded, int uid, KarmaContext db)
{ {
@ -65,6 +67,7 @@ namespace JetKarmaBot.Services.Handling
{ {
if (!TimeoutCache.ContainsKey(uid)) if (!TimeoutCache.ContainsKey(uid))
{ {
log.ConditionalTrace($"User {uid} not present: saving to cache");
TimeoutCache[uid] = new TimeoutStats() TimeoutCache[uid] = new TimeoutStats()
{ {
CooldownDate = (await db.Users.FindAsync(uid))?.CooldownDate ?? DateTime.Now CooldownDate = (await db.Users.FindAsync(uid))?.CooldownDate ?? DateTime.Now
@ -73,6 +76,7 @@ namespace JetKarmaBot.Services.Handling
} }
public async Task Save(CancellationToken ct = default(CancellationToken)) public async Task Save(CancellationToken ct = default(CancellationToken))
{ {
log.Info("Saving timeout info to database");
using (KarmaContext db = Db.GetContext()) using (KarmaContext db = Db.GetContext())
{ {
foreach (int i in TimeoutCache.Keys) foreach (int i in TimeoutCache.Keys)
@ -81,6 +85,7 @@ namespace JetKarmaBot.Services.Handling
} }
await db.SaveChangesAsync(ct); await db.SaveChangesAsync(ct);
} }
log.Info("Saved");
} }
public async Task SaveLoop(CancellationToken ct = default(CancellationToken)) public async Task SaveLoop(CancellationToken ct = default(CancellationToken))
{ {