Skip to content

Commit

Permalink
Fix not stopping and reporting when error occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuniwak committed Nov 9, 2023
1 parent f5eaf92 commit eb4e90c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
23 changes: 17 additions & 6 deletions Runtime/AgentDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2023 DeNA Co., Ltd.
// This software is released under the MIT License.

using System;
using DeNA.Anjin.Agents;
using DeNA.Anjin.Settings;
using DeNA.Anjin.Utilities;
Expand Down Expand Up @@ -88,27 +89,37 @@ public void DispatchByScene(Scene scene)
agent = _settings.fallbackAgent;
}

DispatchAgent(agent);
DispatchAgent(agent).Forget();

if (_settings.observerAgent != null)
{
DispatchAgent(_settings.observerAgent);
DispatchAgent(_settings.observerAgent).Forget();
// Note: The ObserverAgent is not made to DontDestroyOnLoad, to start every time.
// Because it is a source of bugs to force the implementation of DontDestroyOnLoad to the descendants of the Composite.
}
}

private void DispatchAgent(AbstractAgent agent)
private async UniTask DispatchAgent(AbstractAgent agent)
{
var agentName = agent.name;
var gameObject = new GameObject(agentName);
var token = gameObject.GetCancellationTokenOnDestroy(); // Agent also dies when GameObject is destroyed

agent.Logger = _logger;
agent.Random = _randomFactory.CreateRandom();
agent.Run(token).Forget();

_logger.Log($"Agent {agentName} dispatched!");

try
{
agent.Run(token).Forget();
_logger.Log($"Agent {agentName} dispatched!");
}
catch (Exception e)
{
// XXX: Wait 1 frame to fix https://github.com/DeNA/Anjin/issues/20
await UniTask.DelayFrame(1, cancellationToken: token);
Debug.LogException(e);
_logger.Log($"Agent {agentName} dispatched but immediately threw an error!");
}
}
}
}
11 changes: 7 additions & 4 deletions Runtime/Autopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ private void Start()

_randomFactory = new RandomFactory(seed);
_logger.Log($"Random seed is {seed}");

// NOTE: Registering logMessageReceived must be placed before DispatchByScene.
// Because some agent can throw an error immediately, so reporter can miss the error if
// registering logMessageReceived is placed after DispatchByScene.
_reporter = new SlackReporter(_settings, new SlackAPI());
_logMessageHandler = new LogMessageHandler(_settings, _reporter);
Application.logMessageReceived += _logMessageHandler.HandleLog;

_dispatcher = new AgentDispatcher(_settings, _logger, _randomFactory);
_dispatcher.DispatchByScene(SceneManager.GetActiveScene());
SceneManager.activeSceneChanged += _dispatcher.DispatchByScene;

_reporter = new SlackReporter(_settings, new SlackAPI());
_logMessageHandler = new LogMessageHandler(_settings, _reporter);
Application.logMessageReceived += _logMessageHandler.HandleLog;

if (_settings.lifespanSec > 0)
{
StartCoroutine(Lifespan(_settings.lifespanSec));
Expand Down
9 changes: 9 additions & 0 deletions Tests/Runtime/TestDoubles/StubClickAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@

namespace DeNA.Anjin.TestDoubles
{
/// <summary>
/// A test double for agent. This agent immediately click game objects that have the name specified
/// </summary>
public class StubClickAgent : AbstractAgent
{
/// <summary>
/// A name of game objects to click
/// </summary>
[SerializeField] public string targetName;


/// <inheritdoc />
public override UniTask Run(CancellationToken token)
{
foreach (var obj in FindObjectsByType<GameObject>(FindObjectsSortMode.None))
Expand All @@ -33,6 +41,7 @@ public override UniTask Run(CancellationToken token)

handler.OnPointerClick(new PointerEventData(EventSystem.current));
}

return UniTask.CompletedTask;
}
}
Expand Down

0 comments on commit eb4e90c

Please sign in to comment.