Added worker app.
This commit is contained in:
21
JSMR.Worker/JSMR.Worker.csproj
Normal file
21
JSMR.Worker/JSMR.Worker.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.12" />
|
||||
<PackageReference Include="Serilog" Version="4.3.0" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\JSMR.Application\JSMR.Application.csproj" />
|
||||
<ProjectReference Include="..\JSMR.Infrastructure\JSMR.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
11
JSMR.Worker/Options/ScanOptions.cs
Normal file
11
JSMR.Worker/Options/ScanOptions.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace JSMR.Worker.Options;
|
||||
|
||||
public sealed class ScanOptions
|
||||
{
|
||||
public string? Locale { get; init; } = "Japanese"; // maps to your Locale enum
|
||||
public int? StartPage { get; init; } // if null, resume from checkpoint or 1
|
||||
public int? EndPage { get; init; } // optional cap
|
||||
public int? PageSize { get; init; } // override default
|
||||
public bool Watch { get; init; } // loop forever
|
||||
public TimeSpan Interval { get; init; } = TimeSpan.FromMinutes(5);
|
||||
}
|
||||
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();
|
||||
28
JSMR.Worker/Services/FileCheckpointStore.cs
Normal file
28
JSMR.Worker/Services/FileCheckpointStore.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace JSMR.Worker.Services;
|
||||
|
||||
public sealed class FileCheckpointStore : ICheckpointStore
|
||||
{
|
||||
private readonly string _root = Path.Combine(AppContext.BaseDirectory, "State");
|
||||
|
||||
public FileCheckpointStore() => Directory.CreateDirectory(_root);
|
||||
|
||||
public Task<int?> GetLastPageAsync(string locale, CancellationToken ct)
|
||||
{
|
||||
string path = Path.Combine(_root, $"scan.{locale}.page");
|
||||
if (!File.Exists(path))
|
||||
return Task.FromResult<int?>(null);
|
||||
|
||||
if (int.TryParse(File.ReadAllText(path).Trim(), out var n))
|
||||
return Task.FromResult<int?>(n);
|
||||
|
||||
return Task.FromResult<int?>(null);
|
||||
}
|
||||
|
||||
public Task SaveLastPageAsync(string locale, int page, CancellationToken ct)
|
||||
{
|
||||
string path = Path.Combine(_root, $"scan.{locale}.page");
|
||||
File.WriteAllText(path, page.ToString());
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
7
JSMR.Worker/Services/ICheckpointStore.cs
Normal file
7
JSMR.Worker/Services/ICheckpointStore.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace JSMR.Worker.Services;
|
||||
|
||||
public interface ICheckpointStore
|
||||
{
|
||||
Task<int?> GetLastPageAsync(string locale, CancellationToken cancellationToken);
|
||||
Task SaveLastPageAsync(string locale, int page, CancellationToken cancellationToken);
|
||||
}
|
||||
28
JSMR.Worker/Services/ScanJob.cs
Normal file
28
JSMR.Worker/Services/ScanJob.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using JSMR.Application.Enums;
|
||||
using JSMR.Application.Scanning;
|
||||
using JSMR.Worker.Options;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace JSMR.Worker.Services;
|
||||
|
||||
public sealed class ScanJob(ILogger<ScanJob> log, IOptions<ScanOptions> options, ScanVoiceWorksHandler scanVoiceWorksHandler)
|
||||
{
|
||||
private readonly ScanOptions _options = options.Value;
|
||||
|
||||
public async Task RunOnceAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
log.LogInformation("Starting scan: Locale={Locale}, Start Page={StartPage}, EndPage={EndPage}",
|
||||
_options.Locale, _options.StartPage, _options.EndPage);
|
||||
|
||||
ScanVoiceWorksRequest request = new(
|
||||
PageNumber: 1,
|
||||
PageSize: 100,
|
||||
Locale: Enum.Parse<Locale>(_options.Locale, true)
|
||||
);
|
||||
|
||||
await scanVoiceWorksHandler.HandleAsync(request, cancellationToken);
|
||||
|
||||
log.LogInformation("Scan completed.");
|
||||
}
|
||||
}
|
||||
59
JSMR.Worker/Services/ScanRunner.cs
Normal file
59
JSMR.Worker/Services/ScanRunner.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using JSMR.Application.Enums;
|
||||
using JSMR.Application.Scanning;
|
||||
using JSMR.Worker.Options;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace JSMR.Worker.Services;
|
||||
|
||||
public sealed class PagedScanRunner(
|
||||
ILogger<PagedScanRunner> log,
|
||||
IServiceProvider serviceProvider,
|
||||
ICheckpointStore checkpoints)
|
||||
{
|
||||
public async Task RunAsync(ScanOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Enum.TryParse(options.Locale, ignoreCase: true, out Locale locale) == false)
|
||||
throw new ArgumentException($"Unknown locale '{options.Locale}'.");
|
||||
|
||||
int pageSize = options.PageSize ?? 100;
|
||||
|
||||
int startPage = options.StartPage
|
||||
?? (await checkpoints.GetLastPageAsync(options.Locale, cancellationToken)).GetValueOrDefault(0) + 1;
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
int currentPage = startPage;
|
||||
int? end = options.EndPage;
|
||||
|
||||
// Iterate until empty page or end reached
|
||||
for (; !cancellationToken.IsCancellationRequested && (!end.HasValue || currentPage <= end.Value); currentPage++)
|
||||
{
|
||||
ScanVoiceWorksHandler handler = serviceProvider.GetRequiredService<ScanVoiceWorksHandler>();
|
||||
|
||||
log.LogInformation("Scanning page {Page} (size {Size}, locale {Locale})…", currentPage, pageSize, locale);
|
||||
|
||||
ScanVoiceWorksRequest request = new(
|
||||
PageNumber: currentPage,
|
||||
PageSize: 100,
|
||||
Locale: locale
|
||||
);
|
||||
|
||||
ScanVoiceWorksResponse response = await handler.HandleAsync(request, cancellationToken);
|
||||
|
||||
// Save checkpoint
|
||||
await checkpoints.SaveLastPageAsync(options.Locale, currentPage, cancellationToken);
|
||||
}
|
||||
|
||||
if (!options.Watch) break;
|
||||
|
||||
log.LogInformation("Watch mode: sleeping {Interval}…", options.Interval);
|
||||
await Task.Delay(options.Interval, cancellationToken);
|
||||
|
||||
// Compute next “start” for next cycle:
|
||||
// - If you want to re-scan the latest N pages every loop to catch late updates,
|
||||
// modify logic here (e.g., start = Math.Max(1, current - 2))
|
||||
startPage = currentPage; // continue from where we left off
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user