Added logging to module framework

Implemented logging to Adafruit and changed database loading behavior.
This commit is contained in:
2021-08-07 17:20:46 -05:00
parent c94ea4a624
commit 38ffb3c7e1
36 changed files with 304 additions and 240 deletions

View File

@@ -7,6 +7,7 @@ namespace Props.Services.Modules
{
public interface ISearchManager
{
public IEnumerable<ProductListing> Search(string query, SearchOutline searchOutline);
public IShopManager ShopManager { get; }
public Task<IEnumerable<ProductListing>> Search(string query, SearchOutline searchOutline);
}
}

View File

@@ -14,17 +14,17 @@ namespace Props.Services.Modules
{
private ILogger<LiveSearchManager> logger;
private SearchOptions searchOptions;
private IShopManager shopManager;
public IShopManager ShopManager { get; private set; }
private IMetricsManager metricsManager;
public LiveSearchManager(IMetricsManager metricsManager, IShopManager shopManager, IConfiguration configuration, ILogger<LiveSearchManager> logger)
{
this.logger = logger;
this.metricsManager = metricsManager;
this.shopManager = shopManager;
this.ShopManager = shopManager;
this.searchOptions = configuration.GetSection(SearchOptions.Search).Get<SearchOptions>();
}
public IEnumerable<ProductListing> Search(string query, SearchOutline searchOutline)
public async Task<IEnumerable<ProductListing>> Search(string query, SearchOutline searchOutline)
{
if (string.IsNullOrWhiteSpace(query)) throw new ArgumentException($"Query \"{query}\" is null or whitepsace.");
if (searchOutline == null) throw new ArgumentNullException("searchOutline");
@@ -32,13 +32,13 @@ namespace Props.Services.Modules
metricsManager.RegisterSearchQuery(query);
logger.LogDebug("Searching for \"{0}\".", query);
foreach (string shopName in shopManager.GetAllShopNames())
foreach (string shopName in ShopManager.GetAllShopNames())
{
if (searchOutline.Enabled[shopName])
{
logger.LogDebug("Checking \"{0}\".", shopName);
int amount = 0;
foreach (ProductListing product in shopManager.GetShop(shopName).Search(query, searchOutline.Filters))
await foreach (ProductListing product in ShopManager.GetShop(shopName).Search(query, searchOutline.Filters))
{
if (searchOutline.Filters.Validate(product))
{

View File

@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.Web.CodeGeneration;
using Props.Data;
using Props.Models.Search;
using Props.Options;
@@ -18,26 +19,22 @@ namespace Props.Services.Modules
{
public class ModularShopManager : IShopManager
{
private ILoggerFactory loggerFactory;
private ILogger<ModularShopManager> logger;
private Dictionary<string, IShop> shops;
private ModulesOptions options;
private IConfiguration configuration;
private bool disposedValue;
public ModularShopManager(IConfiguration configuration, ILogger<ModularShopManager> logger)
public ModularShopManager(IConfiguration configuration, ILogger<ModularShopManager> logger, ILoggerFactory loggerFactory)
{
this.configuration = configuration;
this.loggerFactory = loggerFactory;
this.logger = logger;
this.configuration = configuration;
options = configuration.GetSection(ModulesOptions.Modules).Get<ModulesOptions>();
Directory.CreateDirectory(options.ModuleDataDir);
shops = new Dictionary<string, IShop>();
foreach (IShop shop in LoadShops(options.ShopsDir, options.ShopRegex, options.RecursiveLoad))
{
if (!shops.TryAdd(shop.ShopName, shop))
{
logger.LogWarning("Duplicate shop {0} detected. Ignoring the latter.", shop.ShopName);
}
}
LoadShops();
}
public IEnumerable<string> GetAllShopNames()
@@ -56,9 +53,13 @@ namespace Props.Services.Modules
return shops.Values;
}
private IEnumerable<IShop> LoadShops(string shopsDir, string shopRegex, bool recursiveLoad)
public void LoadShops()
{
Stack<Task> asyncInitTasks = new Stack<Task>();
// TODO: Figure out how to best call this.
string shopsDir = options.ModulesDir;
string shopRegex = options.ShopRegex;
bool recursiveLoad = options.RecursiveLoad;
Stack<string> directories = new Stack<string>();
directories.Push(shopsDir);
string currentDirectory = null;
@@ -86,12 +87,14 @@ namespace Props.Services.Modules
IShop shop = Activator.CreateInstance(type) as IShop;
if (shop != null)
{
// TODO: load persisted shop data.
shop.Initialize(null);
asyncInitTasks.Push(shop.InitializeAsync(null));
DirectoryInfo dataDir = Directory.CreateDirectory(Path.Combine(options.ModuleDataDir, file));
shop.Initialize(dataDir.FullName, loggerFactory);
success += 1;
if (!shops.TryAdd(shop.ShopName, shop))
{
logger.LogWarning("Duplicate shop {0} detected. Ignoring the latter.", shop.ShopName);
}
logger.LogDebug("Loaded \"{0}\".", shop.ShopName);
yield return shop;
}
}
}
@@ -103,7 +106,6 @@ namespace Props.Services.Modules
}
}
logger.LogDebug("Waiting for all shops to finish asynchronous initialization.");
Task.WaitAll(asyncInitTasks.ToArray());
logger.LogDebug("All shops finished asynchronous initialization.");
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Logging;
using Props.Shop.Framework;
namespace Props.Services.Modules
@@ -18,6 +19,7 @@ namespace Props.Services.Modules
protected override Assembly Load(AssemblyName assemblyName)
{
if (assemblyName.FullName.Equals(typeof(IShop).Assembly.FullName)) return null;
if (assemblyName.FullName.Equals(typeof(ILoggerFactory).Assembly.FullName)) return null;
string assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null;
}