Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make active parsers configurable #66

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions CentCom.Server/BanSources/VgBanParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@

namespace CentCom.Server.BanSources;

public class VgBanParser : BanParser
public class VgBanParser(DatabaseContext dbContext, VgBanService banService, ILogger<VgBanParser> logger)
: BanParser(dbContext, logger)
{
private readonly VgBanService _banService;

public VgBanParser(DatabaseContext dbContext, VgBanService banService, ILogger<VgBanParser> logger) : base(dbContext, logger)
{
_banService = banService;
}

protected override Dictionary<string, BanSource> Sources => new()
{
{ "vgstation", new BanSource
Expand All @@ -32,13 +26,13 @@ public VgBanParser(DatabaseContext dbContext, VgBanService banService, ILogger<V
public override async Task<List<Ban>> FetchAllBansAsync()
{
Logger.LogInformation("Fetching all bans for /vg/station...");
return await _banService.GetBansAsync();
return await banService.GetBansAsync();
}

public override async Task<List<Ban>> FetchNewBansAsync()
{
// Note that the /vg/station website only has a single page for bans, so we always do a full refresh
Logger.LogInformation("Fetching new bans for /vg/station...");
return await _banService.GetBansAsync();
return await banService.GetBansAsync();
}
}
36 changes: 15 additions & 21 deletions CentCom.Server/Data/DatabaseUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
namespace CentCom.Server.Data;

[DisallowConcurrentExecution]
public class DatabaseUpdater : IJob
public class DatabaseUpdater(
DatabaseContext dbContext,
ILogger<DatabaseUpdater> logger,
FlatDataImporter importer,
ISchedulerFactory schedulerFactory,
IConfiguration config)
: IJob
{
/// <summary>
/// Types of BanParsers which should not be automatically configured with a refresh schedule
Expand All @@ -23,32 +29,19 @@ public class DatabaseUpdater : IJob
typeof(StandardBanParser)
};

private readonly DatabaseContext _dbContext;
private readonly FlatDataImporter _importer;
private readonly ILogger _logger;
private readonly List<StandardProviderConfiguration> _providerConfigs;
private readonly ISchedulerFactory _schedulerFactory;

public DatabaseUpdater(DatabaseContext dbContext, ILogger<DatabaseUpdater> logger, FlatDataImporter importer,
ISchedulerFactory schedulerFactory, IConfiguration config)
{
_dbContext = dbContext;
_logger = logger;
_importer = importer;
_schedulerFactory = schedulerFactory;
_providerConfigs = config.GetSection("standardSources").Get<List<StandardProviderConfiguration>>();
}
private readonly ILogger _logger = logger;
private readonly List<StandardProviderConfiguration> _providerConfigs = config.GetSection("standardSources").Get<List<StandardProviderConfiguration>>();

public async Task Execute(IJobExecutionContext context)
{
_logger.LogInformation("Checking for any pending migrations");
var appliedMigration = await _dbContext.Migrate(context.CancellationToken);
var appliedMigration = await dbContext.Migrate(context.CancellationToken);
if (appliedMigration)
_logger.LogInformation("Applied new migration");

// Import any new flat data prior to registering ban parsing jobs
_logger.LogInformation("Checking for any updates to flat file data");
await _importer.RunImports();
await importer.RunImports();

// Call register jobs after db migration to ensure that the DB is actually created on first run before doing any ops
_logger.LogInformation("Registering ban parsing jobs");
Expand All @@ -62,15 +55,16 @@ public async Task Execute(IJobExecutionContext context)
/// </summary>
private async Task RegisterJobs()
{
var allowedParsers = config.GetSection("enabledParsers").Get<HashSet<string>>();
var parsers = AppDomain.CurrentDomain.GetAssemblies().Aggregate(new List<Type>(), (curr, next) =>
{
curr.AddRange(next.GetTypes()
.Where(x => x.IsSubclassOf(typeof(BanParser)) && !_autoConfigBypass.Contains(x)));
.Where(x => x.IsSubclassOf(typeof(BanParser)) && !_autoConfigBypass.Contains(x) && allowedParsers.Contains(x.Name)));
return curr;
});

// Get a scheduler instance
var scheduler = await _schedulerFactory.GetScheduler();
var scheduler = await schedulerFactory.GetScheduler();

foreach (var p in parsers)
{
Expand Down Expand Up @@ -102,7 +96,7 @@ private async Task RegisterJobs()
private async Task RegisterStandardJobs()
{
// Get a scheduler instance
var scheduler = await _schedulerFactory.GetScheduler();
var scheduler = await schedulerFactory.GetScheduler();

// Iterate through each standard provider to set it up
foreach (var provider in _providerConfigs)
Expand Down
5 changes: 3 additions & 2 deletions CentCom.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Extensions.Hosting;
using Quartz;
using Serilog;
using Serilog.Filters;

namespace CentCom.Server;

Expand All @@ -27,7 +28,7 @@ static Task Main(string[] args)
.Enrich.FromLogContext()
.WriteTo.Logger(lc =>
{
//lc.Filter.ByExcluding(Matching.FromSource("Quartz"));
lc.Filter.ByExcluding(Matching.FromSource("Quartz"));
lc.WriteTo.Console(
outputTemplate:
"[{Timestamp:HH:mm:ss} {Level:u3}] ({SourceContext}) {Message:lj}{NewLine}{Exception}");
Expand All @@ -43,7 +44,7 @@ static Task Main(string[] args)
Log.Logger.ForContext<Program>()
.Information("Starting CentCom Server {Version} ({Commit})", AssemblyInformation.Current.Version,
AssemblyInformation.Current.Commit[..7]);

return CreateHostBuilder(args).RunConsoleAsync();
}

Expand Down
19 changes: 6 additions & 13 deletions CentCom.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
"sourceConfig": {
"allowFulpExpiredSSL": true
},
"standardSources": [
{
"id": "shiptest",
"display": "Shiptest 13",
"roleplayLevel": "Medium",
"url": "https://bans.shiptest.ga/"
},
{
"id": "austation",
"display": "AuStation",
"roleplayLevel": "Low",
"url": "https://api.austation.net/"
}
"standardSources": [],
"enabledParsers": [
"BeeBanParser",
"FulpBanParser",
"TgBanParser",
"YogBanParser"
]
}
Loading