diff --git a/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/Chat/ChatClient.cs b/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/Chat/ChatClient.cs index fe1cfe0b..da3bc72a 100644 --- a/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/Chat/ChatClient.cs +++ b/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/Chat/ChatClient.cs @@ -16,7 +16,6 @@ using Ghosts.Api.Infrastructure; using ghosts.api.Infrastructure.Animations.AnimationDefinitions.Chat.Mattermost; using ghosts.api.Infrastructure.ContentServices; -using Ghosts.Api.Infrastructure.Data; using ghosts.api.Infrastructure.Extensions; using Ghosts.Api.Infrastructure.Extensions; using ghosts.api.Infrastructure.Models; @@ -35,24 +34,22 @@ public class ChatClient private readonly HttpClient _client; private string _token; private string UserId { get; set; } - private IFormatterService _formatterService; + private readonly IFormatterService _formatterService; - private readonly ApplicationDbContext _context; - private IHubContext _activityHubContext; - private CancellationToken _cancellationToken; + private readonly IHubContext _activityHubContext; + private readonly CancellationToken _cancellationToken; public ChatClient(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.ChatSettings chatSettings, ChatJobConfiguration config, IFormatterService formatterService, IHubContext activityHubContext, - ApplicationDbContext context, CancellationToken ct) + CancellationToken ct) { _configuration = config; _chatSettings = chatSettings; this._baseUrl = _configuration.Chat.BaseUrl; this._client = new HttpClient(); this._formatterService = formatterService; - this._context = context; this._activityHubContext = activityHubContext; - this._cancellationToken = _cancellationToken; + this._cancellationToken = ct; } private async Task AdminLogin() @@ -80,7 +77,7 @@ private async Task Login(string username, string password) var content = new StringContent($"{{\"login_id\":\"{username}\",\"password\":\"{password}\"}}", null, "application/json"); request.Content = content; - var response = await this._client.SendAsync(request); + var response = await this._client.SendAsync(request, this._cancellationToken); response.EnsureSuccessStatusCode(); // Reading the 'Token' value from response headers @@ -94,7 +91,7 @@ private async Task Login(string username, string password) throw new Exception("Token not found in the response headers."); } - var contentString = await response.Content.ReadAsStringAsync(); + var contentString = await response.Content.ReadAsStringAsync(this._cancellationToken); var user = JsonSerializer.Deserialize(contentString, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (user == null) @@ -447,15 +444,15 @@ private async Task ExecuteRequest(HttpRequestMessage request) { try { - var response = await this._client.SendAsync(request); + var response = await this._client.SendAsync(request, this._cancellationToken); if (response.StatusCode is HttpStatusCode.Forbidden or HttpStatusCode.BadRequest) { - _log.Info(await response.Content.ReadAsStringAsync()); + _log.Info(await response.Content.ReadAsStringAsync(this._cancellationToken)); return string.Empty; } response.EnsureSuccessStatusCode(); - var contentString = await response.Content.ReadAsStringAsync(); + var contentString = await response.Content.ReadAsStringAsync(this._cancellationToken); return contentString; } @@ -472,6 +469,9 @@ public async Task Step(Random random, IEnumerable agents) { while (!this._cancellationToken.IsCancellationRequested) { + if (agents == null || !agents.Any()) + return; + var u = await this.AdminLogin(); if (u == null) return; @@ -669,7 +669,7 @@ await this._activityHubContext.Clients.All.SendAsync("show", npcId, "chat", message, - DateTime.Now.ToString(CultureInfo.InvariantCulture) + DateTime.Now.ToString(CultureInfo.InvariantCulture), this._cancellationToken ); } else @@ -677,7 +677,7 @@ await this._activityHubContext.Clients.All.SendAsync("show", _log.Info($"{prompt}|NOT SENT|{message}"); } - await Task.Delay(random.Next(5000, 250000)); + await Task.Delay(random.Next(5000, 250000), this._cancellationToken); } } catch (Exception e) diff --git a/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/ChatJob.cs b/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/ChatJob.cs index 0150a369..7e11578e 100644 --- a/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/ChatJob.cs +++ b/src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/ChatJob.cs @@ -25,8 +25,8 @@ public class ChatJob private readonly Random _random; private readonly ChatClient _chatClient; private readonly int _currentStep; - private CancellationToken _cancellationToken; - private IFormatterService _formatterService; + private readonly CancellationToken _cancellationToken; + private readonly IFormatterService _formatterService; public ChatJob(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.ChatSettings configuration, IServiceScopeFactory scopeFactory, Random random, IHubContext activityHubContext, CancellationToken cancellationToken) @@ -47,7 +47,7 @@ public ChatJob(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.Cha this._formatterService = new ContentCreationService(_configuration.ContentEngine).FormatterService; - this._chatClient = new ChatClient(_configuration, chatConfiguration, this._formatterService, activityHubContext, this._context, this._cancellationToken); + this._chatClient = new ChatClient(_configuration, chatConfiguration, this._formatterService, activityHubContext, this._cancellationToken); while (!_cancellationToken.IsCancellationRequested) {