Фоновое удаление файлов (тех, дата загрузки которых составляет более 30 дней от текущей даты)

This commit is contained in:
Olga Nemt 2023-11-08 17:12:53 +05:00
parent 395cda4e10
commit 1458c76c8d
4 changed files with 54 additions and 10 deletions

View File

@ -57,5 +57,13 @@ namespace AsbCloudApp.Services
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ReportPropertiesDto>> GetAllReportsByWellAsync(int idWell, CancellationToken token);
/// <summary>
/// Удаление отчетов, с момента загрузки которых прошло n дней
/// </summary>
/// <param name="days">период, за который отчеты хранятся на сервере и при превышении которого удаляются</param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteAllOldReportsAsync(int days, CancellationToken token);
}
}

View File

@ -147,17 +147,17 @@ namespace AsbCloudInfrastructure.Repository
public async Task<IEnumerable<FileInfoDto>> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = dbSetConfigured
.Where(f => ids.Contains(f.Id) && f.IsDeleted);
.Where(f => ids.Contains(f.Id));
var files = await query.ToListAsync(token);
var files = await query.ToListAsync(token);
var filesDtos = files.Select(x => Convert(x));
var filesDtos = files.Select(x => Convert(x));
db.Files.RemoveRange(query);
await db.SaveChangesAsync(token).ConfigureAwait(false);
db.Files.RemoveRange(query);
await db.SaveChangesAsync(token).ConfigureAwait(false);
return filesDtos;
}
return filesDtos;
}
public async Task<FileInfoDto> GetByMarkId(int idMark, CancellationToken token)
{

View File

@ -18,22 +18,39 @@ namespace AsbCloudInfrastructure.Services
public class ReportService : IReportService
{
public class WorkToDeleteOldReports : Work
{
public WorkToDeleteOldReports()
: base("work to delete reports older than 30 days")
{
Timeout = TimeSpan.FromMinutes(10);
}
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
{
var reportService = services.GetRequiredService<IReportService>();
await reportService.DeleteAllOldReportsAsync(-30, token);
}
}
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private readonly FileService fileService;
private readonly IWellService wellService;
private readonly BackgroundWorker backgroundWorkerService;
public int ReportCategoryId { get; private set; }
public ReportService(IAsbCloudDbContext db,
ITelemetryService telemetryService,
IWellService wellService,
ITelemetryService telemetryService,
IWellService wellService,
FileService fileService,
BackgroundWorker backgroundWorkerService)
{
this.db = db;
this.wellService = wellService;
this.backgroundWorkerService = backgroundWorkerService;
this.telemetryService = telemetryService;
this.fileService = fileService;
ReportCategoryId = db.FileCategories
.AsNoTracking()
.First(c => c.Name.Equals("Рапорт"))
@ -69,7 +86,7 @@ namespace AsbCloudInfrastructure.Services
progressHandler.Invoke(arg, id);
};
generator.Make(reportFileName);
var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!;
progressHandler.Invoke(new
@ -186,6 +203,24 @@ namespace AsbCloudInfrastructure.Services
return generator;
}
public async Task<int> DeleteAllOldReportsAsync(int days, CancellationToken token)
{
var reports = db.ReportProperties
.Include(r => r.File)
.Where(r => r.File.UploadDate.Date < DateTime.UtcNow.Date.AddDays(days))
.AsNoTracking();
var files = await reports.Select(r => r.IdFile).ToListAsync(token);
if (files.Any())
{
await fileService.DeleteAsync(files, token);
}
db.ReportProperties.RemoveRange(reports);
return await db.SaveChangesAsync(token);
}
}
}

View File

@ -29,6 +29,7 @@ namespace AsbCloudInfrastructure
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSpinDto>>();
var backgroundWorker = provider.GetRequiredService<PeriodicBackgroundWorker>();
backgroundWorker.Add<ReportService.WorkToDeleteOldReports>(TimeSpan.FromDays(1));
backgroundWorker.Add<WellInfoService.WorkWellInfoUpdate>(TimeSpan.FromMinutes(30));
backgroundWorker.Add<WorkOperationDetection>(TimeSpan.FromMinutes(15));
backgroundWorker.Add<WorkSubsystemOperationTimeCalc>(TimeSpan.FromMinutes(30));