Added worker app.
This commit is contained in:
109
JSMR.Worker/Program.cs
Normal file
109
JSMR.Worker/Program.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using JSMR.Application.DI;
|
||||
using JSMR.Infrastructure.Data;
|
||||
using JSMR.Infrastructure.DI;
|
||||
using JSMR.Worker.Options;
|
||||
using JSMR.Worker.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.CommandLine;
|
||||
|
||||
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
|
||||
|
||||
//builder.Services.AddSerilog(o => o
|
||||
// .WriteTo.Console()
|
||||
// .MinimumLevel.Information());
|
||||
|
||||
builder.Services
|
||||
.AddApplication()
|
||||
.AddInfrastructure();
|
||||
|
||||
string connectionString = builder.Configuration.GetConnectionString("AppDb")
|
||||
?? throw new InvalidOperationException("Missing ConnectionStrings:AppDb2");
|
||||
|
||||
builder.Services.AddDbContextFactory<AppDbContext>(optionsBuilder =>
|
||||
optionsBuilder
|
||||
.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
|
||||
.EnableSensitiveDataLogging(false));
|
||||
|
||||
// Worker services
|
||||
builder.Services.AddSingleton<ICheckpointStore, FileCheckpointStore>();
|
||||
builder.Services.AddTransient<PagedScanRunner>();
|
||||
|
||||
RootCommand rootCommand = new("JSMR worker");
|
||||
Command scan = new("scan", "Scan and update the database");
|
||||
|
||||
Option<string?> localeOption = new("--locale", "-l")
|
||||
{
|
||||
Description = "Locale (Japanese/English)",
|
||||
Required = false
|
||||
};
|
||||
|
||||
Option<int?> startOption = new("--start", "-s")
|
||||
{
|
||||
Description = "Start page (default = checkpoint+1 or 1)"
|
||||
};
|
||||
|
||||
Option<int?> endOption = new("--end", "-e")
|
||||
{
|
||||
Description = "End page (optional)"
|
||||
};
|
||||
|
||||
Option<int?> sizeOption = new("--pageSize", "-ps")
|
||||
{
|
||||
Description = "Page size (default from config or 100)",
|
||||
DefaultValueFactory = _ => 100
|
||||
};
|
||||
|
||||
Option<bool> watchOption = new("--watch", "-w")
|
||||
{
|
||||
Description = "Loop forever",
|
||||
DefaultValueFactory = _ => false
|
||||
};
|
||||
|
||||
Option<TimeSpan> everyOption = new("--every", "-e")
|
||||
{
|
||||
Description = "Interval when --watch is set",
|
||||
DefaultValueFactory = _ => TimeSpan.FromMinutes(5)
|
||||
};
|
||||
|
||||
|
||||
scan.Add(localeOption);
|
||||
scan.Add(startOption);
|
||||
scan.Add(endOption);
|
||||
scan.Add(sizeOption);
|
||||
scan.Add(watchOption);
|
||||
scan.Add(everyOption);
|
||||
|
||||
scan.SetAction(async (parseResult, cancellationToken) =>
|
||||
{
|
||||
using var host = builder.Build();
|
||||
var runner = host.Services.GetRequiredService<PagedScanRunner>();
|
||||
|
||||
ScanOptions options = new()
|
||||
{
|
||||
Locale = parseResult.GetValue(localeOption),
|
||||
StartPage = parseResult.GetValue(startOption),
|
||||
EndPage = parseResult.GetValue(endOption),
|
||||
PageSize = parseResult.GetValue(sizeOption),
|
||||
Watch = parseResult.GetValue(watchOption),
|
||||
Interval = parseResult.GetValue(everyOption)
|
||||
};
|
||||
|
||||
using CancellationTokenSource cancellationTokenSource = new();
|
||||
|
||||
Console.CancelKeyPress += (_, eventArgs) =>
|
||||
{
|
||||
eventArgs.Cancel = true;
|
||||
cancellationTokenSource.Cancel();
|
||||
};
|
||||
|
||||
await runner.RunAsync(options, cancellationTokenSource.Token);
|
||||
});
|
||||
|
||||
rootCommand.Add(scan);
|
||||
|
||||
//rootCommand.SetAction(async (parseResult, cancellationToken) => await rootCommand.InvokeAsync("scan"));
|
||||
|
||||
return await rootCommand.Parse(args).InvokeAsync();
|
||||
Reference in New Issue
Block a user