Skip to content

Commit

Permalink
Merge pull request 'feature/backup-progress-in-socket' (#122) from fe…
Browse files Browse the repository at this point in the history
…ature/backup-progress-in-socket into develop

Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/DocSpace-server/pulls/122
Reviewed-by: pavelbannov <[email protected]>
  • Loading branch information
AlexeySafronov committed Jan 21, 2025
2 parents 542a7c7 + 0e4d086 commit 068bffd
Show file tree
Hide file tree
Showing 23 changed files with 274 additions and 58 deletions.
2 changes: 1 addition & 1 deletion common/ASC.Api.Core/Middleware/TenantStatusFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ASC.Api.Core.Middleware;
public class TenantStatusFilter(ILogger<TenantStatusFilter> logger, TenantManager tenantManager)
: IAsyncResourceFilter
{
private readonly string[] _passthroughtRequestEndings = ["preparation-portal", "getrestoreprogress", "settings", "settings.json", "colortheme", "colortheme.json", "logos", "logos.json", "build", "build.json", "@self", "@self.json"
private readonly string[] _passthroughtRequestEndings = ["preparation-portal", "portal", "getrestoreprogress", "settings", "settings.json", "colortheme", "colortheme.json", "logos", "logos.json", "build", "build.json", "@self", "@self.json"
]; //TODO add or update when "preparation-portal" will be done


Expand Down
16 changes: 12 additions & 4 deletions common/ASC.Data.Backup.Core/BackupAjaxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,14 @@ private async Task DemandPermissionsBackupAsync()

#region restore

public async Task StartRestoreAsync(string backupId, BackupStorageType storageType, Dictionary<string, string> storageParams, bool notify, string serverBaseUri)
public async Task<string> StartRestoreAsync(string backupId,
BackupStorageType storageType,
Dictionary<string, string> storageParams,
bool notify,
string serverBaseUri,
bool dump,
bool enqueueTask = true,
string taskId = null)
{
await DemandPermissionsRestoreAsync();
var tenantId = GetCurrentTenantIdAsync();
Expand All @@ -304,7 +311,8 @@ public async Task StartRestoreAsync(string backupId, BackupStorageType storageTy
TenantId = tenantId,
NotifyAfterCompletion = notify,
StorageParams = storageParams,
ServerBaseUri = serverBaseUri
ServerBaseUri = serverBaseUri,
Dump = dump
};

if (Guid.TryParse(backupId, out var guidBackupId))
Expand All @@ -316,15 +324,15 @@ public async Task StartRestoreAsync(string backupId, BackupStorageType storageTy
restoreRequest.StorageType = storageType;
restoreRequest.FilePathOrId = storageParams["filePath"];

if (restoreRequest.StorageType == BackupStorageType.Local)
if (restoreRequest.StorageType == BackupStorageType.Local && enqueueTask)
{
var path = await GetTmpFilePathAsync(tenantId);
path = File.Exists(path + ".tar.gz") ? path + ".tar.gz" : path + ".tar";
restoreRequest.FilePathOrId = path;
}
}

await backupService.StartRestoreAsync(restoreRequest);
return await backupService.StartRestoreAsync(restoreRequest, enqueueTask, taskId);
}

public async Task<BackupProgress> GetRestoreProgressAsync()
Expand Down
27 changes: 27 additions & 0 deletions common/ASC.Data.Backup.Core/BackupResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions common/ASC.Data.Backup.Core/BackupResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BackupException" xml:space="preserve">
<value>Something went wrong while creating the backup. Please try creating it again or contact support.</value>
</data>
<data name="BackupInvalid" xml:space="preserve">
<value>The backup file is invalid. </value>
</data>
<data name="BackupNotFound" xml:space="preserve">
<value>The backup file is invalid. Please use the file created in the same portal.</value>
</data>
Expand All @@ -126,4 +132,7 @@
<data name="LargeBackup" xml:space="preserve">
<value>Backup is larger than the total size of the portal</value>
</data>
<data name="RestoreException" xml:space="preserve">
<value>Something went wrong while restoring the backup. Please try restoring it again or contact support.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class StartRestoreRequest
public bool NotifyAfterCompletion { get; init; }
public Dictionary<string, string> StorageParams { get; set; }
public string ServerBaseUri { get; init; }
public bool Dump { get; init; }
}

public class CreateScheduleRequest : StartBackupRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public BackupRestoreRequestIntegrationEvent(BackupStorageType storageType,
Dictionary<string, string> storageParams,
bool notify,
string backupId,
bool dump,
string taskId,
string serverBaseUri = null
) : base(createBy, tenantId)
{
Expand All @@ -48,6 +50,8 @@ public BackupRestoreRequestIntegrationEvent(BackupStorageType storageType,
Notify = notify;
BackupId = backupId;
ServerBaseUri = serverBaseUri;
Dump = dump;
TaskId = taskId;
}

[ProtoMember(1)]
Expand All @@ -64,5 +68,11 @@ public BackupRestoreRequestIntegrationEvent(BackupStorageType storageType,

[ProtoMember(5)]
public string ServerBaseUri { get; set; }

[ProtoMember(6)]
public bool Dump { get; set; }

[ProtoMember(7)]
public string TaskId { get; set; }
}

7 changes: 4 additions & 3 deletions common/ASC.Data.Backup.Core/Service/BackupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public async Task StartTransferAsync(StartTransferRequest request)
}
}

public async Task StartRestoreAsync(StartRestoreRequest request)
public async Task<string> StartRestoreAsync(StartRestoreRequest request, bool enqueueTask = true, string taskId = null)
{
if (request.StorageType == BackupStorageType.Local && (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId)))
if (request.StorageType == BackupStorageType.Local && (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId)) && enqueueTask)
{
throw new FileNotFoundException();
}
Expand All @@ -140,11 +140,12 @@ public async Task StartRestoreAsync(StartRestoreRequest request)
request.StorageParams = JsonSerializer.Deserialize<Dictionary<string, string>>(backupRecord.StorageParams);
}

