From 9a1404a4013e8e01b257f8b73bea523827d249e3 Mon Sep 17 00:00:00 2001 From: SuhorukovAnton Date: Mon, 13 Mar 2023 19:29:42 +0300 Subject: [PATCH] BackupsContext - async --- .../ASC.Data.Backup.Core/BackupAjaxHandler.cs | 19 ++-- .../Contracts/IBackupService.cs | 10 +-- .../Service/BackupService.cs | 30 +++---- .../ProgressItems/BackupProgressItem.cs | 2 +- .../ProgressItems/RestoreProgressItem.cs | 2 +- .../Storage/BackupRepository.cs | 89 +++++++++---------- .../Storage/IBackupRepository.cs | 22 ++--- .../Tasks/BackupPortalTask.cs | 8 +- .../Tasks/RestorePortalTask.cs | 2 +- ...heldureRequestedIntegrationEventHandler.cs | 4 +- ...RestoreRequestedIntegrationEventHandler.cs | 2 +- .../Services/BackupCleanerService.cs | 14 +-- .../Services/BackupListenerService.cs | 6 +- .../Services/BackupSchedulerService.cs | 8 +- .../ASC.Data.Backup/Api/BackupController.cs | 8 +- .../Api/Settings/StorageController.cs | 4 +- 16 files changed, 111 insertions(+), 119 deletions(-) diff --git a/common/ASC.Data.Backup.Core/BackupAjaxHandler.cs b/common/ASC.Data.Backup.Core/BackupAjaxHandler.cs index 9383a7919a..353c24a31c 100644 --- a/common/ASC.Data.Backup.Core/BackupAjaxHandler.cs +++ b/common/ASC.Data.Backup.Core/BackupAjaxHandler.cs @@ -127,7 +127,7 @@ public class BackupAjaxHandler { DemandPermissionsBackup(); - await _backupService.DeleteAllBackups(GetCurrentTenantId()); + await _backupService.DeleteAllBackupsAsync(GetCurrentTenantId()); } public async Task> GetBackupHistory() @@ -173,16 +173,16 @@ public class BackupAjaxHandler break; } - _backupService.CreateSchedule(scheduleRequest); + _backupService.CreateScheduleAsync(scheduleRequest); } - public Schedule GetSchedule() + public async Task GetScheduleAsync() { DemandPermissionsBackup(); ScheduleResponse response; - response = _backupService.GetSchedule(GetCurrentTenantId()); + response = await _backupService.GetScheduleAsync(GetCurrentTenantId()); if (response == null) { return null; @@ -224,7 +224,7 @@ public class BackupAjaxHandler StorageParams = schedule.StorageParams }; - _backupService.CreateSchedule(Schedule); + _backupService.CreateScheduleAsync(Schedule); } else if (response.StorageType != BackupStorageType.ThirdPartyConsumer) @@ -235,12 +235,11 @@ public class BackupAjaxHandler return schedule; } - public void DeleteSchedule() + public Task DeleteScheduleAsync() { DemandPermissionsBackup(); - _backupService.DeleteSchedule(GetCurrentTenantId()); - + return _backupService.DeleteScheduleAsync(GetCurrentTenantId()); } private void DemandPermissionsBackup() @@ -257,7 +256,7 @@ public class BackupAjaxHandler #region restore - public void StartRestore(string backupId, BackupStorageType storageType, Dictionary storageParams, bool notify) + public Task StartRestoreAsync(string backupId, BackupStorageType storageType, Dictionary storageParams, bool notify) { DemandPermissionsRestore(); @@ -283,7 +282,7 @@ public class BackupAjaxHandler } } - _backupService.StartRestore(restoreRequest); + return _backupService.StartRestoreAsync(restoreRequest); } public BackupProgress GetRestoreProgress() diff --git a/common/ASC.Data.Backup.Core/Contracts/IBackupService.cs b/common/ASC.Data.Backup.Core/Contracts/IBackupService.cs index 823e8c692a..359832b04e 100644 --- a/common/ASC.Data.Backup.Core/Contracts/IBackupService.cs +++ b/common/ASC.Data.Backup.Core/Contracts/IBackupService.cs @@ -33,13 +33,13 @@ public interface IBackupService BackupProgress GetRestoreProgress(int tenantId); BackupProgress GetTransferProgress(int tenantId); Task> GetBackupHistory(int tenantId); - ScheduleResponse GetSchedule(int tenantId); + Task GetScheduleAsync(int tenantId); string GetTmpFolder(); - void CreateSchedule(CreateScheduleRequest request); - Task DeleteAllBackups(int tenantId); + Task CreateScheduleAsync(CreateScheduleRequest request); + Task DeleteAllBackupsAsync(int tenantId); Task DeleteBackup(Guid backupId); - void DeleteSchedule(int tenantId); + Task DeleteScheduleAsync(int tenantId); void StartBackup(StartBackupRequest request); - void StartRestore(StartRestoreRequest request); + Task StartRestoreAsync(StartRestoreRequest request); void StartTransfer(StartTransferRequest request); } diff --git a/common/ASC.Data.Backup.Core/Service/BackupService.cs b/common/ASC.Data.Backup.Core/Service/BackupService.cs index 2247fc4ef0..b9c07d861f 100644 --- a/common/ASC.Data.Backup.Core/Service/BackupService.cs +++ b/common/ASC.Data.Backup.Core/Service/BackupService.cs @@ -60,8 +60,8 @@ public class BackupService : IBackupService public async Task DeleteBackup(Guid id) { - var backupRecord = _backupRepository.GetBackupRecord(id); - _backupRepository.DeleteBackupRecord(backupRecord.Id); + var backupRecord = await _backupRepository.GetBackupRecordAsync(id); + await _backupRepository.DeleteBackupRecordAsync(backupRecord.Id); var storage = _backupStorageFactory.GetBackupStorage(backupRecord); if (storage == null) @@ -72,13 +72,13 @@ public class BackupService : IBackupService await storage.Delete(backupRecord.StoragePath); } - public async Task DeleteAllBackups(int tenantId) + public async Task DeleteAllBackupsAsync(int tenantId) { - foreach (var backupRecord in _backupRepository.GetBackupRecordsByTenantId(tenantId)) + foreach (var backupRecord in await _backupRepository.GetBackupRecordsByTenantIdAsync(tenantId)) { try { - _backupRepository.DeleteBackupRecord(backupRecord.Id); + await _backupRepository.DeleteBackupRecordAsync(backupRecord.Id); var storage = _backupStorageFactory.GetBackupStorage(backupRecord); if (storage == null) { @@ -97,7 +97,7 @@ public class BackupService : IBackupService public async Task> GetBackupHistory(int tenantId) { var backupHistory = new List(); - foreach (var record in _backupRepository.GetBackupRecordsByTenantId(tenantId)) + foreach (var record in await _backupRepository.GetBackupRecordsByTenantIdAsync(tenantId)) { var storage = _backupStorageFactory.GetBackupStorage(record); if (storage == null) @@ -118,7 +118,7 @@ public class BackupService : IBackupService } else { - _backupRepository.DeleteBackupRecord(record.Id); + await _backupRepository.DeleteBackupRecordAsync(record.Id); } } return backupHistory; @@ -133,7 +133,7 @@ public class BackupService : IBackupService } } - public void StartRestore(StartRestoreRequest request) + public async Task StartRestoreAsync(StartRestoreRequest request) { if (request.StorageType == BackupStorageType.Local) { @@ -145,7 +145,7 @@ public class BackupService : IBackupService if (!request.BackupId.Equals(Guid.Empty)) { - var backupRecord = _backupRepository.GetBackupRecord(request.BackupId); + var backupRecord = await _backupRepository.GetBackupRecordAsync(request.BackupId); if (backupRecord == null) { throw new FileNotFoundException(); @@ -183,9 +183,9 @@ public class BackupService : IBackupService return _backupWorker.TempFolder; } - public void CreateSchedule(CreateScheduleRequest request) + public Task CreateScheduleAsync(CreateScheduleRequest request) { - _backupRepository.SaveBackupSchedule( + return _backupRepository.SaveBackupScheduleAsync( new BackupSchedule() { TenantId = request.TenantId, @@ -197,14 +197,14 @@ public class BackupService : IBackupService }); } - public void DeleteSchedule(int tenantId) + public Task DeleteScheduleAsync(int tenantId) { - _backupRepository.DeleteBackupSchedule(tenantId); + return _backupRepository.DeleteBackupScheduleAsync(tenantId); } - public ScheduleResponse GetSchedule(int tenantId) + public async Task GetScheduleAsync(int tenantId) { - var schedule = _backupRepository.GetBackupSchedule(tenantId); + var schedule = await _backupRepository.GetBackupScheduleAsync(tenantId); if (schedule != null) { var tmp = new ScheduleResponse diff --git a/common/ASC.Data.Backup.Core/Service/ProgressItems/BackupProgressItem.cs b/common/ASC.Data.Backup.Core/Service/ProgressItems/BackupProgressItem.cs index 33a968fc96..1984427469 100644 --- a/common/ASC.Data.Backup.Core/Service/ProgressItems/BackupProgressItem.cs +++ b/common/ASC.Data.Backup.Core/Service/ProgressItems/BackupProgressItem.cs @@ -137,7 +137,7 @@ public class BackupProgressItem : BaseBackupProgressItem var repo = _backupRepository; - repo.SaveBackupRecord( + await repo.SaveBackupRecordAsync( new BackupRecord { Id = Guid.Parse(Id), diff --git a/common/ASC.Data.Backup.Core/Service/ProgressItems/RestoreProgressItem.cs b/common/ASC.Data.Backup.Core/Service/ProgressItems/RestoreProgressItem.cs index 62e49651a3..205f6990da 100644 --- a/common/ASC.Data.Backup.Core/Service/ProgressItems/RestoreProgressItem.cs +++ b/common/ASC.Data.Backup.Core/Service/ProgressItems/RestoreProgressItem.cs @@ -132,7 +132,7 @@ public class RestoreProgressItem : BaseBackupProgressItem if (!_coreBaseSettings.Standalone) { var backupHash = BackupWorker.GetBackupHash(tempFile); - var record = _backupRepository.GetBackupRecord(backupHash, TenantId); + var record = await _backupRepository.GetBackupRecordAsync(backupHash, TenantId); if (record == null) { diff --git a/common/ASC.Data.Backup.Core/Storage/BackupRepository.cs b/common/ASC.Data.Backup.Core/Storage/BackupRepository.cs index 0a88181341..1b27a43ba8 100644 --- a/common/ASC.Data.Backup.Core/Storage/BackupRepository.cs +++ b/common/ASC.Data.Backup.Core/Storage/BackupRepository.cs @@ -38,48 +38,48 @@ public class BackupRepository : IBackupRepository _creatorDbContext = creatorDbContext; } - public void SaveBackupRecord(BackupRecord backup) + public async Task SaveBackupRecordAsync(BackupRecord backup) { - using var backupContext = _dbContextFactory.CreateDbContext(); - backupContext.AddOrUpdate(backupContext.Backups, backup); - backupContext.SaveChanges(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + await backupContext.AddOrUpdateAsync(b => b.Backups, backup); + await backupContext.SaveChangesAsync(); } - public BackupRecord GetBackupRecord(Guid id) + public async Task GetBackupRecordAsync(Guid id) { - using var backupContext = _dbContextFactory.CreateDbContext(); - return backupContext.Backups.Find(id); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + return await backupContext.Backups.FindAsync(id); } - public BackupRecord GetBackupRecord(string hash, int tenant) + public async Task GetBackupRecordAsync(string hash, int tenant) { - using var backupContext = _dbContextFactory.CreateDbContext(); - return backupContext.Backups.AsNoTracking().AsQueryable().SingleOrDefault(b => b.Hash == hash && b.TenantId == tenant); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + return await backupContext.Backups.AsNoTracking().SingleOrDefaultAsync(b => b.Hash == hash && b.TenantId == tenant); } - public List GetExpiredBackupRecords() + public async Task> GetExpiredBackupRecordsAsync() { - using var backupContext = _dbContextFactory.CreateDbContext(); - return backupContext.Backups.AsNoTracking().Where(b => b.ExpiresOn != DateTime.MinValue && b.ExpiresOn <= DateTime.UtcNow).ToList(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + return await backupContext.Backups.AsNoTracking().Where(b => b.ExpiresOn != DateTime.MinValue && b.ExpiresOn <= DateTime.UtcNow).ToListAsync(); } - public List GetScheduledBackupRecords() + public async Task> GetScheduledBackupRecordsAsync() { - using var backupContext = _dbContextFactory.CreateDbContext(); - return backupContext.Backups.AsNoTracking().AsQueryable().Where(b => b.IsScheduled == true && b.Removed == false).ToList(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + return await backupContext.Backups.AsNoTracking().Where(b => b.IsScheduled == true && b.Removed == false).ToListAsync(); } - public List GetBackupRecordsByTenantId(int tenantId) + public async Task> GetBackupRecordsByTenantIdAsync(int tenantId) { - using var backupContext = _dbContextFactory.CreateDbContext(); - return backupContext.Backups.AsNoTracking().AsQueryable().Where(b => b.TenantId == tenantId && b.Removed == false).ToList(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + return await backupContext.Backups.AsNoTracking().Where(b => b.TenantId == tenantId && b.Removed == false).ToListAsync(); } - public void MigrationBackupRecords(int tenantId, int newTenantId, string region) + public async Task MigrationBackupRecordsAsync(int tenantId, int newTenantId, string region) { - using var backupContext = _dbContextFactory.CreateDbContext(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); - var backups = backupContext.Backups.AsNoTracking().AsQueryable().Where(b => b.TenantId == tenantId).ToList(); + var backups = await backupContext.Backups.AsNoTracking().Where(b => b.TenantId == tenantId).ToListAsync(); backups.ForEach(backup => { @@ -88,52 +88,47 @@ public class BackupRepository : IBackupRepository }); var backupContextByNewTenant = _creatorDbContext.CreateDbContext(region); - backupContextByNewTenant.Backups.AddRange(backups); - backupContextByNewTenant.SaveChanges(); + await backupContextByNewTenant.Backups.AddRangeAsync(backups); + await backupContextByNewTenant.SaveChangesAsync(); } - public void DeleteBackupRecord(Guid id) + public async Task DeleteBackupRecordAsync(Guid id) { - using var backupContext = _dbContextFactory.CreateDbContext(); - var backup = backupContext.Backups.Find(id); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); - if (backup != null) - { - backup.Removed = true; - backupContext.Backups.Update(backup); - backupContext.SaveChanges(); - } + await backupContext.Backups.Where(q => q.Id == id) + .ExecuteUpdateAsync(q => q.SetProperty(b => b.Removed, b => true)); } - public void SaveBackupSchedule(BackupSchedule schedule) + public async Task SaveBackupScheduleAsync(BackupSchedule schedule) { - using var backupContext = _dbContextFactory.CreateDbContext(); - backupContext.AddOrUpdate(backupContext.Schedules, schedule); - backupContext.SaveChanges(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + await backupContext.AddOrUpdateAsync(q => q.Schedules, schedule); + await backupContext.SaveChangesAsync(); } - public void DeleteBackupSchedule(int tenantId) + public async Task DeleteBackupScheduleAsync(int tenantId) { - using var backupContext = _dbContextFactory.CreateDbContext(); - backupContext.Schedules.Where(s => s.TenantId == tenantId).ExecuteDelete(); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + await backupContext.Schedules.Where(s => s.TenantId == tenantId).ExecuteDeleteAsync(); } - public List GetBackupSchedules() + public async Task> GetBackupSchedulesAsync() { - using var backupContext = _dbContextFactory.CreateDbContext(); - var query = backupContext.Schedules.AsQueryable().Join(backupContext.Tenants, + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + var query = backupContext.Schedules.Join(backupContext.Tenants, s => s.TenantId, t => t.Id, (s, t) => new { schedule = s, tenant = t }) .Where(q => q.tenant.Status == TenantStatus.Active) .Select(q => q.schedule); - return query.ToList(); + return await query.ToListAsync(); } - public BackupSchedule GetBackupSchedule(int tenantId) + public async Task GetBackupScheduleAsync(int tenantId) { - using var backupContext = _dbContextFactory.CreateDbContext(); - return backupContext.Schedules.AsNoTracking().SingleOrDefault(s => s.TenantId == tenantId); + using var backupContext = await _dbContextFactory.CreateDbContextAsync(); + return await backupContext.Schedules.AsNoTracking().SingleOrDefaultAsync(s => s.TenantId == tenantId); } } diff --git a/common/ASC.Data.Backup.Core/Storage/IBackupRepository.cs b/common/ASC.Data.Backup.Core/Storage/IBackupRepository.cs index 1f70f63ce6..6448ff6a2c 100644 --- a/common/ASC.Data.Backup.Core/Storage/IBackupRepository.cs +++ b/common/ASC.Data.Backup.Core/Storage/IBackupRepository.cs @@ -28,15 +28,15 @@ namespace ASC.Data.Backup.Storage; public interface IBackupRepository { - BackupRecord GetBackupRecord(Guid id); - BackupRecord GetBackupRecord(string hash, int tenant); - BackupSchedule GetBackupSchedule(int tenantId); - List GetBackupRecordsByTenantId(int tenantId); - List GetExpiredBackupRecords(); - List GetScheduledBackupRecords(); - List GetBackupSchedules(); - void DeleteBackupRecord(Guid id); - void DeleteBackupSchedule(int tenantId); - void SaveBackupRecord(BackupRecord backupRecord); - void SaveBackupSchedule(BackupSchedule schedule); + Task GetBackupRecordAsync(Guid id); + Task GetBackupRecordAsync(string hash, int tenant); + Task GetBackupScheduleAsync(int tenantId); + Task> GetBackupRecordsByTenantIdAsync(int tenantId); + Task> GetExpiredBackupRecordsAsync(); + Task> GetScheduledBackupRecordsAsync(); + Task> GetBackupSchedulesAsync(); + Task DeleteBackupRecordAsync(Guid id); + Task DeleteBackupScheduleAsync(int tenantId); + Task SaveBackupRecordAsync(BackupRecord backupRecord); + Task SaveBackupScheduleAsync(BackupSchedule schedule); } diff --git a/common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs b/common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs index e462d34e5a..a57655e115 100644 --- a/common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs +++ b/common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs @@ -261,8 +261,8 @@ public class BackupPortalTask : PortalTaskBase private async Task> GetFiles(int tenantId) { var files = (await GetFilesToProcess(tenantId)).ToList(); - using var backupRecordContext = _dbContextFactory.CreateDbContext(); - var exclude = backupRecordContext.Backups.AsQueryable().Where(b => b.TenantId == tenantId && b.StorageType == 0 && b.StoragePath != null).ToList(); + using var backupRecordContext = await _dbContextFactory.CreateDbContextAsync(); + var exclude = await backupRecordContext.Backups.Where(b => b.TenantId == tenantId && b.StorageType == 0 && b.StoragePath != null).ToListAsync(); files = files.Where(f => !exclude.Any(e => f.Path.Replace('\\', '/').Contains($"/file_{e.StoragePath}/"))).ToList(); return files; @@ -625,8 +625,8 @@ public class BackupPortalTask : PortalTaskBase { var files = (await GetFilesToProcess(TenantId)).ToList(); - using var backupRecordContext = _dbContextFactory.CreateDbContext(); - var exclude = backupRecordContext.Backups.AsQueryable().Where(b => b.TenantId == TenantId && b.StorageType == 0 && b.StoragePath != null).ToList(); + using var backupRecordContext = await _dbContextFactory.CreateDbContextAsync(); + var exclude = await backupRecordContext.Backups.Where(b => b.TenantId == TenantId && b.StorageType == 0 && b.StoragePath != null).ToListAsync(); files = files.Where(f => !exclude.Any(e => f.Path.Replace('\\', '/').Contains($"/file_{e.StoragePath}/"))).ToList(); diff --git a/common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs b/common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs index d46d1587e4..5ecee3bbad 100644 --- a/common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs +++ b/common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs @@ -118,7 +118,7 @@ public class RestorePortalTask : PortalTaskBase await restoreTask.RunJob(); } - _backupRepository.MigrationBackupRecords(TenantId, _columnMapper.GetTenantMapping(), _region); + await _backupRepository.MigrationBackupRecordsAsync(TenantId, _columnMapper.GetTenantMapping(), _region); } _options.DebugEndRestoreData(); diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupDeleteScheldureRequestedIntegrationEventHandler.cs b/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupDeleteScheldureRequestedIntegrationEventHandler.cs index e0708f885e..05f79b4e4f 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupDeleteScheldureRequestedIntegrationEventHandler.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupDeleteScheldureRequestedIntegrationEventHandler.cs @@ -46,9 +46,7 @@ public class BackupDeleteScheldureRequestedIntegrationEventHandler : IIntegratio { _logger.InformationHandlingIntegrationEvent(@event.Id, Program.AppName, @event); - _backupService.DeleteSchedule(@event.TenantId); - - await Task.CompletedTask; + await _backupService.DeleteScheduleAsync(@event.TenantId); } } } \ No newline at end of file diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRestoreRequestedIntegrationEventHandler.cs b/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRestoreRequestedIntegrationEventHandler.cs index ec09069baa..cf7f51ce05 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRestoreRequestedIntegrationEventHandler.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRestoreRequestedIntegrationEventHandler.cs @@ -69,7 +69,7 @@ public class BackupRestoreRequestedIntegrationEventHandler : IIntegrationEventHa _tenantManager.SetCurrentTenant(@event.TenantId); _securityContext.AuthenticateMeWithoutCookie(_authManager.GetAccountByID(@event.TenantId, @event.CreateBy)); - _backupAjaxHandler.StartRestore(@event.BackupId, + await _backupAjaxHandler.StartRestoreAsync(@event.BackupId, @event.StorageType, @event.StorageParams, @event.Notify); diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs index a4845a1d54..808bfbf9b9 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs @@ -66,7 +66,7 @@ internal sealed class BackupCleanerService : BackgroundService continue; } - await ExecuteBackupCleaner(stoppingToken); + await ExecuteBackupCleanerAsync(stoppingToken); await Task.Delay(_backupCleanerPeriod, stoppingToken); } @@ -74,7 +74,7 @@ internal sealed class BackupCleanerService : BackgroundService _logger.DebugBackupCleanerServiceStopping(); } - private async Task ExecuteBackupCleaner(CancellationToken stoppingToken) + private async Task ExecuteBackupCleanerAsync(CancellationToken stoppingToken) { await using var serviceScope = _scopeFactory.CreateAsyncScope(); @@ -83,18 +83,18 @@ internal sealed class BackupCleanerService : BackgroundService _logger.DebugStartedClean(); - var backupsToRemove = backupRepository.GetExpiredBackupRecords(); + var backupsToRemove = await backupRepository.GetExpiredBackupRecordsAsync(); _logger.DebugFoundBackups(backupsToRemove.Count); - foreach (var scheduledBackups in backupRepository.GetScheduledBackupRecords().GroupBy(r => r.TenantId)) + foreach (var scheduledBackups in (await backupRepository.GetScheduledBackupRecordsAsync()).GroupBy(r => r.TenantId)) { if (stoppingToken.IsCancellationRequested) { return; } - var schedule = backupRepository.GetBackupSchedule(scheduledBackups.Key); + var schedule = await backupRepository.GetBackupScheduleAsync(scheduledBackups.Key); if (schedule != null) { @@ -128,7 +128,7 @@ internal sealed class BackupCleanerService : BackgroundService await backupStorage.Delete(backupRecord.StoragePath); - backupRepository.DeleteBackupRecord(backupRecord.Id); + await backupRepository.DeleteBackupRecordAsync(backupRecord.Id); } catch (ProviderInfoArgumentException error) { @@ -136,7 +136,7 @@ internal sealed class BackupCleanerService : BackgroundService if (DateTime.UtcNow > backupRecord.CreatedOn.AddMonths(6)) { - backupRepository.DeleteBackupRecord(backupRecord.Id); + await backupRepository.DeleteBackupRecordAsync(backupRecord.Id); } } catch (Exception error) diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupListenerService.cs b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupListenerService.cs index f9347a14ec..83a5a3e3dc 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupListenerService.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupListenerService.cs @@ -39,19 +39,19 @@ internal sealed class BackupListenerService : IHostedService _scopeFactory = scopeFactory; } - public void DeleteScheldure(DeleteSchedule deleteSchedule) + public async Task DeleteScheldureAsync(DeleteSchedule deleteSchedule) { using (var scope = _scopeFactory.CreateScope()) { var backupService = scope.ServiceProvider.GetService(); - backupService.DeleteSchedule(deleteSchedule.TenantId); + await backupService.DeleteScheduleAsync(deleteSchedule.TenantId); } } public Task StartAsync(CancellationToken cancellationToken) { - _cacheDeleteSchedule.Subscribe((n) => DeleteScheldure(n), CacheNotifyAction.Insert); + _cacheDeleteSchedule.Subscribe((n) => DeleteScheldureAsync(n), CacheNotifyAction.Insert); return Task.CompletedTask; } diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs index c27e1fb1ed..e32e8330e5 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs @@ -73,7 +73,7 @@ public sealed class BackupSchedulerService : BackgroundService _logger.DebugBackupSchedulerServiceDoingWork(); - ExecuteBackupScheduler(stoppingToken); + await ExecuteBackupSchedulerAsync(stoppingToken); await Task.Delay(_backupSchedulerPeriod, stoppingToken); } @@ -81,7 +81,7 @@ public sealed class BackupSchedulerService : BackgroundService _logger.DebugBackupSchedulerServiceStopping(); } - private void ExecuteBackupScheduler(CancellationToken stoppingToken) + private async Task ExecuteBackupSchedulerAsync(CancellationToken stoppingToken) { using var serviceScope = _scopeFactory.CreateScope(); @@ -92,7 +92,7 @@ public sealed class BackupSchedulerService : BackgroundService _logger.DebugStartedToSchedule(); - var backupsToSchedule = backupRepository.GetBackupSchedules().Where(schedule => backupSchedule.IsToBeProcessed(schedule)).ToList(); + var backupsToSchedule = (await backupRepository.GetBackupSchedulesAsync()).Where(schedule => backupSchedule.IsToBeProcessed(schedule)).ToList(); _logger.DebugBackupsSchedule(backupsToSchedule.Count); @@ -113,7 +113,7 @@ public sealed class BackupSchedulerService : BackgroundService { schedule.LastBackupTime = DateTime.UtcNow; - backupRepository.SaveBackupSchedule(schedule); + await backupRepository.SaveBackupScheduleAsync(schedule); _logger.DebugStartScheduledBackup(schedule.TenantId, schedule.StorageType, schedule.StorageBasePath); diff --git a/common/services/ASC.Data.Backup/Api/BackupController.cs b/common/services/ASC.Data.Backup/Api/BackupController.cs index 1ec704a681..742ef0405e 100644 --- a/common/services/ASC.Data.Backup/Api/BackupController.cs +++ b/common/services/ASC.Data.Backup/Api/BackupController.cs @@ -53,9 +53,9 @@ public class BackupController : ControllerBase /// Backup /// Backup Schedule [HttpGet("getbackupschedule")] - public BackupAjaxHandler.Schedule GetBackupSchedule() + public Task GetBackupSchedule() { - return _backupHandler.GetSchedule(); + return _backupHandler.GetScheduleAsync(); } /// @@ -87,9 +87,9 @@ public class BackupController : ControllerBase /// /// Backup [HttpDelete("deletebackupschedule")] - public bool DeleteBackupSchedule() + public async Task DeleteBackupSchedule() { - _backupHandler.DeleteSchedule(); + await _backupHandler.DeleteScheduleAsync(); return true; } diff --git a/web/ASC.Web.Api/Api/Settings/StorageController.cs b/web/ASC.Web.Api/Api/Settings/StorageController.cs index ffea8a947f..80df50582e 100644 --- a/web/ASC.Web.Api/Api/Settings/StorageController.cs +++ b/web/ASC.Web.Api/Api/Settings/StorageController.cs @@ -438,11 +438,11 @@ public class StorageController : BaseSettingsController } [HttpGet("storage/backup")] - public List GetAllBackupStorages() + public async Task> GetAllBackupStorages() { _permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings); - var schedule = _backupAjaxHandler.GetSchedule(); + var schedule = await _backupAjaxHandler.GetScheduleAsync(); var current = new StorageSettings(); if (schedule != null && schedule.StorageType == BackupStorageType.ThirdPartyConsumer)