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
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
namespace ASC.Data.Backup.Services;
|
|
|
|
|
|
|
|
[Singletone]
|
2022-02-23 12:33:42 +00:00
|
|
|
public sealed class BackupSchedulerService : BackgroundService
|
2022-01-17 11:15:46 +00:00
|
|
|
{
|
2022-02-23 12:33:42 +00:00
|
|
|
private readonly TimeSpan _backupSchedulerPeriod;
|
2022-05-26 09:01:54 +00:00
|
|
|
private readonly ILogger<BackupSchedulerService> _logger;
|
2022-01-19 16:25:10 +00:00
|
|
|
|
|
|
|
private readonly CoreBaseSettings _coreBaseSettings;
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
2022-02-23 12:33:42 +00:00
|
|
|
private readonly IEventBus _eventBus;
|
2022-01-19 16:25:10 +00:00
|
|
|
|
|
|
|
public BackupSchedulerService(
|
2022-04-26 17:53:48 +00:00
|
|
|
ILogger<BackupSchedulerService> logger,
|
2022-01-19 16:25:10 +00:00
|
|
|
IServiceScopeFactory scopeFactory,
|
|
|
|
ConfigurationExtension configuration,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
2022-02-23 12:33:42 +00:00
|
|
|
IEventBus eventBus)
|
2022-01-17 11:15:46 +00:00
|
|
|
{
|
2022-03-17 10:13:52 +00:00
|
|
|
_logger = logger;
|
2022-01-19 16:25:10 +00:00
|
|
|
_coreBaseSettings = coreBaseSettings;
|
2022-02-23 12:33:42 +00:00
|
|
|
_backupSchedulerPeriod = configuration.GetSetting<BackupSettings>("backup").Scheduler.Period;
|
2022-01-19 16:25:10 +00:00
|
|
|
_scopeFactory = scopeFactory;
|
2022-02-28 14:48:55 +00:00
|
|
|
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
|
2022-01-19 16:25:10 +00:00
|
|
|
}
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-02-23 12:33:42 +00:00
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
2022-01-19 16:25:10 +00:00
|
|
|
{
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugBackupSchedulerServiceStarting();
|
2022-02-24 14:15:39 +00:00
|
|
|
|
2022-05-06 10:38:26 +00:00
|
|
|
stoppingToken.Register(() => _logger.DebugBackupSchedulerServiceStopping());
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-02-23 12:33:42 +00:00
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
{
|
2022-02-24 15:01:32 +00:00
|
|
|
using var serviceScope = _scopeFactory.CreateScope();
|
|
|
|
|
|
|
|
var registerInstanceService = serviceScope.ServiceProvider.GetService<IRegisterInstanceManager<BackupSchedulerService>>();
|
|
|
|
|
|
|
|
if (!await registerInstanceService.IsActive(RegisterInstanceWorkerService<BackupSchedulerService>.InstanceId))
|
|
|
|
{
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugBackupSchedulerServiceIsNotActive(RegisterInstanceWorkerService<BackupSchedulerService>.InstanceId);
|
2022-02-24 15:01:32 +00:00
|
|
|
|
|
|
|
await Task.Delay(1000, stoppingToken);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugBackupSchedulerServiceDoingWork();
|
2022-02-24 14:15:39 +00:00
|
|
|
|
2022-02-24 15:01:32 +00:00
|
|
|
ExecuteBackupScheduler(stoppingToken);
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-02-23 12:33:42 +00:00
|
|
|
await Task.Delay(_backupSchedulerPeriod, stoppingToken);
|
|
|
|
}
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugBackupSchedulerServiceStopping();
|
2022-01-19 16:25:10 +00:00
|
|
|
}
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-02-24 15:01:32 +00:00
|
|
|
private void ExecuteBackupScheduler(CancellationToken stoppingToken)
|
2022-01-19 16:25:10 +00:00
|
|
|
{
|
|
|
|
using var serviceScope = _scopeFactory.CreateScope();
|
2022-01-18 16:04:35 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
var paymentManager = serviceScope.ServiceProvider.GetRequiredService<PaymentManager>();
|
|
|
|
var backupRepository = serviceScope.ServiceProvider.GetRequiredService<BackupRepository>(); ;
|
|
|
|
var backupSchedule = serviceScope.ServiceProvider.GetRequiredService<Schedule>();
|
|
|
|
var tenantManager = serviceScope.ServiceProvider.GetRequiredService<TenantManager>();
|
2022-02-24 15:01:32 +00:00
|
|
|
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugStartedToSchedule();
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
var backupsToSchedule = backupRepository.GetBackupSchedules().Where(schedule => backupSchedule.IsToBeProcessed(schedule)).ToList();
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugBackupsSchedule(backupsToSchedule.Count);
|
2022-01-17 11:15:46 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
foreach (var schedule in backupsToSchedule)
|
|
|
|
{
|
2022-04-14 19:23:57 +00:00
|
|
|
if (stoppingToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
try
|
2022-01-17 11:15:46 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
if (_coreBaseSettings.Standalone || tenantManager.GetTenantQuota(schedule.TenantId).AutoBackup)
|
2022-01-17 11:15:46 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
var tariff = paymentManager.GetTariff(schedule.TenantId);
|
2022-02-23 12:33:42 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
if (tariff.State < TariffState.Delay)
|
2022-01-17 11:15:46 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
schedule.LastBackupTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
backupRepository.SaveBackupSchedule(schedule);
|
2022-02-23 12:33:42 +00:00
|
|
|
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugStartScheduledBackup(schedule.TenantId, schedule.BackupMail, schedule.StorageType, schedule.StorageBasePath);
|
2022-02-23 12:33:42 +00:00
|
|
|
|
2022-02-24 16:53:49 +00:00
|
|
|
_eventBus.Publish(new BackupRequestIntegrationEvent(
|
|
|
|
tenantId: schedule.TenantId,
|
|
|
|
storageBasePath: schedule.StorageBasePath,
|
2022-04-14 19:42:15 +00:00
|
|
|
storageParams: JsonConvert.DeserializeObject<Dictionary<string, string>>(schedule.StorageParams),
|
2022-02-24 16:53:49 +00:00
|
|
|
storageType: schedule.StorageType,
|
|
|
|
backupMail: schedule.BackupMail,
|
|
|
|
createBy: ASC.Core.Configuration.Constants.CoreSystem.ID,
|
|
|
|
isScheduled: true,
|
2022-04-14 19:42:15 +00:00
|
|
|
backupsStored: schedule.BackupsStored
|
2022-02-24 16:53:49 +00:00
|
|
|
));
|
2022-01-17 11:15:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugNotPaid(schedule.TenantId);
|
2022-01-17 11:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 16:25:10 +00:00
|
|
|
else
|
2022-01-17 11:15:46 +00:00
|
|
|
{
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.DebugHaveNotAccess(schedule.TenantId);
|
2022-01-17 11:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 16:25:10 +00:00
|
|
|
catch (Exception error)
|
|
|
|
{
|
2022-05-06 10:38:26 +00:00
|
|
|
_logger.ErrorBackups(error);
|
2022-01-19 16:25:10 +00:00
|
|
|
}
|
2022-01-17 11:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|