From b5b8b1edf0629ff032efbaacdd1a883a43b723b3 Mon Sep 17 00:00:00 2001 From: Alexey Bannov Date: Thu, 24 Feb 2022 19:53:49 +0300 Subject: [PATCH] added support scheduler backup request handling --- build/run/BackupBackgroundTasks.bat | 4 ++ .../Events/BackupRequestIntegrationEvent.cs | 13 ++++++- .../ASC.Data.Backup.BackgroundTasks.csproj | 1 + .../GlobalUsings.cs | 19 +++++----- ...> BackupRequestIntegrationEventHandler.cs} | 38 ++++++++++++------- .../Program.cs | 9 +---- .../Services/BackupCleanerService.cs | 2 - .../Services/BackupSchedulerService.cs | 16 +++++--- .../Startup.cs | 3 +- common/services/ASC.Data.Backup/Startup.cs | 2 - 10 files changed, 63 insertions(+), 44 deletions(-) create mode 100644 build/run/BackupBackgroundTasks.bat rename common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/{BackupRequesteIntegrationEventHandler.cs => BackupRequestIntegrationEventHandler.cs} (50%) diff --git a/build/run/BackupBackgroundTasks.bat b/build/run/BackupBackgroundTasks.bat new file mode 100644 index 0000000000..660cbb27c0 --- /dev/null +++ b/build/run/BackupBackgroundTasks.bat @@ -0,0 +1,4 @@ +@echo off + +PUSHD %~dp0..\.. +set servicepath=%cd%\common\services\ASC.Data.Backup.BackgroundTasks\bin\Debug\ASC.Data.Backup.BackgroundTasks.exe urls=http://0.0.0.0:5013 $STORAGE_ROOT=%cd%\Data log:dir=%cd%\Logs log:name=backup.backgroundtasks pathToConf=%cd%\config core:products:folder=%cd%\products \ No newline at end of file diff --git a/common/ASC.Data.Backup.Core/IntegrationEvents/Events/BackupRequestIntegrationEvent.cs b/common/ASC.Data.Backup.Core/IntegrationEvents/Events/BackupRequestIntegrationEvent.cs index b071a7ad24..49c5d6ff65 100644 --- a/common/ASC.Data.Backup.Core/IntegrationEvents/Events/BackupRequestIntegrationEvent.cs +++ b/common/ASC.Data.Backup.Core/IntegrationEvents/Events/BackupRequestIntegrationEvent.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; - using ASC.Data.Backup.Contracts; using ASC.EventBus.Events; @@ -12,14 +11,24 @@ public record BackupRequestIntegrationEvent : IntegrationEvent int tenantId, Guid createBy, Dictionary storageParams, - bool backupMail) : base(createBy, tenantId) + bool backupMail, + bool isScheduled = false, + int backupsStored = 0, + string storageBasePath = "") : base(createBy, tenantId) { StorageType = storageType; StorageParams = storageParams; BackupMail = backupMail; + IsScheduled = isScheduled; + BackupsStored = backupsStored; + StorageBasePath = storageBasePath; } public BackupStorageType StorageType { get; private init; } public Dictionary StorageParams { get; private init; } public bool BackupMail { get; private init; } + public bool IsScheduled { get; private init; } + public int BackupsStored { get; private init; } + public string StorageBasePath { get; private init; } } + diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/ASC.Data.Backup.BackgroundTasks.csproj b/common/services/ASC.Data.Backup.BackgroundTasks/ASC.Data.Backup.BackgroundTasks.csproj index dbdb2290c3..b60e2e3158 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/ASC.Data.Backup.BackgroundTasks.csproj +++ b/common/services/ASC.Data.Backup.BackgroundTasks/ASC.Data.Backup.BackgroundTasks.csproj @@ -7,6 +7,7 @@ false false false + enable diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/GlobalUsings.cs b/common/services/ASC.Data.Backup.BackgroundTasks/GlobalUsings.cs index c47b66b24d..54a9430699 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/GlobalUsings.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/GlobalUsings.cs @@ -1,19 +1,17 @@ -global using System; -global using System.Collections.Generic; -global using System.IO; -global using System.Linq; -global using System.Runtime.InteropServices; -global using System.Threading; -global using System.Threading.Tasks; - +global using System.Runtime.InteropServices; global using ASC.Api.Core; +global using ASC.Api.Core.Extensions; global using ASC.Common; global using ASC.Common.Caching; global using ASC.Common.Logging; global using ASC.Common.Utils; global using ASC.Core; global using ASC.Core.Billing; +global using ASC.Core.Common.Hosting; +global using ASC.Core.Common.Hosting.Interfaces; +global using ASC.Data.Backup.BackgroundTasks; global using ASC.Data.Backup.Contracts; +global using ASC.Data.Backup.Core.IntegrationEvents.Events; global using ASC.Data.Backup.IntegrationEvents.EventHandling; global using ASC.Data.Backup.Services; global using ASC.Data.Backup.Storage; @@ -29,6 +27,7 @@ global using Microsoft.AspNetCore.Hosting; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Hosting; +global using Microsoft.Extensions.Hosting.WindowsServices; global using Microsoft.Extensions.Options; -global using ASC.Api.Core.Extensions; -global using ASC.Core.Common.Hosting.Interfaces; \ No newline at end of file + +global using Newtonsoft.Json; diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRequesteIntegrationEventHandler.cs b/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRequestIntegrationEventHandler.cs similarity index 50% rename from common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRequesteIntegrationEventHandler.cs rename to common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRequestIntegrationEventHandler.cs index 23586c454b..ae0e5cac55 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRequesteIntegrationEventHandler.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/IntegrationEvents/EventHandling/BackupRequestIntegrationEventHandler.cs @@ -1,31 +1,29 @@ -using ASC.Core; -using ASC.Data.Backup.Core.IntegrationEvents.Events; -using ASC.EventBus.Exceptions; - -namespace ASC.Data.Backup.IntegrationEvents.EventHandling; +namespace ASC.Data.Backup.IntegrationEvents.EventHandling; [Scope] -public class BackupRequesteIntegrationEventHandler : IIntegrationEventHandler +public class BackupRequestIntegrationEventHandler : IIntegrationEventHandler { private readonly BackupAjaxHandler _backupAjaxHandler; private readonly ILog _logger; private readonly TenantManager _tenantManager; private readonly SecurityContext _securityContext; private readonly AuthManager _authManager; + private readonly BackupWorker _backupWorker; - public BackupRequesteIntegrationEventHandler( + public BackupRequestIntegrationEventHandler( BackupAjaxHandler backupAjaxHandler, IOptionsMonitor logger, TenantManager tenantManager, SecurityContext securityContext, - AuthManager authManager - ) + AuthManager authManager, + BackupWorker backupWorker) { _tenantManager = tenantManager; _authManager = authManager; _securityContext = securityContext; _backupAjaxHandler = backupAjaxHandler; - _logger = logger.CurrentValue; + _logger = logger.CurrentValue; + _backupWorker = backupWorker; } public async Task Handle(BackupRequestIntegrationEvent @event) @@ -36,11 +34,23 @@ public class BackupRequesteIntegrationEventHandler : IIntegrationEventHandler(); -eventBus.Subscribe(); +eventBus.Subscribe(); app.Run(); diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs index 1eca2c6d45..ebfe2dc62b 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupCleanerService.cs @@ -23,8 +23,6 @@ * */ -using ASC.Core.Common.Hosting; - namespace ASC.Data.Backup.Services; [Singletone] diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs index f00673a7ac..b399b9aaf3 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/Services/BackupSchedulerService.cs @@ -23,9 +23,6 @@ * */ -using ASC.Core.Common.Hosting; -using ASC.ElasticSearch.Service; - namespace ASC.Data.Backup.Services; [Singletone] @@ -34,7 +31,6 @@ public sealed class BackupSchedulerService : BackgroundService private readonly TimeSpan _backupSchedulerPeriod; private readonly ILog _logger; - private readonly BackupWorker _backupWorker; private readonly CoreBaseSettings _coreBaseSettings; private readonly IServiceScopeFactory _scopeFactory; private readonly IEventBus _eventBus; @@ -50,7 +46,6 @@ public sealed class BackupSchedulerService : BackgroundService { _logger = options.CurrentValue; _coreBaseSettings = coreBaseSettings; - _backupWorker = backupWorker; _backupSchedulerPeriod = configuration.GetSetting("backup").Scheduler.Period; _scopeFactory = scopeFactory; _eventBus = eventBus; @@ -120,7 +115,16 @@ public sealed class BackupSchedulerService : BackgroundService _logger.DebugFormat("Start scheduled backup: {0}, {1}, {2}, {3}", schedule.TenantId, schedule.BackupMail, schedule.StorageType, schedule.StorageBasePath); - _backupWorker.StartScheduledBackup(schedule); + _eventBus.Publish(new BackupRequestIntegrationEvent( + tenantId: schedule.TenantId, + storageBasePath: schedule.StorageBasePath, + storageParams: JsonConvert.DeserializeObject>(schedule.StorageParams), + storageType: schedule.StorageType, + backupMail: schedule.BackupMail, + createBy: ASC.Core.Configuration.Constants.CoreSystem.ID, + isScheduled: true, + backupsStored: schedule.BackupsStored + )); } else { diff --git a/common/services/ASC.Data.Backup.BackgroundTasks/Startup.cs b/common/services/ASC.Data.Backup.BackgroundTasks/Startup.cs index 785e015fc4..65d9960fa7 100644 --- a/common/services/ASC.Data.Backup.BackgroundTasks/Startup.cs +++ b/common/services/ASC.Data.Backup.BackgroundTasks/Startup.cs @@ -41,7 +41,8 @@ public class Startup : BaseStartup NotifyConfigurationExtension.Register(DIHelper); - DIHelper.TryAdd(); + DIHelper.TryAdd(); + DIHelper.TryAdd(); DIHelper.TryAdd(); services.AddHostedService(); diff --git a/common/services/ASC.Data.Backup/Startup.cs b/common/services/ASC.Data.Backup/Startup.cs index 28beeadbc6..9c91d7490d 100644 --- a/common/services/ASC.Data.Backup/Startup.cs +++ b/common/services/ASC.Data.Backup/Startup.cs @@ -43,8 +43,6 @@ public class Startup : BaseStartup DIHelper.TryAdd(); DIHelper.TryAdd(); - DIHelper.TryAdd(); - DIHelper.TryAdd(); NotifyConfigurationExtension.Register(DIHelper);