From 46c50dc8e29d2db09c1299a19b6cb47034dae780 Mon Sep 17 00:00:00 2001 From: Sergey Karmanov Date: Sun, 1 Feb 2026 08:35:24 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=92=D0=B2=D0=B5=D0=BB=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BD=D1=82=D0=BD=D0=BE?= =?UTF-8?q?=D0=B5=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=D1=82=D1=80=D1=83=D0=B4=D0=BD=D0=B8=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Реализовал загрузку списка сотрудников с диска при запуске службы. Добавил методы для сохранения и загрузки сотрудников в файл. --- .../Services/ModeusEmployeeService.cs | 51 ++++++++++--------- SfeduSchedule/Services/ModeusHttpClient.cs | 6 +-- SfeduSchedule/Services/ModeusService.cs | 4 +- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/SfeduSchedule/Services/ModeusEmployeeService.cs b/SfeduSchedule/Services/ModeusEmployeeService.cs index fe81756..0db07ce 100644 --- a/SfeduSchedule/Services/ModeusEmployeeService.cs +++ b/SfeduSchedule/Services/ModeusEmployeeService.cs @@ -1,4 +1,6 @@ -using SfeduSchedule.Logging; +using Quartz; +using SfeduSchedule.Jobs; +using System.Text.Json; namespace SfeduSchedule.Services; @@ -28,29 +30,8 @@ public class ModeusEmployeeService(ISchedulerFactory schedulerFactory) _cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); _backgroundTask = Task.Run(async () => { - try - { - await Task.Delay(TimeSpan.FromSeconds(15), _cts.Token); - - var employees = await modeusService.GetEmployeesAsync(); - if (employees.Count == 0) - { - logger.LogWarningHere("Не удалось получить список сотрудников из Modeus."); - } - else - { - _employees = employees; - logger.LogInformationHere($"Получено {employees.Count} сотрудников из Modeus."); - } - } - catch (OperationCanceledException) - { - // ignore - } - catch (Exception ex) - { - logger.LogErrorHere(ex, "Ошибка при загрузке сотрудников из Modeus."); - } + // Загрузка с диска + await LoadEmployeesFromDisk(); }, _cts.Token); return Task.CompletedTask; @@ -74,4 +55,26 @@ public class ModeusEmployeeService(ISchedulerFactory schedulerFactory) // ignore } } + + public async Task SetEmployees(Dictionary)> employees) + { + _employees = employees; + await SaveEmployeesToDisk(); + } + + private async Task LoadEmployeesFromDisk() + { + + if (File.Exists(_employeesFilePath)) + { + var json = await File.ReadAllTextAsync(_employeesFilePath); + _employees = JsonSerializer.Deserialize)>>(json) ?? new Dictionary)>(); + } + } + + private async Task SaveEmployeesToDisk() + { + var json = JsonSerializer.Serialize(_employees, new JsonSerializerOptions { WriteIndented = false }); + await File.WriteAllTextAsync(_employeesFilePath, json); + } } \ No newline at end of file diff --git a/SfeduSchedule/Services/ModeusHttpClient.cs b/SfeduSchedule/Services/ModeusHttpClient.cs index 6860b3f..23e0fcf 100644 --- a/SfeduSchedule/Services/ModeusHttpClient.cs +++ b/SfeduSchedule/Services/ModeusHttpClient.cs @@ -75,13 +75,13 @@ public class ModeusHttpClient return []; } - public async Task SearchPersonAsync(ModeusSearchPersonRequest modeusSearchPersonRequest) + public async Task SearchPersonAsync(ModeusSearchPersonRequest modeusSearchPersonRequest, CancellationToken cancellationToken = default) { using var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/people/persons/search"); request.Content = JsonContent.Create(modeusSearchPersonRequest, options: GlobalConsts.JsonSerializerOptions); var stopwatch = Stopwatch.StartNew(); - using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); var requestMs = stopwatch.ElapsedMilliseconds; if (response.StatusCode != System.Net.HttpStatusCode.OK) { @@ -95,7 +95,7 @@ public class ModeusHttpClient await using var contentStream = await response.Content.ReadAsStreamAsync(); var content = await JsonSerializer.DeserializeAsync( contentStream, - GlobalConsts.JsonSerializerOptions); + GlobalConsts.JsonSerializerOptions, cancellationToken); var groupMs = stopwatch.ElapsedMilliseconds - deserializeStartMs; _logger.LogInformationHere($"SearchPersonAsync: Request time: {requestMs} ms, Deserialization time: {groupMs} ms, Total time: {stopwatch.ElapsedMilliseconds} ms."); diff --git a/SfeduSchedule/Services/ModeusService.cs b/SfeduSchedule/Services/ModeusService.cs index 8031f52..5a809e4 100644 --- a/SfeduSchedule/Services/ModeusService.cs +++ b/SfeduSchedule/Services/ModeusService.cs @@ -186,10 +186,10 @@ public class ModeusService( return serializedCalendar; } - public async Task)>> GetEmployeesAsync() + public async Task)>> GetEmployeesAsync(CancellationToken cancellationToken = default) { var searchPersonResponse = - await modeusHttpClient.SearchPersonAsync(new ModeusSearchPersonRequest() { Size = 38000 }); + await modeusHttpClient.SearchPersonAsync(new ModeusSearchPersonRequest() { Size = 38000 }, cancellationToken); if (searchPersonResponse == null) { logger.LogErrorHere("persons is null");