Files
MoShiro/backend/src/MoShiro.Backend.Application/StartupTasks.cs
T

37 lines
1.4 KiB
C#

using Microsoft.Extensions.Hosting;
namespace MoShiro.Backend.Application;
/// <summary>
/// 启动时最先执行:截断没有索引记录的段文件尾部、(用配置里的凭据创建管理员,没有的话把首次认领码写进日志)创建默认的线路任务模板。
/// 宿主按注册顺序依次等待各个 IHostedService 启动,所以它需要最先注册。
/// </summary>
public sealed class StartupTasks : IHostedService
{
private readonly IRawStore _raw;
private readonly AuthService _auth;
private readonly TaskService _tasks;
private readonly ServerIdentityService _identity;
private readonly SetupService _setup;
public StartupTasks(IRawStore raw, AuthService auth, TaskService tasks, ServerIdentityService identity, SetupService setup)
{
_setup = setup;
_identity = identity;
_raw = raw;
_auth = auth;
_tasks = tasks;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _raw.RecoverAsync(cancellationToken);
await _auth.EnsureInitialAdminAsync(cancellationToken);
await _setup.AnnounceAsync(cancellationToken);
await _tasks.SeedDefaultsAsync(cancellationToken);
await _identity.EnsureAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}