DocSpace-client/common/services/ASC.Data.Backup/Service/BackupWorker.cs

880 lines
33 KiB
C#
Raw Normal View History

2020-06-08 10:40:26 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2020
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-05-29 14:48:27 +00:00
using System.Threading;
2020-06-08 10:40:26 +00:00
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.Logging;
using ASC.Common.Threading.Progress;
using ASC.Core;
using ASC.Core.Billing;
using ASC.Core.Tenants;
2020-06-23 10:48:36 +00:00
using ASC.Data.Backup.Contracts;
2020-06-08 10:40:26 +00:00
using ASC.Data.Backup.EF.Model;
using ASC.Data.Backup.Storage;
using ASC.Data.Backup.Tasks;
using ASC.Data.Backup.Tasks.Modules;
using ASC.Data.Backup.Utils;
2020-05-29 14:48:27 +00:00
using ASC.Data.Storage;
2020-06-03 09:15:05 +00:00
using Microsoft.Extensions.DependencyInjection;
2020-06-08 08:17:54 +00:00
using Microsoft.Extensions.Options;
2020-05-29 14:48:27 +00:00
using Newtonsoft.Json;
2020-06-08 10:40:26 +00:00
namespace ASC.Data.Backup.Service
{
2020-06-23 10:48:36 +00:00
public class BackupWorker
2020-06-08 10:40:26 +00:00
{
private ILog Log { get; set; }
private ProgressQueue<BaseBackupProgressItem> ProgressQueue { get; set; }
internal string TempFolder { get; set; }
private string CurrentRegion { get; set; }
private Dictionary<string, string> ConfigPaths { get; set; }
private int Limit { get; set; }
private string UpgradesPath { get; set; }
2020-06-16 11:34:44 +00:00
private ICacheNotify<BackupProgress> CacheBackupProgress { get; }
private FactoryProgressItem FactoryProgressItem { get; set; }
2020-05-29 14:48:27 +00:00
public BackupWorker(
IOptionsMonitor<ILog> options,
2020-06-16 11:34:44 +00:00
ICacheNotify<BackupProgress> cacheBackupProgress,
2020-06-08 08:17:54 +00:00
ProgressQueueOptionsManager<BaseBackupProgressItem> progressQueue,
2020-06-15 15:33:53 +00:00
FactoryProgressItem factoryProgressItem)
2020-06-08 10:40:26 +00:00
{
2020-05-29 14:48:27 +00:00
Log = options.CurrentValue;
2020-06-08 10:40:26 +00:00
ProgressQueue = progressQueue.Value;
2020-06-16 11:34:44 +00:00
CacheBackupProgress = cacheBackupProgress;
2020-06-23 10:48:36 +00:00
FactoryProgressItem = factoryProgressItem;
2020-06-08 10:40:26 +00:00
}
public void Start(BackupSettings settings)
{
TempFolder = PathHelper.ToRootedPath(settings.TempFolder);
if (!Directory.Exists(TempFolder))
{
2020-09-30 11:50:39 +00:00
_ = Directory.CreateDirectory(TempFolder);
2020-06-08 10:40:26 +00:00
}
Limit = settings.Limit;
UpgradesPath = settings.UpgradesPath;
CurrentRegion = settings.WebConfigs.CurrentRegion;
ConfigPaths = settings.WebConfigs.Elements.ToDictionary(el => el.Region, el => PathHelper.ToRootedConfigPath(el.Path));
ConfigPaths[CurrentRegion] = PathHelper.ToRootedConfigPath(settings.WebConfigs.CurrentPath);
2020-05-29 14:48:27 +00:00
2020-06-08 10:40:26 +00:00
var invalidConfigPath = ConfigPaths.Values.FirstOrDefault(path => !File.Exists(path));
if (invalidConfigPath != null)
{
Log.WarnFormat("Configuration file {0} not found", invalidConfigPath);
}
}
public void Stop()
{
if (ProgressQueue != null)
{
ProgressQueue.Terminate();
ProgressQueue = null;
}
}
public BackupProgress StartBackup(StartBackupRequest request)
{
lock (ProgressQueue.SynchRoot)
{
var item = ProgressQueue.GetItems().OfType<BackupProgressItem>().FirstOrDefault(t => t.TenantId == request.TenantId);
if (item != null && item.IsCompleted)
{
ProgressQueue.Remove(item);
item = null;
}
if (item == null)
{
2020-06-16 11:34:44 +00:00
item = FactoryProgressItem.CreateBackupProgressItem(request, false, TempFolder, Limit, CurrentRegion, ConfigPaths);
2020-06-08 10:40:26 +00:00
ProgressQueue.Add(item);
}
2020-06-16 11:34:44 +00:00
var progress = ToBackupProgress(item);
PublishProgress(progress);
return progress;
2020-06-08 10:40:26 +00:00
}
}
public void StartScheduledBackup(BackupSchedule schedule)
{
lock (ProgressQueue.SynchRoot)
{
var item = ProgressQueue.GetItems().OfType<BackupProgressItem>().FirstOrDefault(t => t.TenantId == schedule.TenantId);
if (item != null && item.IsCompleted)
{
ProgressQueue.Remove(item);
item = null;
}
if (item == null)
2020-06-03 09:15:05 +00:00
{
2020-06-16 11:34:44 +00:00
item = FactoryProgressItem.CreateBackupProgressItem(schedule, false, TempFolder, Limit, CurrentRegion, ConfigPaths);
2020-06-08 10:40:26 +00:00
ProgressQueue.Add(item);
}
}
}
public BackupProgress GetBackupProgress(int tenantId)
{
lock (ProgressQueue.SynchRoot)
{
return ToBackupProgress(ProgressQueue.GetItems().OfType<BackupProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
}
}
public void ResetBackupError(int tenantId)
{
lock (ProgressQueue.SynchRoot)
{
var progress = ProgressQueue.GetItems().OfType<BackupProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
if (progress != null)
{
progress.Error = null;
}
}
}
public void ResetRestoreError(int tenantId)
{
lock (ProgressQueue.SynchRoot)
{
var progress = ProgressQueue.GetItems().OfType<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
if (progress != null)
{
progress.Error = null;
}
}
}
public BackupProgress StartRestore(StartRestoreRequest request)
{
lock (ProgressQueue.SynchRoot)
{
var item = ProgressQueue.GetItems().OfType<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == request.TenantId);
if (item != null && item.IsCompleted)
{
ProgressQueue.Remove(item);
item = null;
}
if (item == null)
{
2020-06-16 11:34:44 +00:00
item = FactoryProgressItem.CreateRestoreProgressItem(request, TempFolder, UpgradesPath, CurrentRegion, ConfigPaths);
2020-06-08 10:40:26 +00:00
ProgressQueue.Add(item);
}
return ToBackupProgress(item);
}
}
public BackupProgress StartTransfer(int tenantId, string targetRegion, bool transferMail, bool notify)
{
lock (ProgressQueue.SynchRoot)
{
var item = ProgressQueue.GetItems().OfType<TransferProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
if (item != null && item.IsCompleted)
{
ProgressQueue.Remove(item);
item = null;
}
2020-06-16 11:34:44 +00:00
2020-06-08 10:40:26 +00:00
if (item == null)
{
2020-06-16 11:34:44 +00:00
item = FactoryProgressItem.CreateTransferProgressItem(targetRegion, transferMail, tenantId, TempFolder, Limit, notify, CurrentRegion, ConfigPaths);
2020-06-08 10:40:26 +00:00
ProgressQueue.Add(item);
}
2020-06-16 11:34:44 +00:00
2020-06-08 10:40:26 +00:00
return ToBackupProgress(item);
}
}
2020-06-16 11:34:44 +00:00
private BackupProgress ToBackupProgress(BaseBackupProgressItem progressItem)
2020-06-08 10:40:26 +00:00
{
if (progressItem == null)
{
return null;
}
2020-06-16 11:34:44 +00:00
var progress = new BackupProgress
{
IsCompleted = progressItem.IsCompleted,
Progress = (int)progressItem.Percentage,
Error = progressItem.Error != null ? ((Exception)progressItem.Error).Message : "",
TenantId = progressItem.TenantId,
BackupProgressEnum = progressItem.BackupProgressItemEnum.Convert()
};
if (progressItem is BackupProgressItem backupProgressItem && backupProgressItem.Link != null)
2020-06-08 10:40:26 +00:00
{
progress.Link = backupProgressItem.Link;
}
else
{
2020-06-16 11:34:44 +00:00
if (progressItem is TransferProgressItem transferProgressItem && transferProgressItem.Link != null)
2020-06-08 10:40:26 +00:00
{
progress.Link = transferProgressItem.Link;
}
}
return progress;
2020-06-08 08:17:54 +00:00
}
2020-06-16 11:34:44 +00:00
internal void PublishProgress(BaseBackupProgressItem progress)
{
PublishProgress(ToBackupProgress(progress));
}
internal void PublishProgress(BackupProgress progress)
{
CacheBackupProgress.Publish(progress, CacheNotifyAction.InsertOrUpdate);
}
}
public enum BackupProgressItemEnum
{
Backup,
Restore,
Transfer
}
public static class BackupProgressItemEnumConverter
{
2020-09-30 14:47:42 +00:00
public static BackupProgressEnum Convert(this BackupProgressItemEnum backupProgressItemEnum)
{
return backupProgressItemEnum switch
2020-06-16 11:34:44 +00:00
{
BackupProgressItemEnum.Backup => BackupProgressEnum.Backup,
BackupProgressItemEnum.Restore => BackupProgressEnum.Restore,
BackupProgressItemEnum.Transfer => BackupProgressEnum.Transfer,
_ => BackupProgressEnum.Backup
};
2020-09-30 14:47:42 +00:00
}
2020-06-08 08:17:54 +00:00
}
public abstract class BaseBackupProgressItem : IProgressItem
{
public object Id { get; set; }
public object Status { get; set; }
public object Error { get; set; }
public double Percentage { get; set; }
public bool IsCompleted { get; set; }
2020-06-16 11:34:44 +00:00
public int TenantId { get; set; }
public abstract BackupProgressItemEnum BackupProgressItemEnum { get; }
2020-06-08 08:17:54 +00:00
public abstract object Clone();
public abstract void RunJob();
}
2020-06-08 10:40:26 +00:00
2020-06-08 08:17:54 +00:00
public class BackupProgressItem : BaseBackupProgressItem
{
private const string ArchiveFormat = "tar.gz";
2020-06-16 11:34:44 +00:00
public override BackupProgressItemEnum BackupProgressItemEnum { get => BackupProgressItemEnum.Backup; }
private bool IsScheduled { get; set; }
private Guid UserId { get; set; }
private BackupStorageType StorageType { get; set; }
private string StorageBasePath { get; set; }
public bool BackupMail { get; set; }
public Dictionary<string, string> StorageParams { get; set; }
public string Link { get; private set; }
public string TempFolder { get; set; }
private string CurrentRegion { get; set; }
private Dictionary<string, string> ConfigPaths { get; set; }
private int Limit { get; set; }
private ILog Log { get; set; }
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; set; }
2020-06-08 11:57:45 +00:00
public BackupProgressItem(IServiceProvider serviceProvider, IOptionsMonitor<ILog> options)
{
Id = Guid.NewGuid();
2020-06-03 09:15:05 +00:00
Log = options.CurrentValue;
2020-06-08 10:40:26 +00:00
ServiceProvider = serviceProvider;
2020-06-03 09:15:05 +00:00
}
2020-06-08 11:57:45 +00:00
2020-06-03 09:15:05 +00:00
public void Init(BackupSchedule schedule, bool isScheduled, string tempFolder, int limit, string currentRegion, Dictionary<string, string> configPaths)
{
UserId = Guid.Empty;
TenantId = schedule.TenantId;
StorageType = schedule.StorageType;
StorageBasePath = schedule.StorageBasePath;
BackupMail = schedule.BackupMail;
StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(schedule.StorageParams);
IsScheduled = isScheduled;
TempFolder = tempFolder;
Limit = limit;
CurrentRegion = currentRegion;
ConfigPaths = configPaths;
2020-06-03 09:15:05 +00:00
}
2020-06-08 11:57:45 +00:00
2020-06-03 09:15:05 +00:00
public void Init(StartBackupRequest request, bool isScheduled, string tempFolder, int limit, string currentRegion, Dictionary<string, string> configPaths)
{
UserId = request.UserId;
2020-06-03 09:15:05 +00:00
TenantId = request.TenantId;
StorageType = request.StorageType;
2020-06-03 09:15:05 +00:00
StorageBasePath = request.StorageBasePath;
BackupMail = request.BackupMail;
2020-06-08 10:40:26 +00:00
StorageParams = request.StorageParams.ToDictionary(r => r.Key, r => r.Value);
2020-06-03 09:15:05 +00:00
IsScheduled = isScheduled;
TempFolder = tempFolder;
2020-06-03 09:15:05 +00:00
Limit = limit;
CurrentRegion = currentRegion;
ConfigPaths = configPaths;
2020-05-29 14:48:27 +00:00
}
2020-06-08 11:57:45 +00:00
2020-06-08 08:17:54 +00:00
public override void RunJob()
{
if (ThreadPriority.BelowNormal < Thread.CurrentThread.Priority)
2020-05-29 14:48:27 +00:00
{
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
}
2020-06-08 11:57:45 +00:00
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<BackupWorkerScope>();
var (tenantManager, backupStorageFactory, notifyHelper, backupRepository, backupWorker, backupPortalTask, _, _, coreBaseSettings) = scopeClass;
2020-06-16 11:34:44 +00:00
2020-08-31 08:18:07 +00:00
var tenant = tenantManager.GetTenant(TenantId);
var dateTime = coreBaseSettings.Standalone ? DateTime.Now : DateTime.UtcNow;
var backupName = string.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.{2}", tenantManager.GetTenant(TenantId).TenantAlias, dateTime, ArchiveFormat);
var tempFile = Path.Combine(TempFolder, backupName);
var storagePath = tempFile;
try
{
2020-08-31 08:18:07 +00:00
var backupTask = backupPortalTask;
2020-06-16 11:34:44 +00:00
2020-06-08 10:40:26 +00:00
backupTask.Init(TenantId, ConfigPaths[CurrentRegion], tempFile, Limit);
if (!BackupMail)
{
backupTask.IgnoreModule(ModuleName.Mail);
}
backupTask.IgnoreTable("tenants_tariff");
2020-06-16 11:34:44 +00:00
backupTask.ProgressChanged += (sender, args) =>
{
Percentage = 0.9 * args.Progress;
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-16 11:34:44 +00:00
};
backupTask.RunJob();
2020-08-31 08:18:07 +00:00
var backupStorage = backupStorageFactory.GetBackupStorage(StorageType, TenantId, StorageParams);
if (backupStorage != null)
{
storagePath = backupStorage.Upload(StorageBasePath, tempFile, UserId);
Link = backupStorage.GetPublicLink(storagePath);
}
2020-08-31 08:18:07 +00:00
var repo = backupRepository;
repo.SaveBackupRecord(
new BackupRecord
{
Id = (Guid)Id,
TenantId = TenantId,
IsScheduled = IsScheduled,
Name = Path.GetFileName(tempFile),
StorageType = StorageType,
StorageBasePath = StorageBasePath,
StoragePath = storagePath,
CreatedOn = DateTime.UtcNow,
ExpiresOn = StorageType == BackupStorageType.DataStore ? DateTime.UtcNow.AddDays(1) : DateTime.MinValue,
StorageParams = JsonConvert.SerializeObject(StorageParams)
});
Percentage = 100;
if (UserId != Guid.Empty && !IsScheduled)
{
2020-08-31 08:18:07 +00:00
notifyHelper.SendAboutBackupCompleted(UserId);
}
IsCompleted = true;
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
}
catch (Exception error)
{
2020-06-08 08:17:54 +00:00
Log.ErrorFormat("RunJob - Params: {0}, Error = {1}", new { Id, Tenant = TenantId, File = tempFile, BasePath = StorageBasePath, }, error);
Error = error;
IsCompleted = true;
}
finally
{
2020-06-29 12:45:38 +00:00
try
{
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-29 12:45:38 +00:00
}
catch (Exception error)
{
Log.Error("publish", error);
}
try
{
if (!(storagePath == tempFile && StorageType == BackupStorageType.Local))
{
File.Delete(tempFile);
}
}
catch (Exception error)
{
Log.Error("can't delete file: {0}", error);
}
}
}
2020-06-08 08:17:54 +00:00
public override object Clone()
{
return MemberwiseClone();
}
}
2020-06-08 08:17:54 +00:00
public class RestoreProgressItem : BaseBackupProgressItem
{
2020-06-16 11:34:44 +00:00
public override BackupProgressItemEnum BackupProgressItemEnum { get => BackupProgressItemEnum.Restore; }
public BackupStorageType StorageType { get; set; }
public string StoragePath { get; set; }
public bool Notify { get; set; }
public Dictionary<string, string> StorageParams { get; set; }
public string TempFolder { get; set; }
private string CurrentRegion { get; set; }
private string UpgradesPath { get; set; }
private Dictionary<string, string> ConfigPaths { get; set; }
private ILog Log { get; set; }
2020-06-08 10:40:26 +00:00
private IServiceProvider ServiceProvider { get; set; }
2020-06-03 09:15:05 +00:00
public RestoreProgressItem(
2020-06-08 08:17:54 +00:00
IOptionsMonitor<ILog> options,
2020-06-08 10:40:26 +00:00
IServiceProvider serviceProvider)
2020-06-08 08:17:54 +00:00
{
Log = options.CurrentValue;
2020-06-08 10:40:26 +00:00
ServiceProvider = serviceProvider;
2020-06-03 09:15:05 +00:00
}
2020-06-10 12:35:10 +00:00
public void Init(StartRestoreRequest request, string tempFolder, string upgradesPath, string currentRegion, Dictionary<string, string> configPaths)
2020-06-03 09:15:05 +00:00
{
Id = Guid.NewGuid();
TenantId = request.TenantId;
Notify = request.NotifyAfterCompletion;
StoragePath = request.FilePathOrId;
2020-06-08 10:40:26 +00:00
StorageType = (BackupStorageType)request.StorageType;
2020-06-03 09:15:05 +00:00
TempFolder = tempFolder;
UpgradesPath = upgradesPath;
CurrentRegion = currentRegion;
2020-06-10 12:35:10 +00:00
ConfigPaths = configPaths;
}
2020-06-16 11:34:44 +00:00
2020-06-08 08:17:54 +00:00
public override void RunJob()
{
2020-06-10 12:35:10 +00:00
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<BackupWorkerScope>();
var (tenantManager, backupStorageFactory, notifyHelper, _, backupWorker, _, restorePortalTask, _, _) = scopeClass;
Tenant tenant = null;
var tempFile = PathHelper.GetTempFileName(TempFolder);
try
{
2020-08-31 08:18:07 +00:00
tenant = tenantManager.GetTenant(TenantId);
notifyHelper.SendAboutRestoreStarted(tenant, Notify);
var storage = backupStorageFactory.GetBackupStorage(StorageType, TenantId, StorageParams);
storage.Download(StoragePath, tempFile);
Percentage = 10;
tenant.SetStatus(TenantStatus.Restoring);
2020-09-30 11:50:39 +00:00
_ = tenantManager.SaveTenant(tenant);
var columnMapper = new ColumnMapper();
columnMapper.SetMapping("tenants_tenants", "alias", tenant.TenantAlias, ((Guid)Id).ToString("N"));
columnMapper.Commit();
2020-06-15 15:33:53 +00:00
2020-08-31 08:18:07 +00:00
var restoreTask = restorePortalTask;
2020-06-08 10:40:26 +00:00
restoreTask.Init(ConfigPaths[CurrentRegion], tempFile, TenantId, columnMapper, UpgradesPath);
2020-06-16 11:34:44 +00:00
restoreTask.ProgressChanged += (sender, args) =>
{
Percentage = Percentage = (10d + 0.65 * args.Progress);
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-16 11:34:44 +00:00
};
restoreTask.RunJob();
Tenant restoredTenant = null;
if (restoreTask.Dump)
{
if (Notify)
{
AscCacheNotify.OnClearCache();
2020-08-31 08:18:07 +00:00
var tenants = tenantManager.GetTenants();
foreach (var t in tenants)
{
2020-08-31 08:18:07 +00:00
notifyHelper.SendAboutRestoreCompleted(t, Notify);
}
}
}
else
{
2020-08-31 08:18:07 +00:00
tenantManager.RemoveTenant(tenant.TenantId);
2020-08-31 08:18:07 +00:00
restoredTenant = tenantManager.GetTenant(columnMapper.GetTenantMapping());
restoredTenant.SetStatus(TenantStatus.Active);
restoredTenant.TenantAlias = tenant.TenantAlias;
restoredTenant.PaymentId = string.Empty;
if (string.IsNullOrEmpty(restoredTenant.MappedDomain) && !string.IsNullOrEmpty(tenant.MappedDomain))
{
restoredTenant.MappedDomain = tenant.MappedDomain;
}
2020-09-30 11:50:39 +00:00
_ = tenantManager.SaveTenant(restoredTenant);
// sleep until tenants cache expires
Thread.Sleep(TimeSpan.FromMinutes(2));
2020-08-31 08:18:07 +00:00
notifyHelper.SendAboutRestoreCompleted(restoredTenant, Notify);
}
Percentage = 75;
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-29 12:45:38 +00:00
File.Delete(tempFile);
Percentage = 100;
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
}
catch (Exception error)
{
Log.Error(error);
Error = error;
if (tenant != null)
{
tenant.SetStatus(TenantStatus.Active);
2020-09-30 11:50:39 +00:00
_ = tenantManager.SaveTenant(tenant);
}
}
finally
{
2020-06-29 12:45:38 +00:00
try
{
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-29 12:45:38 +00:00
}
catch (Exception error)
{
Log.Error("publish", error);
}
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
IsCompleted = true;
}
}
2020-06-08 08:17:54 +00:00
public override object Clone()
{
return MemberwiseClone();
}
2020-06-03 09:15:05 +00:00
}
2020-06-08 08:17:54 +00:00
public class TransferProgressItem : BaseBackupProgressItem
{
2020-06-16 11:34:44 +00:00
public override BackupProgressItemEnum BackupProgressItemEnum { get => BackupProgressItemEnum.Transfer; }
public string TargetRegion { get; set; }
public bool TransferMail { get; set; }
public bool Notify { get; set; }
public string Link { get; set; }
public string TempFolder { get; set; }
public Dictionary<string, string> ConfigPaths { get; set; }
public string CurrentRegion { get; set; }
public int Limit { get; set; }
public ILog Log { get; set; }
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; set; }
2020-06-08 08:17:54 +00:00
public TransferProgressItem(
IOptionsMonitor<ILog> options,
2020-06-08 10:40:26 +00:00
IServiceProvider serviceProvider
)
{
Log = options.CurrentValue;
2020-06-08 10:40:26 +00:00
ServiceProvider = serviceProvider;
}
2020-06-03 09:15:05 +00:00
public void Init(
string targetRegion,
bool transferMail,
int tenantId,
string tempFolder,
int limit,
bool notify,
string currentRegion,
2020-06-08 08:17:54 +00:00
Dictionary<string, string> configPaths)
2020-06-03 09:15:05 +00:00
{
Id = Guid.NewGuid();
TenantId = tenantId;
TargetRegion = targetRegion;
TransferMail = transferMail;
Notify = notify;
TempFolder = tempFolder;
ConfigPaths = configPaths;
CurrentRegion = currentRegion;
Limit = limit;
2020-06-03 09:15:05 +00:00
}
2020-06-08 08:17:54 +00:00
public override void RunJob()
{
2020-06-10 12:35:10 +00:00
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<BackupWorkerScope>();
var (tenantManager, _, notifyHelper, _, backupWorker, _, _, transferPortalTask, _) = scopeClass;
var tempFile = PathHelper.GetTempFileName(TempFolder);
2020-08-31 08:18:07 +00:00
var tenant = tenantManager.GetTenant(TenantId);
2020-06-29 09:32:43 +00:00
var alias = tenant.TenantAlias;
2020-06-16 11:34:44 +00:00
try
{
2020-08-31 08:18:07 +00:00
notifyHelper.SendAboutTransferStart(tenant, TargetRegion, Notify);
var transferProgressItem = transferPortalTask;
2020-06-08 10:40:26 +00:00
transferProgressItem.Init(TenantId, ConfigPaths[CurrentRegion], ConfigPaths[TargetRegion], Limit, TempFolder);
2020-06-16 11:34:44 +00:00
transferProgressItem.ProgressChanged += (sender, args) =>
{
Percentage = args.Progress;
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-16 11:34:44 +00:00
};
if (!TransferMail)
{
transferProgressItem.IgnoreModule(ModuleName.Mail);
}
transferProgressItem.RunJob();
Link = GetLink(alias, false);
2020-08-31 08:18:07 +00:00
notifyHelper.SendAboutTransferComplete(tenant, TargetRegion, Link, !Notify);
backupWorker.PublishProgress(this);
}
catch (Exception error)
{
Log.Error(error);
Error = error;
Link = GetLink(alias, true);
2020-08-31 08:18:07 +00:00
notifyHelper.SendAboutTransferError(tenant, TargetRegion, Link, !Notify);
}
finally
{
2020-06-29 12:45:38 +00:00
try
{
2020-08-31 08:18:07 +00:00
backupWorker.PublishProgress(this);
2020-06-29 12:45:38 +00:00
}
catch (Exception error)
{
Log.Error("publish", error);
}
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
IsCompleted = true;
}
}
private string GetLink(string alias, bool isErrorLink)
{
2020-06-16 16:04:40 +00:00
return "http://" + alias + "." + ConfigurationProvider.Open(ConfigPaths[isErrorLink ? CurrentRegion : TargetRegion]).AppSettings.Settings["core:base-domain"].Value;
}
2020-06-08 08:17:54 +00:00
public override object Clone()
{
return MemberwiseClone();
}
}
public class FactoryProgressItem
{
2020-09-04 10:10:19 +00:00
public IServiceProvider ServiceProvider { get; }
public FactoryProgressItem(IServiceProvider serviceProvider)
2020-06-03 09:15:05 +00:00
{
2020-09-04 10:10:19 +00:00
ServiceProvider = serviceProvider;
2020-06-03 09:15:05 +00:00
}
2020-06-03 09:15:05 +00:00
public BackupProgressItem CreateBackupProgressItem(
StartBackupRequest request,
bool isScheduled,
string tempFolder,
int limit,
string currentRegion,
2020-06-16 11:34:44 +00:00
Dictionary<string, string> configPaths)
{
2020-09-04 10:10:19 +00:00
var item = ServiceProvider.GetService<BackupProgressItem>();
item.Init(request, isScheduled, tempFolder, limit, currentRegion, configPaths);
return item;
}
public BackupProgressItem CreateBackupProgressItem(
2020-06-03 09:15:05 +00:00
BackupSchedule schedule,
bool isScheduled,
string tempFolder,
int limit,
string currentRegion,
2020-06-03 09:15:05 +00:00
Dictionary<string, string> configPaths
)
{
2020-09-04 10:10:19 +00:00
var item = ServiceProvider.GetService<BackupProgressItem>();
item.Init(schedule, isScheduled, tempFolder, limit, currentRegion, configPaths);
return item;
}
2020-09-04 10:10:19 +00:00
public RestoreProgressItem CreateRestoreProgressItem(
2020-06-03 09:15:05 +00:00
StartRestoreRequest request,
string tempFolder,
string upgradesPath,
2020-06-10 12:35:10 +00:00
string currentRegion,
Dictionary<string, string> configPaths
)
{
2020-09-04 10:10:19 +00:00
var item = ServiceProvider.GetService<RestoreProgressItem>();
item.Init(request, tempFolder, upgradesPath, currentRegion, configPaths);
return item;
}
2020-06-03 09:15:05 +00:00
public TransferProgressItem CreateTransferProgressItem(
string targetRegion,
bool transferMail,
int tenantId,
string tempFolder,
int limit,
2020-06-03 09:15:05 +00:00
bool notify,
string currentRegion,
Dictionary<string, string> configPaths
)
{
2020-09-04 10:10:19 +00:00
var item = ServiceProvider.GetService<TransferProgressItem>();
item.Init(targetRegion, transferMail, tenantId, tempFolder, limit, notify, currentRegion, configPaths);
return item;
2020-08-16 10:11:15 +00:00
}
}
2020-08-24 18:41:06 +00:00
internal class BackupWorkerScope
2020-08-16 10:11:15 +00:00
{
2020-08-31 08:18:07 +00:00
private TenantManager TenantManager { get; }
private BackupStorageFactory BackupStorageFactory { get; }
private NotifyHelper NotifyHelper { get; }
private BackupRepository BackupRepository { get; }
private BackupWorker BackupWorker { get; }
private BackupPortalTask BackupPortalTask { get; }
private RestorePortalTask RestorePortalTask { get; }
private TransferPortalTask TransferPortalTask { get; }
public CoreBaseSettings CoreBaseSettings { get; }
2020-08-16 10:11:15 +00:00
2020-08-24 18:41:06 +00:00
public BackupWorkerScope(TenantManager tenantManager,
2020-08-16 10:11:15 +00:00
BackupStorageFactory backupStorageFactory,
NotifyHelper notifyHelper,
BackupRepository backupRepository,
BackupWorker backupWorker,
BackupPortalTask backupPortalTask,
RestorePortalTask restorePortalTask,
TransferPortalTask transferPortalTask,
CoreBaseSettings coreBaseSettings)
2020-08-16 10:11:15 +00:00
{
TenantManager = tenantManager;
BackupStorageFactory = backupStorageFactory;
NotifyHelper = notifyHelper;
BackupRepository = backupRepository;
BackupWorker = backupWorker;
BackupPortalTask = backupPortalTask;
RestorePortalTask = restorePortalTask;
TransferPortalTask = transferPortalTask;
CoreBaseSettings = coreBaseSettings;
}
2020-08-31 08:18:07 +00:00
2020-09-04 10:10:19 +00:00
public void Deconstruct(out TenantManager tenantManager,
2020-08-31 08:18:07 +00:00
out BackupStorageFactory backupStorageFactory,
2020-09-04 10:10:19 +00:00
out NotifyHelper notifyHelper,
out BackupRepository backupRepository,
2020-08-31 08:18:07 +00:00
out BackupWorker backupWorker,
out BackupPortalTask backupPortalTask,
2020-09-04 10:10:19 +00:00
out RestorePortalTask restorePortalTask,
out TransferPortalTask transferPortalTask,
out CoreBaseSettings coreBaseSettings)
2020-08-31 08:18:07 +00:00
{
tenantManager = TenantManager;
backupStorageFactory = BackupStorageFactory;
notifyHelper = NotifyHelper;
backupRepository = BackupRepository;
backupWorker = BackupWorker;
backupPortalTask = BackupPortalTask;
restorePortalTask = RestorePortalTask;
transferPortalTask = TransferPortalTask;
coreBaseSettings = CoreBaseSettings;
2020-08-31 08:18:07 +00:00
}
2020-06-08 10:40:26 +00:00
}
2020-08-16 10:11:15 +00:00
2020-06-08 10:40:26 +00:00
public static class BackupWorkerExtension
{
public static DIHelper AddBackupWorkerService(this DIHelper services)
{
2020-09-30 11:50:39 +00:00
_ = services.TryAddSingleton<BackupWorker>();
_ = services.TryAddSingleton<FactoryProgressItem>();
_ = services.TryAddTransient<BackupProgressItem>();
_ = services.TryAddTransient<TransferProgressItem>();
_ = services.TryAddTransient<RestoreProgressItem>();
_ = services.TryAddScoped<BackupWorkerScope>();
_ = services.TryAddSingleton<ProgressQueueOptionsManager<BaseBackupProgressItem>>();
_ = services.TryAddSingleton<ProgressQueue<BaseBackupProgressItem>>();
_ = services.AddSingleton<IPostConfigureOptions<ProgressQueue<BaseBackupProgressItem>>, ConfigureProgressQueue<BaseBackupProgressItem>>();
2020-06-08 10:40:26 +00:00
return services
.AddTenantManagerService()
.AddCoreBaseSettingsService()
.AddStorageFactoryService()
.AddStorageFactoryConfigService()
.AddLicenseReaderService()
.AddNotifyHelperService()
2020-06-10 12:35:10 +00:00
.AddBackupPortalTaskService()
.AddDbFactoryService()
2020-08-24 18:41:06 +00:00
.AddRestorePortalTaskService()
.AddTransferPortalTaskService();
2020-06-08 10:40:26 +00:00
}
}
}