Merge pull request #646 from ONLYOFFICE/feature/logger-Data.Backup.BackgroundTasks

Feature/logger data.backup.background tasks
This commit is contained in:
Pavel Bannov 2022-05-19 13:41:57 +03:00 committed by GitHub
commit e602d183ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 176 additions and 27 deletions

View File

@ -30,16 +30,18 @@ global using ASC.Common;
global using ASC.Common.Caching;
global using ASC.Common.Utils;
global using ASC.Core;
global using ASC.Core.Billing;
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.BackgroundTasks.Log;
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;
global using ASC.EventBus.Abstractions;
global using ASC.EventBus.Events;
global using ASC.Files.Core;
global using ASC.Web.Studio.Core.Notify;

View File

@ -44,7 +44,7 @@ public class BackupDeleteScheldureRequestIntegrationEventHandler : IIntegrationE
public async Task Handle(IntegrationEvent @event)
{
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_logger.InformationHandlingIntegrationEvent(@event.Id, Program.AppName, @event);
_backupService.DeleteSchedule(@event.TenantId);

View File

@ -56,7 +56,7 @@ public class BackupRequestIntegrationEventHandler : IIntegrationEventHandler<Bac
public async Task Handle(BackupRequestIntegrationEvent @event)
{
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_logger.InformationHandlingIntegrationEvent(@event.Id, Program.AppName, @event);
if (!@event.Redelivered)
{

View File

@ -56,7 +56,7 @@ public class BackupRestoreRequestIntegrationEventHandler : IIntegrationEventHand
public async Task Handle(BackupRestoreRequestIntegrationEvent @event)
{
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_logger.InformationHandlingIntegrationEvent(@event.Id, Program.AppName, @event);
if (!@event.Redelivered)
{

View File

@ -0,0 +1,54 @@
// (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
namespace ASC.Data.Backup.BackgroundTasks.Log;
internal static partial class BackupCleanerServiceLogger
{
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupCleanerService is starting.")]
public static partial void DebugBackupCleanerServiceStarting(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupCleanerService background task is stopping.")]
public static partial void DebugBackupCleanerServiceStopping(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupCleanerService background task is doing background work.")]
public static partial void DebugBackupCleanerServiceDoingWork(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupCleanerService background task with instance id {instanceId} is't active.")]
public static partial void DebugBackupCleanerServiceIsNotActive(this ILogger logger, string instanceId);
[LoggerMessage(Level = LogLevel.Debug, Message = "started to clean expired backups")]
public static partial void DebugStartedClean(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "found {count} backups which are expired")]
public static partial void DebugFoundBackups(this ILogger logger, int count);
[LoggerMessage(Level = LogLevel.Debug, Message = "only last {storedCount} scheduled backup records are to keep for tenant {tenantId} so {removeCount} records must be removed")]
public static partial void DebugOnlyLast(this ILogger logger, int storedCount, int tenantId, int removeCount);
[LoggerMessage(Level = LogLevel.Warning, Message = "can't remove backup record {id}")]
public static partial void WarningCanNotRemoveBackup(this ILogger logger, Guid id, Exception exception);
}

View File

@ -0,0 +1,33 @@
// (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
namespace ASC.Data.Backup.BackgroundTasks.Log;
internal static partial class BackupDeleteScheldureRequestIntegrationEventHandlerLogger
{
[LoggerMessage(Level = LogLevel.Information, Message = "----- Handling integration event: {integrationEventId} at {appName} - ({integrationEvent})")]
public static partial void InformationHandlingIntegrationEvent(this ILogger logger, Guid integrationEventId, string appName, IntegrationEvent integrationEvent);
}

View File

@ -0,0 +1,60 @@
// (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
namespace ASC.Data.Backup.BackgroundTasks.Log;
internal static partial class BackupSchedulerServiceLogger
{
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupSchedulerService is starting.")]
public static partial void DebugBackupSchedulerServiceStarting(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupSchedulerService background task is stopping.")]
public static partial void DebugBackupSchedulerServiceStopping(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupSchedulerService background task with instance id {instanceId} is't active.")]
public static partial void DebugBackupSchedulerServiceIsNotActive(this ILogger logger, string instanceId);
[LoggerMessage(Level = LogLevel.Debug, Message = "BackupSchedulerService background task is doing background work.")]
public static partial void DebugBackupSchedulerServiceDoingWork(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "started to schedule backups")]
public static partial void DebugStartedToSchedule(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "{count} backups are to schedule")]
public static partial void DebugBackupsSchedule(this ILogger logger, int count);
[LoggerMessage(Level = LogLevel.Debug, Message = "Start scheduled backup: {tenantId}, {backupMail}, {storageType}, {storageBasePath}")]
public static partial void DebugStartScheduledBackup(this ILogger logger, int tenantId, bool backupMail, BackupStorageType storageType, string storageBasePath);
[LoggerMessage(Level = LogLevel.Debug, Message = "Skip portal {tenantId} not paid")]
public static partial void DebugNotPaid(this ILogger logger, int tenantId);
[LoggerMessage(Level = LogLevel.Debug, Message = "Skip portal {tenantId} haven't access")]
public static partial void DebugHaveNotAccess(this ILogger logger, int tenantId);
[LoggerMessage(Level = LogLevel.Error, Message = "error while scheduling backups")]
public static partial void ErrorBackups(this ILogger logger, Exception exception);
}

View File

@ -22,8 +22,8 @@
//
// 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
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
namespace ASC.Data.Backup.Services;
[Singletone]
@ -45,13 +45,13 @@ internal sealed class BackupCleanerService : BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug("BackupCleanerService is starting.");
_logger.DebugBackupCleanerServiceStarting();
stoppingToken.Register(() => _logger.LogDebug("#1 BackupCleanerService background task is stopping."));
stoppingToken.Register(() => _logger.DebugBackupCleanerServiceStopping());
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogDebug("BackupCleanerService background task is doing background work.");
_logger.DebugBackupCleanerServiceDoingWork();
using var serviceScope = _scopeFactory.CreateScope();
@ -59,7 +59,7 @@ internal sealed class BackupCleanerService : BackgroundService
if (!await registerInstanceService.IsActive(RegisterInstanceWorkerService<BackupCleanerService>.InstanceId))
{
_logger.LogDebug($"BackupCleanerService background task with instance id {RegisterInstanceWorkerService<BackupCleanerService>.InstanceId} is't active.");
_logger.DebugBackupCleanerServiceIsNotActive(RegisterInstanceWorkerService<BackupCleanerService>.InstanceId);
await Task.Delay(1000, stoppingToken);
@ -71,7 +71,7 @@ internal sealed class BackupCleanerService : BackgroundService
await Task.Delay(_backupCleanerPeriod, stoppingToken);
}
_logger.LogDebug("BackupCleanerService background task is stopping.");
_logger.DebugBackupCleanerServiceStopping();
}
private void ExecuteBackupCleaner(CancellationToken stoppingToken)
@ -81,11 +81,11 @@ internal sealed class BackupCleanerService : BackgroundService
var backupRepository = serviceScope.ServiceProvider.GetRequiredService<BackupRepository>();
var backupStorageFactory = serviceScope.ServiceProvider.GetRequiredService<BackupStorageFactory>();
_logger.LogDebug("started to clean expired backups");
_logger.DebugStartedClean();
var backupsToRemove = backupRepository.GetExpiredBackupRecords();
_logger.LogDebug("found {count} backups which are expired", backupsToRemove.Count);
_logger.DebugFoundBackups(backupsToRemove.Count);
foreach (var scheduledBackups in backupRepository.GetScheduledBackupRecords().GroupBy(r => r.TenantId))
{
@ -101,7 +101,7 @@ internal sealed class BackupCleanerService : BackgroundService
var scheduledBackupsToRemove = scheduledBackups.OrderByDescending(r => r.CreatedOn).Skip(schedule.BackupsStored).ToList();
if (scheduledBackupsToRemove.Any())
{
_logger.LogDebug("only last {storedCount} scheduled backup records are to keep for tenant {tenantId} so {removeCount} records must be removed", schedule.BackupsStored, schedule.TenantId, scheduledBackupsToRemove.Count);
_logger.DebugOnlyLast(schedule.BackupsStored, schedule.TenantId, scheduledBackupsToRemove.Count);
backupsToRemove.AddRange(scheduledBackupsToRemove);
}
}
@ -132,7 +132,7 @@ internal sealed class BackupCleanerService : BackgroundService
}
catch (ProviderInfoArgumentException error)
{
_logger.LogWarning(error, "can't remove backup record " + backupRecord.Id);
_logger.WarningCanNotRemoveBackup(backupRecord.Id, error);
if (DateTime.UtcNow > backupRecord.CreatedOn.AddMonths(6))
{
@ -141,7 +141,7 @@ internal sealed class BackupCleanerService : BackgroundService
}
catch (Exception error)
{
_logger.LogWarning(error, "can't remove backup record: " + backupRecord.Id);
_logger.WarningCanNotRemoveBackup(backupRecord.Id, error);
}
}

View File

@ -52,9 +52,9 @@ public sealed class BackupSchedulerService : BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug("BackupSchedulerService is starting.");
_logger.DebugBackupSchedulerServiceStarting();
stoppingToken.Register(() => _logger.LogDebug("#1 BackupSchedulerService background task is stopping."));
stoppingToken.Register(() => _logger.DebugBackupSchedulerServiceStopping());
while (!stoppingToken.IsCancellationRequested)
{
@ -64,21 +64,21 @@ public sealed class BackupSchedulerService : BackgroundService
if (!await registerInstanceService.IsActive(RegisterInstanceWorkerService<BackupSchedulerService>.InstanceId))
{
_logger.LogDebug($"BackupSchedulerService background task with instance id {RegisterInstanceWorkerService<BackupSchedulerService>.InstanceId} is't active.");
_logger.DebugBackupSchedulerServiceIsNotActive(RegisterInstanceWorkerService<BackupSchedulerService>.InstanceId);
await Task.Delay(1000, stoppingToken);
continue;
}
_logger.LogDebug("BackupSchedulerService background task is doing background work.");
_logger.DebugBackupSchedulerServiceDoingWork();
ExecuteBackupScheduler(stoppingToken);
await Task.Delay(_backupSchedulerPeriod, stoppingToken);
}
_logger.LogDebug("BackupSchedulerService background task is stopping.");
_logger.DebugBackupSchedulerServiceStopping();
}
private void ExecuteBackupScheduler(CancellationToken stoppingToken)
@ -90,11 +90,11 @@ public sealed class BackupSchedulerService : BackgroundService
var backupSchedule = serviceScope.ServiceProvider.GetRequiredService<Schedule>();
var tenantManager = serviceScope.ServiceProvider.GetRequiredService<TenantManager>();
_logger.LogDebug("started to schedule backups");
_logger.DebugStartedToSchedule();
var backupsToSchedule = backupRepository.GetBackupSchedules().Where(schedule => backupSchedule.IsToBeProcessed(schedule)).ToList();
_logger.LogDebug("{0} backups are to schedule", backupsToSchedule.Count);
_logger.DebugBackupsSchedule(backupsToSchedule.Count);
foreach (var schedule in backupsToSchedule)
{
@ -115,7 +115,7 @@ public sealed class BackupSchedulerService : BackgroundService
backupRepository.SaveBackupSchedule(schedule);
_logger.LogDebug("Start scheduled backup: {tenantId}, {backupMail}, {storageType}, {storageBasePath}", schedule.TenantId, schedule.BackupMail, schedule.StorageType, schedule.StorageBasePath);
_logger.DebugStartScheduledBackup(schedule.TenantId, schedule.BackupMail, schedule.StorageType, schedule.StorageBasePath);
_eventBus.Publish(new BackupRequestIntegrationEvent(
tenantId: schedule.TenantId,
@ -130,17 +130,17 @@ public sealed class BackupSchedulerService : BackgroundService
}
else
{
_logger.LogDebug("Skip portal {tenantId} not paid", schedule.TenantId);
_logger.DebugNotPaid(schedule.TenantId);
}
}
else
{
_logger.LogDebug("Skip portal {tenantId} haven't access", schedule.TenantId);
_logger.DebugHaveNotAccess(schedule.TenantId);
}
}
catch (Exception error)
{
_logger.LogError(error, "error while scheduling backups");
_logger.ErrorBackups(error);
}
}
}