blob: c3d9ac4efd890c79d46d706b910cb3655e0447ac (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 | using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
using Timeline.Entities;
namespace Timeline.Services.DatabaseManagement
{
    public class DatabaseManagementService : IHostedService
    {
        private readonly IServiceProvider _serviceProvider;
        public DatabaseManagementService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            using var scope = _serviceProvider.CreateScope();
            var provider = scope.ServiceProvider;
            var backupService = provider.GetRequiredService<IDatabaseBackupService>();
            var database = provider.GetRequiredService<DatabaseContext>();
            var customMigrator = provider.GetRequiredService<IDatabaseCustomMigrator>();
            await backupService.BackupAsync(cancellationToken);
            await database.Database.MigrateAsync(cancellationToken);
            await customMigrator.MigrateAsync(cancellationToken);
        }
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }
}
 |