var progress = await backupWorker.StartRestoreAsync(request);
var progress = await backupWorker.StartRestoreAsync(request, enqueueTask, taskId);
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
return progress.TaskId;
}

public async Task<BackupProgress> GetBackupProgress(int tenantId)
Expand Down
38 changes: 19 additions & 19 deletions common/ASC.Data.Backup.Core/Service/BackupWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public async Task ResetRestoreErrorAsync(int tenantId)
}
}

public async Task<BackupProgress> StartRestoreAsync(StartRestoreRequest request)
public async Task<BackupProgress> StartRestoreAsync(StartRestoreRequest request, bool enqueueTask = true, string taskId = null)
{
await using (await distributedLockProvider.TryAcquireLockAsync(LockKey))
{
Expand All @@ -190,12 +190,27 @@ public async Task<BackupProgress> StartRestoreAsync(StartRestoreRequest request)
await _progressQueue.DequeueTask(item.Id);
item = null;
}
if (item == null)

if (item == null || (enqueueTask && item.Id == taskId && item.Status == DistributedTaskStatus.Created))
{

item = serviceProvider.GetService<RestoreProgressItem>();

item.Init(request, TempFolder, _upgradesPath);

await _progressQueue.EnqueueTask(item);
if (!string.IsNullOrEmpty(taskId))
{
item.Id = taskId;
}

if (enqueueTask)
{
await _progressQueue.EnqueueTask(item);
}
else
{
await _progressQueue.PublishTask(item);
}
}
return ToBackupProgress(item);
}
Expand Down Expand Up @@ -274,22 +289,7 @@ private BackupProgress ToBackupProgress(BaseBackupProgressItem progressItem)
{
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.BackupProgressItemType.Convert(),
TaskId = progressItem.Id
};

if (progressItem.BackupProgressItemType is BackupProgressItemType.Backup or BackupProgressItemType.Transfer && progressItem.Link != null)
{
progress.Link = progressItem.Link;
}

return progress;
return progressItem.ToBackupProgress();
}

public async Task<bool> IsInstanceTooBusy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class BackupProgressItem(ILogger<BackupProgressItem> logger,
public void Init(BackupSchedule schedule, bool isScheduled, string tempFolder, int limit)
{
Init();
BackupProgressItemType = BackupProgressItemType.Backup;
_userId = Guid.Empty;
TenantId = schedule.TenantId;
_storageType = schedule.StorageType;
Expand All @@ -63,6 +64,7 @@ public void Init(BackupSchedule schedule, bool isScheduled, string tempFolder, i
public void Init(StartBackupRequest request, bool isScheduled, string tempFolder, int limit)
{
Init();
BackupProgressItemType = BackupProgressItemType.Backup;
_userId = request.UserId;
TenantId = request.TenantId;
_storageType = request.StorageType;
Expand All @@ -84,6 +86,9 @@ protected override async Task DoJob()
var backupRepository = scope.ServiceProvider.GetService<BackupRepository>();
var backupPortalTask = scope.ServiceProvider.GetService<BackupPortalTask>();
var tempStream = scope.ServiceProvider.GetService<TempStream>();
var socketManager = scope.ServiceProvider.GetService<SocketManager>();
await tenantManager.SetCurrentTenantAsync(TenantId);
await socketManager.BackupProgressAsync(0);

var dateTime = coreBaseSettings.Standalone ? DateTime.Now : DateTime.UtcNow;
var tempFile = "";
Expand All @@ -107,6 +112,7 @@ protected override async Task DoJob()
backupPortalTask.ProgressChanged = async args =>
{
Percentage = 0.9 * args.Progress;
await socketManager.BackupProgressAsync((int)Percentage);
await PublishChanges();
};

Expand Down Expand Up @@ -165,6 +171,7 @@ await backupRepository.SaveBackupRecordAsync(
{
try
{
await socketManager.EndBackupAsync(ToBackupProgress());
await PublishChanges();
}
catch (Exception error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// 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

using ASC.Common.Threading.Progress;

namespace ASC.Data.Backup.Services;

public abstract class BaseBackupProgressItem(IServiceScopeFactory serviceScopeFactory) : DistributedTaskProgress
Expand Down Expand Up @@ -74,10 +76,9 @@ public BackupProgressItemType BackupProgressItemType
{
return _backupProgressItemEnum ?? (BackupProgressItemType)this[nameof(_backupProgressItemEnum)];
}
protected init
protected set
{
_backupProgressItemEnum = value;

this[nameof(_backupProgressItemEnum)] = (int)value;
}
}
Expand All @@ -87,7 +88,25 @@ protected void Init()
this[nameof(_tenantId)] = 0;
this[nameof(_newTenantId)] = 0;
this[nameof(_link)] = "";
this[nameof(_backupProgressItemEnum)] = 0;
}

public BackupProgress ToBackupProgress()
{
var progress = new BackupProgress
{
IsCompleted = IsCompleted,
Progress = (int)Percentage,
Error = Exception != null ? Exception.Message : "",
TenantId = TenantId,
BackupProgressEnum = BackupProgressItemType.Convert(),
TaskId = Id
};
if (BackupProgressItemType is BackupProgressItemType.Backup or BackupProgressItemType.Transfer && Link != null)
{
progress.Link = Link;
}

return progress;
}

public abstract object Clone();
Expand Down
Loading

0 comments on commit 068bffd

Please sign in to comment.