Skip to content

Commit

Permalink
Merge pull request #379 from cmu-sei/v8
Browse files Browse the repository at this point in the history
fixes cancellation token woes
  • Loading branch information
sei-dupdyke authored Jul 19, 2024
2 parents 7fb427f + 91efacd commit 49cd54d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ActivityHub> _activityHubContext;
private CancellationToken _cancellationToken;
private readonly IHubContext<ActivityHub> _activityHubContext;
private readonly CancellationToken _cancellationToken;

public ChatClient(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.ChatSettings chatSettings,
ChatJobConfiguration config, IFormatterService formatterService, IHubContext<ActivityHub> 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<User> AdminLogin()
Expand Down Expand Up @@ -80,7 +77,7 @@ private async Task<User> 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
Expand All @@ -94,7 +91,7 @@ private async Task<User> 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<User>(contentString,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (user == null)
Expand Down Expand Up @@ -447,15 +444,15 @@ private async Task<string> 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;
}
Expand All @@ -472,6 +469,9 @@ public async Task Step(Random random, IEnumerable<NpcRecord> agents)
{
while (!this._cancellationToken.IsCancellationRequested)
{
if (agents == null || !agents.Any())
return;

var u = await this.AdminLogin();
if (u == null)
return;
Expand Down Expand Up @@ -669,15 +669,15 @@ await this._activityHubContext.Clients.All.SendAsync("show",
npcId,
"chat",
message,
DateTime.Now.ToString(CultureInfo.InvariantCulture)
DateTime.Now.ToString(CultureInfo.InvariantCulture), this._cancellationToken
);
}
else
{
_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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityHub> activityHubContext, CancellationToken cancellationToken)
Expand All @@ -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)
{
Expand Down

0 comments on commit 49cd54d

Please sign in to comment.