Skip to content

Commit 49cd54d

Browse files
authored
Merge pull request #379 from cmu-sei/v8
fixes cancellation token woes
2 parents 7fb427f + 91efacd commit 49cd54d

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/Chat/ChatClient.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using Ghosts.Api.Infrastructure;
1717
using ghosts.api.Infrastructure.Animations.AnimationDefinitions.Chat.Mattermost;
1818
using ghosts.api.Infrastructure.ContentServices;
19-
using Ghosts.Api.Infrastructure.Data;
2019
using ghosts.api.Infrastructure.Extensions;
2120
using Ghosts.Api.Infrastructure.Extensions;
2221
using ghosts.api.Infrastructure.Models;
@@ -35,24 +34,22 @@ public class ChatClient
3534
private readonly HttpClient _client;
3635
private string _token;
3736
private string UserId { get; set; }
38-
private IFormatterService _formatterService;
37+
private readonly IFormatterService _formatterService;
3938

40-
private readonly ApplicationDbContext _context;
41-
private IHubContext<ActivityHub> _activityHubContext;
42-
private CancellationToken _cancellationToken;
39+
private readonly IHubContext<ActivityHub> _activityHubContext;
40+
private readonly CancellationToken _cancellationToken;
4341

4442
public ChatClient(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.ChatSettings chatSettings,
4543
ChatJobConfiguration config, IFormatterService formatterService, IHubContext<ActivityHub> activityHubContext,
46-
ApplicationDbContext context, CancellationToken ct)
44+
CancellationToken ct)
4745
{
4846
_configuration = config;
4947
_chatSettings = chatSettings;
5048
this._baseUrl = _configuration.Chat.BaseUrl;
5149
this._client = new HttpClient();
5250
this._formatterService = formatterService;
53-
this._context = context;
5451
this._activityHubContext = activityHubContext;
55-
this._cancellationToken = _cancellationToken;
52+
this._cancellationToken = ct;
5653
}
5754

5855
private async Task<User> AdminLogin()
@@ -80,7 +77,7 @@ private async Task<User> Login(string username, string password)
8077
var content = new StringContent($"{{\"login_id\":\"{username}\",\"password\":\"{password}\"}}", null,
8178
"application/json");
8279
request.Content = content;
83-
var response = await this._client.SendAsync(request);
80+
var response = await this._client.SendAsync(request, this._cancellationToken);
8481
response.EnsureSuccessStatusCode();
8582

8683
// Reading the 'Token' value from response headers
@@ -94,7 +91,7 @@ private async Task<User> Login(string username, string password)
9491
throw new Exception("Token not found in the response headers.");
9592
}
9693

97-
var contentString = await response.Content.ReadAsStringAsync();
94+
var contentString = await response.Content.ReadAsStringAsync(this._cancellationToken);
9895
var user = JsonSerializer.Deserialize<User>(contentString,
9996
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
10097
if (user == null)
@@ -447,15 +444,15 @@ private async Task<string> ExecuteRequest(HttpRequestMessage request)
447444
{
448445
try
449446
{
450-
var response = await this._client.SendAsync(request);
447+
var response = await this._client.SendAsync(request, this._cancellationToken);
451448
if (response.StatusCode is HttpStatusCode.Forbidden or HttpStatusCode.BadRequest)
452449
{
453-
_log.Info(await response.Content.ReadAsStringAsync());
450+
_log.Info(await response.Content.ReadAsStringAsync(this._cancellationToken));
454451
return string.Empty;
455452
}
456453

457454
response.EnsureSuccessStatusCode();
458-
var contentString = await response.Content.ReadAsStringAsync();
455+
var contentString = await response.Content.ReadAsStringAsync(this._cancellationToken);
459456

460457
return contentString;
461458
}
@@ -472,6 +469,9 @@ public async Task Step(Random random, IEnumerable<NpcRecord> agents)
472469
{
473470
while (!this._cancellationToken.IsCancellationRequested)
474471
{
472+
if (agents == null || !agents.Any())
473+
return;
474+
475475
var u = await this.AdminLogin();
476476
if (u == null)
477477
return;
@@ -669,15 +669,15 @@ await this._activityHubContext.Clients.All.SendAsync("show",
669669
npcId,
670670
"chat",
671671
message,
672-
DateTime.Now.ToString(CultureInfo.InvariantCulture)
672+
DateTime.Now.ToString(CultureInfo.InvariantCulture), this._cancellationToken
673673
);
674674
}
675675
else
676676
{
677677
_log.Info($"{prompt}|NOT SENT|{message}");
678678
}
679679

680-
await Task.Delay(random.Next(5000, 250000));
680+
await Task.Delay(random.Next(5000, 250000), this._cancellationToken);
681681
}
682682
}
683683
catch (Exception e)

src/Ghosts.Api/Infrastructure/Animations/AnimationDefinitions/ChatJob.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class ChatJob
2525
private readonly Random _random;
2626
private readonly ChatClient _chatClient;
2727
private readonly int _currentStep;
28-
private CancellationToken _cancellationToken;
29-
private IFormatterService _formatterService;
28+
private readonly CancellationToken _cancellationToken;
29+
private readonly IFormatterService _formatterService;
3030

3131
public ChatJob(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.ChatSettings configuration, IServiceScopeFactory scopeFactory, Random random,
3232
IHubContext<ActivityHub> activityHubContext, CancellationToken cancellationToken)
@@ -47,7 +47,7 @@ public ChatJob(ApplicationSettings.AnimatorSettingsDetail.AnimationsSettings.Cha
4747
this._formatterService =
4848
new ContentCreationService(_configuration.ContentEngine).FormatterService;
4949

50-
this._chatClient = new ChatClient(_configuration, chatConfiguration, this._formatterService, activityHubContext, this._context, this._cancellationToken);
50+
this._chatClient = new ChatClient(_configuration, chatConfiguration, this._formatterService, activityHubContext, this._cancellationToken);
5151

5252
while (!_cancellationToken.IsCancellationRequested)
5353
{

0 commit comments

Comments
 (0)