Skip to content

Commit

Permalink
backup: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeybannov committed Jan 18, 2022
1 parent d3c1dfd commit 07ebea5
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 80 deletions.
2 changes: 1 addition & 1 deletion common/ASC.Data.Backup.Core/BackupAjaxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using ASC.Core.Common.Configuration;
using ASC.Core.Users;
using ASC.Data.Backup.Contracts;
using ASC.Data.Backup.Service;
using ASC.Data.Backup.Services;
using ASC.MessagingSystem;
using ASC.Notify.Cron;
using ASC.Web.Core.PublicResources;
Expand Down
42 changes: 0 additions & 42 deletions common/ASC.Data.Backup.Core/Listerners/BackupListener.cs

This file was deleted.

2 changes: 1 addition & 1 deletion common/ASC.Data.Backup.Core/Service/BackupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

using Newtonsoft.Json;

namespace ASC.Data.Backup.Service
namespace ASC.Data.Backup.Services
{
[Scope]
public class BackupService : IBackupService
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Data.Backup.Core/Service/BackupSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using ASC.Common.Utils;

namespace ASC.Data.Backup.Service
namespace ASC.Data.Backup.Services
{
public class BackupSettings
{
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Data.Backup.Core/Service/BackupWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

using Newtonsoft.Json;

namespace ASC.Data.Backup.Service
namespace ASC.Data.Backup.Services
{
[Singletone(Additional = typeof(BackupWorkerExtension))]
public class BackupWorker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
using ASC.Core;
using ASC.Data.Backup.Contracts;
using ASC.Data.Backup.EF.Model;
using ASC.Data.Backup.Service;
using ASC.Data.Backup.Services;
using ASC.Data.Backup.Utils;

using Microsoft.Extensions.Options;
Expand Down
19 changes: 9 additions & 10 deletions common/services/ASC.Data.Backup/Services/BackupCleanerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.Data.Backup.Storage;
using ASC.ElasticSearch.Service;
using ASC.Files.Core;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace ASC.Data.Backup.Service
namespace ASC.Data.Backup.Services
{
[Singletone]
internal sealed class BackupCleanerService : IHostedService, IDisposable
Expand All @@ -49,9 +50,8 @@ internal sealed class BackupCleanerService : IHostedService, IDisposable
private readonly TimeSpan _period;

private readonly IServiceScopeFactory _scopeFactory;
private IServiceScope _serviceScope;

public BackupCleanerService(
public BackupCleanerService(
ConfigurationExtension configuration,
IOptionsMonitor<ILog> options,
IServiceScopeFactory scopeFactory)
Expand All @@ -65,18 +65,19 @@ public Task StartAsync(CancellationToken cancellationToken)
{
_logger.Info("starting backup cleaner service...");

_serviceScope = _scopeFactory.CreateScope();
_timer = new Timer(DeleteExpiredBackups, null, TimeSpan.Zero, _period);

_logger.Info("backup cleaner service started");

return Task.CompletedTask;
}

public void DeleteExpiredBackups(object state)
{
var backupRepository = _serviceScope.ServiceProvider.GetRequiredService<BackupRepository>();
var backupStorageFactory = _serviceScope.ServiceProvider.GetRequiredService<BackupStorageFactory>();
using var serviceScope = _scopeFactory.CreateScope();

var backupRepository = serviceScope.ServiceProvider.GetRequiredService<BackupRepository>();
var backupStorageFactory = serviceScope.ServiceProvider.GetRequiredService<BackupStorageFactory>();

_logger.Debug("started to clean expired backups");

Expand Down Expand Up @@ -117,7 +118,7 @@ public void DeleteExpiredBackups(object state)
catch (ProviderInfoArgumentException error)
{
_logger.Warn("can't remove backup record " + backupRecord.Id, error);

if (DateTime.UtcNow > backupRecord.CreatedOn.AddMonths(6))
{
backupRepository.DeleteBackupRecord(backupRecord.Id);
Expand All @@ -135,7 +136,6 @@ public Task StopAsync(CancellationToken cancellationToken)
_logger.Info("stopping backup cleaner service...");

_timer?.Change(Timeout.Infinite, 0);

_logger.Info("backup cleaner service stopped");

return Task.CompletedTask;
Expand All @@ -144,7 +144,6 @@ public Task StopAsync(CancellationToken cancellationToken)
public void Dispose()
{
_timer?.Dispose();
_serviceScope?.Dispose();
}
}
}
52 changes: 52 additions & 0 deletions common/services/ASC.Data.Backup/Services/BackupListenerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

using System;
using System.Threading;
using System.Threading.Tasks;

using ASC.Common;
using ASC.Common.Caching;
using ASC.Data.Backup.Contracts;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ASC.Data.Backup.Services
{
[Singletone]
internal sealed class BackupListenerService : IHostedService
{
private readonly ICacheNotify<DeleteSchedule> _cacheDeleteSchedule;
private readonly IServiceScopeFactory _scopeFactory;

public BackupListenerService(ICacheNotify<DeleteSchedule> cacheDeleteSchedule,
IServiceScopeFactory scopeFactory)
{
_cacheDeleteSchedule = cacheDeleteSchedule;
_scopeFactory = scopeFactory;
}

public void DeleteScheldure(DeleteSchedule deleteSchedule)
{
using (var scope = _scopeFactory.CreateScope())
{
var backupService = scope.ServiceProvider.GetService<BackupService>();

backupService.DeleteSchedule(deleteSchedule.TenantId);
}
}

public Task StartAsync(CancellationToken cancellationToken)
{
_cacheDeleteSchedule.Subscribe((n) => DeleteScheldure(n), CacheNotifyAction.Insert);

return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
_cacheDeleteSchedule.Unsubscribe(CacheNotifyAction.Insert);

return Task.CompletedTask;
}
}
}
20 changes: 10 additions & 10 deletions common/services/ASC.Data.Backup/Services/BackupSchedulerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
using ASC.Core;
using ASC.Core.Billing;
using ASC.Data.Backup.Storage;
using ASC.ElasticSearch.Service;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace ASC.Data.Backup.Service
namespace ASC.Data.Backup.Services
{
[Singletone]
internal sealed class BackupSchedulerService : IHostedService, IDisposable
Expand All @@ -52,7 +53,6 @@ internal sealed class BackupSchedulerService : IHostedService, IDisposable
private readonly BackupWorker _backupWorker;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly IServiceScopeFactory _scopeFactory;
private IServiceScope _serviceScope;

public BackupSchedulerService(
IOptionsMonitor<ILog> options,
Expand All @@ -72,7 +72,6 @@ public Task StartAsync(CancellationToken cancellationToken)
{
_logger.Info("starting backup scheduler service...");

_serviceScope = _scopeFactory.CreateScope();
_timer = new Timer(ScheduleBackupTasks, null, TimeSpan.Zero, _period);

_logger.Info("backup scheduler service started");
Expand All @@ -82,10 +81,12 @@ public Task StartAsync(CancellationToken cancellationToken)

public void ScheduleBackupTasks(object state)
{
var paymentManager = _serviceScope.ServiceProvider.GetRequiredService<PaymentManager>();
var backupRepository = _serviceScope.ServiceProvider.GetRequiredService<BackupRepository>(); ;
var backupSchedule = _serviceScope.ServiceProvider.GetRequiredService<Schedule>();
var tenantManager = _serviceScope.ServiceProvider.GetRequiredService<TenantManager>();
using var serviceScope = _scopeFactory.CreateScope();

var paymentManager = serviceScope.ServiceProvider.GetRequiredService<PaymentManager>();
var backupRepository = serviceScope.ServiceProvider.GetRequiredService<BackupRepository>(); ;
var backupSchedule = serviceScope.ServiceProvider.GetRequiredService<Schedule>();
var tenantManager = serviceScope.ServiceProvider.GetRequiredService<TenantManager>();

_logger.DebugFormat("started to schedule backups");

Expand All @@ -106,7 +107,7 @@ public void ScheduleBackupTasks(object state)

backupRepository.SaveBackupSchedule(schedule);
_logger.DebugFormat("Start scheduled backup: {0}, {1}, {2}, {3}", schedule.TenantId, schedule.BackupMail, schedule.StorageType, schedule.StorageBasePath);
_backupWorker.StartScheduledBackup(schedule);
_backupWorker.StartScheduledBackup(schedule);
}
else
{
Expand All @@ -131,15 +132,14 @@ public Task StopAsync(CancellationToken cancellationToken)
_logger.Info("stopping backup cleaner service...");

_timer?.Change(Timeout.Infinite, 0);

_logger.Info("backup cleaner service stopped");

return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
_serviceScope?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,26 @@

using ASC.Common;
using ASC.Common.Utils;
using ASC.Data.Backup.Listerners;
using ASC.Web.Studio.Core.Notify;

using Microsoft.Extensions.Hosting;

namespace ASC.Data.Backup.Service
namespace ASC.Data.Backup.Services
{
[Singletone]
internal sealed class BackupLauncherService : IHostedService
internal sealed class BackupWorkerService : IHostedService
{
private readonly BackupWorker _backupWorker;
private readonly ConfigurationExtension _configuration;
private readonly BackupListener _backupListener;
private readonly NotifyConfiguration _notifyConfiguration;

public BackupLauncherService(
public BackupWorkerService(
BackupWorker backupWorker,
ConfigurationExtension configuration,
BackupListener backupListener,
NotifyConfiguration notifyConfiguration)
{
_backupWorker = backupWorker;
_configuration = configuration;
_backupListener = backupListener;
_notifyConfiguration = notifyConfiguration;
}

Expand All @@ -63,15 +59,13 @@ public Task StartAsync(CancellationToken cancellationToken)
var settings = _configuration.GetSetting<BackupSettings>("backup");

_backupWorker.Start(settings);
_backupListener.Start();

return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
_backupWorker.Stop();
_backupListener.Stop();

return Task.CompletedTask;
}
Expand Down
13 changes: 9 additions & 4 deletions common/services/ASC.Data.Backup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
using ASC.Common;
using ASC.Common.Threading;
using ASC.Data.Backup.Controllers;
using ASC.Data.Backup.Service;
using ASC.Data.Backup.Services;
using ASC.Web.Studio.Core.Notify;

using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -57,17 +57,22 @@ public override void ConfigureServices(IServiceCollection services)
DIHelper.TryAdd<RestoreProgressItem>();
DIHelper.TryAdd<TransferProgressItem>();

DIHelper.TryAdd<BackupCleanerService>();
DIHelper.TryAdd<BackupSchedulerService>();
DIHelper.TryAdd<Schedule>();

DIHelper.TryAdd<BackupController>();

DIHelper.TryAdd<BackupCleanerService>();
DIHelper.TryAdd<BackupSchedulerService>();
DIHelper.TryAdd<BackupListenerService>();
DIHelper.TryAdd<BackupWorkerService>();

NotifyConfigurationExtension.Register(DIHelper);

services.AddHostedService<BackupCleanerService>();
services.AddHostedService<BackupSchedulerService>();

services.AddHostedService<BackupListenerService>();
services.AddHostedService<BackupWorkerService>();

services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(Configuration.GetSection("Redis").Get<RedisConfiguration>());
}
}
Expand Down

0 comments on commit 07ebea5

Please sign in to comment.