Skip to content

Commit

Permalink
Merge pull request #391 from cmu-sei/v8
Browse files Browse the repository at this point in the history
Temp fix for #385
  • Loading branch information
sei-dupdyke committed Aug 9, 2024
2 parents 098e721 + 5fde4fa commit eb6d0a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading;
using System.Threading.Tasks;
using Ghosts.Animator.Extensions;
using Ghosts.Api;
using ghosts.api.Hubs;
using Ghosts.Api.Infrastructure;
using Ghosts.Api.Infrastructure.Data;
Expand All @@ -35,7 +34,8 @@ public class SocialGraphJob
private bool _isEnabled = true;
private CancellationToken _cancellationToken;
private readonly IHubContext<ActivityHub> _activityHubContext;

private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);

public SocialGraphJob(ApplicationSettings configuration, IServiceScopeFactory scopeFactory, Random random,
IHubContext<ActivityHub> activityHubContext, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -66,17 +66,18 @@ public SocialGraphJob(ApplicationSettings configuration, IServiceScopeFactory sc
{
foreach (var npc in npcs)
{
this.Step(npc);
_ = this.Step(npc);
}

_log.Info(
$"Step complete, sleeping for {this._configuration.AnimatorSettings.Animations.SocialGraph.TurnLength}ms");
Thread.Sleep(this._configuration.AnimatorSettings.Animations.SocialGraph.TurnLength);
}
}
catch (ThreadInterruptedException)
catch (ThreadInterruptedException e)
{
// continue
_log.Error(e);
}
catch (Exception e)
{
Expand Down Expand Up @@ -112,9 +113,47 @@ private async Task Step(NpcRecord npc)
});
}

npc.NpcSocialGraph = graph;
await this._context.SaveChangesAsync(_cancellationToken);
_log.Trace($"Social graph saved for {npc.NpcProfile.Name}...");
await _semaphore.WaitAsync(_cancellationToken);

try
{
npc.NpcSocialGraph = graph;
this._context.Entry(npc).State = EntityState.Modified;
await this._context.SaveChangesAsync(_cancellationToken);
_log.Trace($"Social graph saved for {npc.NpcProfile.Name}...");
}
catch (Exception e)
{
_log.Trace($"Error creating new social graph: {e}. Continuing...");
}
finally
{
_semaphore.Release();
}
}
else
{
if (npc.NpcSocialGraph.Id != npc.Id)
{
// reports of 00000... ids in the social graph. not sure how this is happening? old version of api that is out there?
await _semaphore.WaitAsync(_cancellationToken);

try
{
npc.NpcSocialGraph.Id = npc.Id;
this._context.Entry(npc).State = EntityState.Modified;
await this._context.SaveChangesAsync(_cancellationToken);
_log.Trace($"Social graph id corrected for {npc.NpcProfile.Id}...");
}
catch (Exception e)
{
_log.Trace($"Error correcting social graph id: {e}. Continuing...");
}
finally
{
_semaphore.Release();
}
}
}

_log.Trace("Social graph step proceeding...");
Expand Down
2 changes: 1 addition & 1 deletion src/Ghosts.Api/ghosts.api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>ghosts.api</AssemblyName>

<AssemblyVersion>8.0.0.0</AssemblyVersion>
<FileVersion>8.1.1.0</FileVersion>
<FileVersion>8.1.2.0</FileVersion>

<Authors>GHOSTS Development Team for CERT > Software Engineering Institute > Carnegie Mellon University</Authors>
<Company>Carnegie Mellon University</Company>
Expand Down

0 comments on commit eb6d0a2

Please sign in to comment.