DocSpace-buildtools/common/ASC.Data.Backup.Core/Service/BackupWorker.cs

878 lines
29 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2021-08-31 09:40:28 +00:00
using ConfigurationProvider = ASC.Data.Backup.Utils.ConfigurationProvider;
2021-08-31 09:40:28 +00:00
namespace ASC.Data.Backup.Services;
public enum BackupProgressItemEnum
2021-08-31 09:40:28 +00:00
{
Backup,
Restore,
Transfer
}
[Singletone(Additional = typeof(BackupWorkerExtension))]
public class BackupWorker
{
internal string TempFolder { get; set; }
private DistributedTaskQueue _progressQueue;
private string _currentRegion;
private Dictionary<string, string> _configPaths;
private int _limit;
private string _upgradesPath;
private readonly ILog _logger;
private readonly FactoryProgressItem _factoryProgressItem;
private readonly TempPath _tempPath;
private readonly object _synchRoot = new object();
public BackupWorker(
2022-03-17 10:13:52 +00:00
ILog logger,
DistributedTaskQueueOptionsManager progressQueue,
FactoryProgressItem factoryProgressItem,
TempPath tempPath)
{
2022-03-17 10:13:52 +00:00
_logger = logger;
_progressQueue = progressQueue.Get<BaseBackupProgressItem>();
_factoryProgressItem = factoryProgressItem;
_tempPath = tempPath;
}
public void Start(BackupSettings settings)
{
TempFolder = _tempPath.GetTempPath();
if (!Directory.Exists(TempFolder))
2021-08-31 09:40:28 +00:00
{
Directory.CreateDirectory(TempFolder);
2021-08-31 09:40:28 +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);
var invalidConfigPath = _configPaths.Values.FirstOrDefault(path => !File.Exists(path));
if (invalidConfigPath != null)
2021-08-31 09:40:28 +00:00
{
_logger.WarnFormat("Configuration file {0} not found", invalidConfigPath);
}
}
2021-08-31 09:40:28 +00:00
public void Stop()
{
if (_progressQueue != null)
{
var tasks = _progressQueue.GetTasks();
2021-08-31 09:40:28 +00:00
foreach (var t in tasks)
2021-08-31 09:40:28 +00:00
{
_progressQueue.CancelTask(t.Id);
2021-08-31 09:40:28 +00:00
}
_progressQueue = null;
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public BackupProgress StartBackup(StartBackupRequest request)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
var item = _progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == request.TenantId);
if (item != null && item.IsCompleted)
2021-08-31 09:40:28 +00:00
{
_progressQueue.RemoveTask(item.Id);
item = null;
2021-08-31 09:40:28 +00:00
}
if (item == null)
2021-08-31 09:40:28 +00:00
{
item = _factoryProgressItem.CreateBackupProgressItem(request, false, TempFolder, _limit, _currentRegion, _configPaths);
_progressQueue.QueueTask(item);
}
2021-08-31 09:40:28 +00:00
item.PublishChanges();
2021-08-31 09:40:28 +00:00
return ToBackupProgress(item);
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public void StartScheduledBackup(BackupSchedule schedule)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
var item = _progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == schedule.TenantId);
if (item != null && item.IsCompleted)
2021-08-31 09:40:28 +00:00
{
_progressQueue.RemoveTask(item.Id);
item = null;
2021-08-31 09:40:28 +00:00
}
if (item == null)
2021-08-31 09:40:28 +00:00
{
item = _factoryProgressItem.CreateBackupProgressItem(schedule, false, TempFolder, _limit, _currentRegion, _configPaths);
_progressQueue.QueueTask(item);
2021-08-31 09:40:28 +00:00
}
}
}
2021-08-31 09:40:28 +00:00
public BackupProgress GetBackupProgress(int tenantId)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
return ToBackupProgress(_progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public BackupProgress GetTransferProgress(int tenantId)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
return ToBackupProgress(_progressQueue.GetTasks<TransferProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public BackupProgress GetRestoreProgress(int tenantId)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
return ToBackupProgress(_progressQueue.GetTasks<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public void ResetBackupError(int tenantId)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
var progress = _progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
if (progress != null)
2021-08-31 09:40:28 +00:00
{
progress.Exception = null;
2021-08-31 09:40:28 +00:00
}
}
}
2021-08-31 09:40:28 +00:00
public void ResetRestoreError(int tenantId)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
var progress = _progressQueue.GetTasks<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
if (progress != null)
2021-08-31 09:40:28 +00:00
{
progress.Exception = null;
2021-08-31 09:40:28 +00:00
}
}
}
2021-08-31 09:40:28 +00:00
public BackupProgress StartRestore(StartRestoreRequest request)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
var item = _progressQueue.GetTasks<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == request.TenantId);
if (item != null && item.IsCompleted)
2021-08-31 09:40:28 +00:00
{
_progressQueue.RemoveTask(item.Id);
item = null;
2021-08-31 09:40:28 +00:00
}
if (item == null)
2022-02-09 18:33:50 +00:00
{
item = _factoryProgressItem.CreateRestoreProgressItem(request, TempFolder, _upgradesPath, _currentRegion, _configPaths);
_progressQueue.QueueTask(item);
2021-08-31 09:40:28 +00:00
}
return ToBackupProgress(item);
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public BackupProgress StartTransfer(int tenantId, string targetRegion, bool transferMail, bool notify)
{
lock (_synchRoot)
2021-08-31 09:40:28 +00:00
{
var item = _progressQueue.GetTasks<TransferProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
if (item != null && item.IsCompleted)
2021-08-31 09:40:28 +00:00
{
_progressQueue.RemoveTask(item.Id);
item = null;
2021-08-31 09:40:28 +00:00
}
if (item == null)
2021-08-31 09:40:28 +00:00
{
item = _factoryProgressItem.CreateTransferProgressItem(targetRegion, transferMail, tenantId, TempFolder, _limit, notify, _currentRegion, _configPaths);
_progressQueue.QueueTask(item);
2021-08-31 09:40:28 +00:00
}
return ToBackupProgress(item);
2021-08-31 09:40:28 +00:00
}
}
internal static string GetBackupHash(string path)
2021-08-31 09:40:28 +00:00
{
using (var sha256 = SHA256.Create())
using (var fileStream = File.OpenRead(path))
2021-08-31 09:40:28 +00:00
{
fileStream.Position = 0;
var hash = sha256.ComputeHash(fileStream);
return BitConverter.ToString(hash).Replace("-", string.Empty);
2021-08-31 09:40:28 +00:00
}
}
private BackupProgress ToBackupProgress(BaseBackupProgressItem progressItem)
2021-08-31 09:40:28 +00:00
{
if (progressItem == null)
{
return null;
}
var progress = new BackupProgress
{
IsCompleted = progressItem.IsCompleted,
Progress = (int)progressItem.Percentage,
Error = progressItem.Exception != null ? progressItem.Exception.Message : "",
TenantId = progressItem.TenantId,
BackupProgressEnum = progressItem.BackupProgressItemEnum.Convert()
};
2021-08-31 09:40:28 +00:00
if (progressItem is BackupProgressItem backupProgressItem && backupProgressItem.Link != null)
{
progress.Link = backupProgressItem.Link;
}
else
2021-08-31 09:40:28 +00:00
{
if (progressItem is TransferProgressItem transferProgressItem && transferProgressItem.Link != null)
2021-08-31 09:40:28 +00:00
{
progress.Link = transferProgressItem.Link;
2021-08-31 09:40:28 +00:00
}
}
return progress;
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
public static class BackupProgressItemEnumConverter
{
public static BackupProgressEnum Convert(this BackupProgressItemEnum backupProgressItemEnum)
2021-08-31 09:40:28 +00:00
{
return backupProgressItemEnum switch
2021-08-31 09:40:28 +00:00
{
BackupProgressItemEnum.Backup => BackupProgressEnum.Backup,
BackupProgressItemEnum.Restore => BackupProgressEnum.Restore,
BackupProgressItemEnum.Transfer => BackupProgressEnum.Transfer,
_ => BackupProgressEnum.Backup
};
}
}
2021-08-31 09:40:28 +00:00
public abstract class BaseBackupProgressItem : DistributedTaskProgress
{
public int TenantId
{
get => _tenantId ?? GetProperty<int>(nameof(_tenantId));
set
{
_tenantId = value;
SetProperty(nameof(_tenantId), value);
2022-02-09 18:33:50 +00:00
}
}
public abstract BackupProgressItemEnum BackupProgressItemEnum { get; }
2022-02-11 09:59:06 +00:00
protected ILog Logger { get; set; }
protected IServiceScopeFactory ServiceScopeFactory { get; set; }
2021-08-31 09:40:28 +00:00
private int? _tenantId;
2021-08-31 09:40:28 +00:00
2022-03-18 08:04:29 +00:00
protected BaseBackupProgressItem(ILog logger, IServiceScopeFactory serviceScopeFactory)
{
2022-03-18 08:04:29 +00:00
Logger = logger;
ServiceScopeFactory = serviceScopeFactory;
2021-08-31 09:40:28 +00:00
}
public abstract object Clone();
}
2021-08-31 09:40:28 +00:00
[Transient]
public class BackupProgressItem : BaseBackupProgressItem
{
public bool BackupMail { get; set; }
public Dictionary<string, string> StorageParams { get; set; }
public string Link { get; private set; }
public string TempFolder { get; set; }
public override BackupProgressItemEnum BackupProgressItemEnum => BackupProgressItemEnum.Backup;
private const string ArchiveFormat = "tar.gz";
private bool _isScheduled;
private Guid _userId;
private BackupStorageType _storageType;
private string _storageBasePath;
private string _currentRegion;
private Dictionary<string, string> _configPaths;
private int _limit;
public BackupProgressItem(
2022-03-18 08:04:29 +00:00
ILog logger,
IServiceScopeFactory serviceScopeFactory)
2022-03-18 08:04:29 +00:00
: base(logger, serviceScopeFactory)
2021-08-31 09:40:28 +00:00
{
}
2022-02-09 18:33:50 +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;
2021-08-31 09:40:28 +00:00
}
public void Init(StartBackupRequest request, bool isScheduled, string tempFolder, int limit, string currentRegion, Dictionary<string, string> configPaths)
2021-08-31 09:40:28 +00:00
{
_userId = request.UserId;
TenantId = request.TenantId;
_storageType = request.StorageType;
_storageBasePath = request.StorageBasePath;
BackupMail = request.BackupMail;
StorageParams = request.StorageParams.ToDictionary(r => r.Key, r => r.Value);
_isScheduled = isScheduled;
TempFolder = tempFolder;
_limit = limit;
_currentRegion = currentRegion;
_configPaths = configPaths;
}
2021-08-31 09:40:28 +00:00
protected override void DoJob()
{
if (ThreadPriority.BelowNormal < Thread.CurrentThread.Priority)
2021-08-31 09:40:28 +00:00
{
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
2021-08-31 09:40:28 +00:00
}
using var scope = ServiceScopeFactory.CreateScope();
var scopeClass = scope.ServiceProvider.GetService<BackupWorkerScope>();
var (tenantManager, backupStorageFactory, notifyHelper, backupRepository, backupWorker, backupPortalTask, _, _, coreBaseSettings) = scopeClass;
2021-08-31 09:40:28 +00:00
var dateTime = coreBaseSettings.Standalone ? DateTime.Now : DateTime.UtcNow;
var backupName = string.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.{2}", tenantManager.GetTenant(TenantId).Alias, dateTime, ArchiveFormat);
2021-08-31 09:40:28 +00:00
var tempFile = CrossPlatform.PathCombine(TempFolder, backupName);
var storagePath = tempFile;
try
2021-08-31 09:40:28 +00:00
{
var backupTask = backupPortalTask;
backupTask.Init(TenantId, _configPaths[_currentRegion], tempFile, _limit);
if (!BackupMail)
2021-08-31 09:40:28 +00:00
{
backupTask.IgnoreModule(ModuleName.Mail);
2021-08-31 09:40:28 +00:00
}
backupTask.ProgressChanged += (sender, args) =>
2021-08-31 09:40:28 +00:00
{
Percentage = 0.9 * args.Progress;
PublishChanges();
};
2021-08-31 09:40:28 +00:00
backupTask.RunJob();
2021-08-31 09:40:28 +00:00
var backupStorage = backupStorageFactory.GetBackupStorage(_storageType, TenantId, StorageParams);
if (backupStorage != null)
{
storagePath = backupStorage.Upload(_storageBasePath, tempFile, _userId);
Link = backupStorage.GetPublicLink(storagePath);
}
2021-08-31 09:40:28 +00:00
var repo = backupRepository;
repo.SaveBackupRecord(
new BackupRecord
{
Id = Guid.Parse(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),
Hash = BackupWorker.GetBackupHash(tempFile)
});
Percentage = 100;
if (_userId != Guid.Empty && !_isScheduled)
{
notifyHelper.SendAboutBackupCompleted(_userId);
}
2021-08-31 09:40:28 +00:00
IsCompleted = true;
PublishChanges();
}
catch (Exception error)
{
2022-02-11 09:59:06 +00:00
Logger.ErrorFormat("RunJob - Params: {0}, Error = {1}", new { Id, Tenant = TenantId, File = tempFile, BasePath = _storageBasePath, }, error);
Exception = error;
IsCompleted = true;
}
finally
{
try
{
2021-08-31 09:40:28 +00:00
PublishChanges();
}
catch (Exception error)
{
2022-02-11 09:59:06 +00:00
Logger.Error("publish", error);
2021-08-31 09:40:28 +00:00
}
try
{
if (!(storagePath == tempFile && _storageType == BackupStorageType.Local))
2021-08-31 09:40:28 +00:00
{
File.Delete(tempFile);
2021-08-31 09:40:28 +00:00
}
}
catch (Exception error)
{
2022-02-11 09:59:06 +00:00
Logger.Error("can't delete file: {0}", error);
}
2021-08-31 09:40:28 +00:00
}
}
public override object Clone()
2021-08-31 09:40:28 +00:00
{
return MemberwiseClone();
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
[Transient]
public class RestoreProgressItem : BaseBackupProgressItem
{
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;
private string _upgradesPath;
private Dictionary<string, string> _configPaths;
public RestoreProgressItem(
2022-03-18 08:04:29 +00:00
ILog logger,
IServiceScopeFactory serviceScopeFactory)
2022-03-18 08:04:29 +00:00
: base(logger, serviceScopeFactory)
2021-08-31 09:40:28 +00:00
{
}
2021-08-31 09:40:28 +00:00
public void Init(StartRestoreRequest request, string tempFolder, string upgradesPath, string currentRegion, Dictionary<string, string> configPaths)
{
TenantId = request.TenantId;
Notify = request.NotifyAfterCompletion;
StoragePath = request.FilePathOrId;
StorageType = request.StorageType;
TempFolder = tempFolder;
_upgradesPath = upgradesPath;
_currentRegion = currentRegion;
_configPaths = configPaths;
}
2021-08-31 09:40:28 +00:00
protected override void DoJob()
{
using var scope = ServiceScopeFactory.CreateScope();
var scopeClass = scope.ServiceProvider.GetService<BackupWorkerScope>();
var (tenantManager, backupStorageFactory, notifyHelper, backupRepository, backupWorker, _, restorePortalTask, _, coreBaseSettings) = scopeClass;
Tenant tenant = null;
var tempFile = PathHelper.GetTempFileName(TempFolder);
try
{
tenant = tenantManager.GetTenant(TenantId);
tenantManager.SetCurrentTenant(tenant);
notifyHelper.SendAboutRestoreStarted(tenant, Notify);
var storage = backupStorageFactory.GetBackupStorage(StorageType, TenantId, StorageParams);
storage.Download(StoragePath, tempFile);
if (!coreBaseSettings.Standalone)
2021-08-31 09:40:28 +00:00
{
var backupHash = BackupWorker.GetBackupHash(tempFile);
var record = backupRepository.GetBackupRecord(backupHash, TenantId);
if (record == null)
2021-08-31 09:40:28 +00:00
{
throw new Exception(BackupResource.BackupNotFound);
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
Percentage = 10;
2021-08-31 09:40:28 +00:00
tenant.SetStatus(TenantStatus.Restoring);
tenantManager.SaveTenant(tenant);
2021-08-31 09:40:28 +00:00
var columnMapper = new ColumnMapper();
columnMapper.SetMapping("tenants_tenants", "alias", tenant.Alias, Guid.Parse(Id).ToString("N"));
2021-08-31 09:40:28 +00:00
columnMapper.Commit();
var restoreTask = restorePortalTask;
restoreTask.Init(_configPaths[_currentRegion], tempFile, TenantId, columnMapper, _upgradesPath);
restoreTask.ProgressChanged += (sender, args) =>
{
2022-01-21 13:46:30 +00:00
Percentage = Percentage = 10d + 0.65 * args.Progress;
PublishChanges();
};
restoreTask.RunJob();
2021-08-31 09:40:28 +00:00
Tenant restoredTenant = null;
2021-08-31 09:40:28 +00:00
if (restoreTask.Dump)
{
AscCacheNotify.OnClearCache();
2021-08-31 09:40:28 +00:00
if (Notify)
{
var tenants = tenantManager.GetTenants();
foreach (var t in tenants)
{
notifyHelper.SendAboutRestoreCompleted(t, Notify);
}
}
}
else
{
tenantManager.RemoveTenant(tenant.Id);
2021-08-31 09:40:28 +00:00
restoredTenant = tenantManager.GetTenant(columnMapper.GetTenantMapping());
restoredTenant.SetStatus(TenantStatus.Active);
restoredTenant.Alias = tenant.Alias;
2021-08-31 09:40:28 +00:00
restoredTenant.PaymentId = string.Empty;
if (string.IsNullOrEmpty(restoredTenant.MappedDomain) && !string.IsNullOrEmpty(tenant.MappedDomain))
{
restoredTenant.MappedDomain = tenant.MappedDomain;
}
tenantManager.SaveTenant(restoredTenant);
tenantManager.SetCurrentTenant(restoredTenant);
// sleep until tenants cache expires
Thread.Sleep(TimeSpan.FromMinutes(2));
notifyHelper.SendAboutRestoreCompleted(restoredTenant, Notify);
}
2021-08-31 09:40:28 +00:00
Percentage = 75;
2021-08-31 09:40:28 +00:00
PublishChanges();
2021-08-31 09:40:28 +00:00
File.Delete(tempFile);
2021-08-31 09:40:28 +00:00
Percentage = 100;
PublishChanges();
}
catch (Exception error)
{
2022-02-11 09:59:06 +00:00
Logger.Error(error);
Exception = error;
if (tenant != null)
{
tenant.SetStatus(TenantStatus.Active);
tenantManager.SaveTenant(tenant);
}
}
finally
{
try
{
2021-08-31 09:40:28 +00:00
PublishChanges();
}
catch (Exception error)
{
2022-02-11 09:59:06 +00:00
Logger.Error("publish", error);
2021-08-31 09:40:28 +00:00
}
if (File.Exists(tempFile))
{
File.Delete(tempFile);
2021-08-31 09:40:28 +00:00
}
}
}
2021-08-31 09:40:28 +00:00
public override object Clone()
{
return MemberwiseClone();
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
[Transient]
public class TransferProgressItem : BaseBackupProgressItem
{
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 TransferProgressItem(
2022-03-18 08:04:29 +00:00
ILog logger,
IServiceScopeFactory serviceScopeFactory) : base(logger, serviceScopeFactory)
2021-08-31 09:40:28 +00:00
{
}
public void Init(
string targetRegion,
bool transferMail,
int tenantId,
string tempFolder,
int limit,
bool notify,
string currentRegion,
Dictionary<string, string> configPaths)
2021-08-31 09:40:28 +00:00
{
TenantId = tenantId;
TargetRegion = targetRegion;
TransferMail = transferMail;
Notify = notify;
TempFolder = tempFolder;
ConfigPaths = configPaths;
CurrentRegion = currentRegion;
Limit = limit;
2021-08-31 09:40:28 +00:00
}
2021-08-31 09:40:28 +00:00
protected override void DoJob()
{
using var scope = ServiceScopeFactory.CreateScope();
2021-08-31 09:40:28 +00:00
var scopeClass = scope.ServiceProvider.GetService<BackupWorkerScope>();
var (tenantManager, _, notifyHelper, _, backupWorker, _, _, transferPortalTask, _) = scopeClass;
var tempFile = PathHelper.GetTempFileName(TempFolder);
var tenant = tenantManager.GetTenant(TenantId);
var alias = tenant.Alias;
2021-08-31 09:40:28 +00:00
try
{
notifyHelper.SendAboutTransferStart(tenant, TargetRegion, Notify);
var transferProgressItem = transferPortalTask;
transferProgressItem.Init(TenantId, ConfigPaths[CurrentRegion], ConfigPaths[TargetRegion], Limit, TempFolder);
transferProgressItem.ProgressChanged += (sender, args) =>
2021-08-31 09:40:28 +00:00
{
Percentage = args.Progress;
PublishChanges();
};
if (!TransferMail)
{
transferProgressItem.IgnoreModule(ModuleName.Mail);
}
transferProgressItem.RunJob();
2021-08-31 09:40:28 +00:00
Link = GetLink(alias, false);
notifyHelper.SendAboutTransferComplete(tenant, TargetRegion, Link, !Notify, transferProgressItem.ToTenantId);
PublishChanges();
2021-08-31 09:40:28 +00:00
}
catch (Exception error)
2021-08-31 09:40:28 +00:00
{
2022-02-11 09:59:06 +00:00
Logger.Error(error);
Exception = error;
2021-08-31 09:40:28 +00:00
Link = GetLink(alias, true);
notifyHelper.SendAboutTransferError(tenant, TargetRegion, Link, !Notify);
}
finally
{
2021-08-31 09:40:28 +00:00
try
{
PublishChanges();
}
catch (Exception error)
{
2022-02-11 09:59:06 +00:00
Logger.Error("publish", error);
2021-08-31 09:40:28 +00:00
}
if (File.Exists(tempFile))
{
File.Delete(tempFile);
2021-08-31 09:40:28 +00:00
}
}
}
2021-08-31 09:40:28 +00:00
private string GetLink(string alias, bool isErrorLink)
{
return "https://" + alias + "." + ConfigurationProvider.Open(ConfigPaths[isErrorLink ? CurrentRegion : TargetRegion]).AppSettings.Settings["core:base-domain"].Value;
2021-08-31 09:40:28 +00:00
}
public override object Clone()
2021-08-31 09:40:28 +00:00
{
return MemberwiseClone();
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
[Singletone(Additional = typeof(FactoryProgressItemExtension))]
public class FactoryProgressItem
{
public IServiceProvider ServiceProvider { get; }
2021-08-31 09:40:28 +00:00
public FactoryProgressItem(IServiceProvider serviceProvider)
2021-08-31 09:40:28 +00:00
{
ServiceProvider = serviceProvider;
}
2021-08-31 09:40:28 +00:00
public BackupProgressItem CreateBackupProgressItem(
StartBackupRequest request,
bool isScheduled,
string tempFolder,
int limit,
string currentRegion,
Dictionary<string, string> configPaths)
{
var item = ServiceProvider.GetService<BackupProgressItem>();
item.Init(request, isScheduled, tempFolder, limit, currentRegion, configPaths);
2021-08-31 09:40:28 +00:00
return item;
}
2021-08-31 09:40:28 +00:00
public BackupProgressItem CreateBackupProgressItem(
BackupSchedule schedule,
bool isScheduled,
string tempFolder,
int limit,
string currentRegion,
Dictionary<string, string> configPaths
)
{
var item = ServiceProvider.GetService<BackupProgressItem>();
item.Init(schedule, isScheduled, tempFolder, limit, currentRegion, configPaths);
2021-08-31 09:40:28 +00:00
return item;
}
2021-08-31 09:40:28 +00:00
public RestoreProgressItem CreateRestoreProgressItem(
StartRestoreRequest request,
string tempFolder,
string upgradesPath,
string currentRegion,
Dictionary<string, string> configPaths
)
{
var item = ServiceProvider.GetService<RestoreProgressItem>();
item.Init(request, tempFolder, upgradesPath, currentRegion, configPaths);
2021-08-31 09:40:28 +00:00
return item;
2021-08-31 09:40:28 +00:00
}
public TransferProgressItem CreateTransferProgressItem(
string targetRegion,
bool transferMail,
int tenantId,
string tempFolder,
int limit,
bool notify,
string currentRegion,
Dictionary<string, string> configPaths
)
{
var item = ServiceProvider.GetService<TransferProgressItem>();
item.Init(targetRegion, transferMail, tenantId, tempFolder, limit, notify, currentRegion, configPaths);
2021-08-31 09:40:28 +00:00
return item;
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
[Scope]
internal class BackupWorkerScope
{
private readonly TenantManager _tenantManager;
private readonly BackupStorageFactory _backupStorageFactory;
private readonly NotifyHelper _notifyHelper;
private readonly BackupRepository _backupRepository;
private readonly BackupWorker _backupWorker;
private readonly BackupPortalTask _backupPortalTask;
private readonly RestorePortalTask _restorePortalTask;
private readonly TransferPortalTask _transferPortalTask;
private readonly CoreBaseSettings _coreBaseSettings;
public BackupWorkerScope(TenantManager tenantManager,
BackupStorageFactory backupStorageFactory,
NotifyHelper notifyHelper,
BackupRepository backupRepository,
BackupWorker backupWorker,
BackupPortalTask backupPortalTask,
RestorePortalTask restorePortalTask,
TransferPortalTask transferPortalTask,
CoreBaseSettings coreBaseSettings)
2021-08-31 09:40:28 +00:00
{
_tenantManager = tenantManager;
_backupStorageFactory = backupStorageFactory;
_notifyHelper = notifyHelper;
_backupRepository = backupRepository;
_backupWorker = backupWorker;
_backupPortalTask = backupPortalTask;
_restorePortalTask = restorePortalTask;
_transferPortalTask = transferPortalTask;
_coreBaseSettings = coreBaseSettings;
2021-08-31 09:40:28 +00:00
}
public void Deconstruct(out TenantManager tenantManager,
out BackupStorageFactory backupStorageFactory,
out NotifyHelper notifyHelper,
out BackupRepository backupRepository,
out BackupWorker backupWorker,
out BackupPortalTask backupPortalTask,
out RestorePortalTask restorePortalTask,
out TransferPortalTask transferPortalTask,
out CoreBaseSettings coreBaseSettings)
2021-08-31 09:40:28 +00:00
{
tenantManager = _tenantManager;
backupStorageFactory = _backupStorageFactory;
notifyHelper = _notifyHelper;
backupRepository = _backupRepository;
backupWorker = _backupWorker;
backupPortalTask = _backupPortalTask;
restorePortalTask = _restorePortalTask;
transferPortalTask = _transferPortalTask;
coreBaseSettings = _coreBaseSettings;
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
Merge branch 'feature/backend-refactor' into feature/asc-backup-core-refactor # Conflicts: # common/ASC.Data.Backup.Core/ActionInvoker.cs # common/ASC.Data.Backup.Core/BackupAjaxHandler.cs # common/ASC.Data.Backup.Core/Core/DbBackupProvider.cs # common/ASC.Data.Backup.Core/Core/DbHelper.cs # common/ASC.Data.Backup.Core/Core/FileBackupProvider.cs # common/ASC.Data.Backup.Core/Core/NotifyHelper.cs # common/ASC.Data.Backup.Core/Core/ZipOperator.cs # common/ASC.Data.Backup.Core/Exceptions/ThrowHelper.cs # common/ASC.Data.Backup.Core/Extensions/EnumerableExtensions.cs # common/ASC.Data.Backup.Core/Service/BackupWorker.cs # common/ASC.Data.Backup.Core/Storage/DocumentsBackupStorage.cs # common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs # common/ASC.Data.Backup.Core/Tasks/ColumnMapper.cs # common/ASC.Data.Backup.Core/Tasks/Data/DataRowInfo.cs # common/ASC.Data.Backup.Core/Tasks/Data/TableInfo.cs # common/ASC.Data.Backup.Core/Tasks/DeletePortalTask.cs # common/ASC.Data.Backup.Core/Tasks/KeyHelper.cs # common/ASC.Data.Backup.Core/Tasks/Modules/MailModuleSpecifics.cs # common/ASC.Data.Backup.Core/Tasks/Modules/ModuleProvider.cs # common/ASC.Data.Backup.Core/Tasks/Modules/ModuleSpecificsBase.cs # common/ASC.Data.Backup.Core/Tasks/Modules/ProjectsModuleSpecifics.cs # common/ASC.Data.Backup.Core/Tasks/PortalTaskBase.cs # common/ASC.Data.Backup.Core/Tasks/RestoreDbModuleTask.cs # common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs # common/ASC.Data.Backup.Core/Tasks/TransferPortalTask.cs
2022-02-15 16:56:43 +00:00
public static class BackupWorkerExtension
{
public static void Register(DIHelper services)
2021-08-31 09:40:28 +00:00
{
services.TryAdd<BackupWorkerScope>();
services.AddDistributedTaskQueueService<BackupProgressItem>(5);
2021-08-31 09:40:28 +00:00
}
}
2021-08-31 09:40:28 +00:00
Merge branch 'feature/backend-refactor' into feature/asc-backup-core-refactor # Conflicts: # common/ASC.Data.Backup.Core/ActionInvoker.cs # common/ASC.Data.Backup.Core/BackupAjaxHandler.cs # common/ASC.Data.Backup.Core/Core/DbBackupProvider.cs # common/ASC.Data.Backup.Core/Core/DbHelper.cs # common/ASC.Data.Backup.Core/Core/FileBackupProvider.cs # common/ASC.Data.Backup.Core/Core/NotifyHelper.cs # common/ASC.Data.Backup.Core/Core/ZipOperator.cs # common/ASC.Data.Backup.Core/Exceptions/ThrowHelper.cs # common/ASC.Data.Backup.Core/Extensions/EnumerableExtensions.cs # common/ASC.Data.Backup.Core/Service/BackupWorker.cs # common/ASC.Data.Backup.Core/Storage/DocumentsBackupStorage.cs # common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs # common/ASC.Data.Backup.Core/Tasks/ColumnMapper.cs # common/ASC.Data.Backup.Core/Tasks/Data/DataRowInfo.cs # common/ASC.Data.Backup.Core/Tasks/Data/TableInfo.cs # common/ASC.Data.Backup.Core/Tasks/DeletePortalTask.cs # common/ASC.Data.Backup.Core/Tasks/KeyHelper.cs # common/ASC.Data.Backup.Core/Tasks/Modules/MailModuleSpecifics.cs # common/ASC.Data.Backup.Core/Tasks/Modules/ModuleProvider.cs # common/ASC.Data.Backup.Core/Tasks/Modules/ModuleSpecificsBase.cs # common/ASC.Data.Backup.Core/Tasks/Modules/ProjectsModuleSpecifics.cs # common/ASC.Data.Backup.Core/Tasks/PortalTaskBase.cs # common/ASC.Data.Backup.Core/Tasks/RestoreDbModuleTask.cs # common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs # common/ASC.Data.Backup.Core/Tasks/TransferPortalTask.cs
2022-02-15 16:56:43 +00:00
public static class FactoryProgressItemExtension
{
public static void Register(DIHelper services)
2021-08-31 09:40:28 +00:00
{
services.TryAdd<BackupProgressItem>();
services.TryAdd<RestoreProgressItem>();
services.TryAdd<TransferProgressItem>();
2021-08-31 09:40:28 +00:00
}
}