Skip to content

Commit

Permalink
Stop DeadLetterListener on terminate if LogDeadLettersDuringShutdown …
Browse files Browse the repository at this point in the history
…is disabled (#4042)
  • Loading branch information
ismaelhamed authored and Aaronontheweb committed Nov 20, 2019
1 parent 42cfae6 commit 07e9ad8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2878,6 +2878,7 @@ namespace Akka.Event
public DeadLetterListener() { }
protected override void PostRestart(System.Exception reason) { }
protected override void PostStop() { }
protected override void PreRestart(System.Exception reason, object message) { }
protected override void PreStart() { }
protected override bool Receive(object message) { }
}
Expand Down
20 changes: 20 additions & 0 deletions src/core/Akka.Tests/Actor/ActorSystemSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ public void Allow_valid_names()
.Terminate();
}

[Fact]
public void Log_dead_letters()
{
var sys = ActorSystem.Create("LogDeadLetters", ConfigurationFactory.ParseString("akka.loglevel=INFO")
.WithFallback(DefaultConfig));

try
{
var a = sys.ActorOf(Props.Create<Terminater>());

var eventFilter = new EventFilterFactory(new TestKit.Xunit2.TestKit(sys));
eventFilter.Info(contains: "not delivered").Expect(1, () =>
{
a.Tell("run");
a.Tell("boom");
});
}
finally { Shutdown(sys); }
}

[Fact]
public void Block_until_exit()
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka/Actor/Internal/ActorSystemImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ public override void RegisterOnTermination(Action code)
public override Task Terminate()
{
Log.Debug("System shutdown initiated");
if (!Settings.LogDeadLettersDuringShutdown && _logDeadLetterListener != null)
Stop(_logDeadLetterListener);
_provider.Guardian.Stop();
return WhenTerminated;
}
Expand Down
23 changes: 13 additions & 10 deletions src/core/Akka/Event/DeadLetterListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ public class DeadLetterListener : ActorBase
private int _count;

/// <summary>
/// TBD
/// Don't re-subscribe, skip call to preStart
/// </summary>
/// <param name="reason">TBD</param>
protected override void PostRestart(Exception reason)
{
}

/// <summary>
/// Don't remove subscription, skip call to postStop, no children to stop
/// </summary>
protected override void PreRestart(Exception reason, object message)
{
}

/// <summary>
/// TBD
/// </summary>
Expand Down Expand Up @@ -55,7 +61,7 @@ protected override bool Receive(object message)
var rcp = deadLetter.Recipient;

_count++;

var done = _maxCount != int.MaxValue && _count >= _maxCount;
var doneMsg = done ? ", no more dead letters will be logged" : "";

Expand All @@ -65,15 +71,12 @@ protected override bool Receive(object message)
var sndPath = snd == ActorRefs.NoSender ? "NoSender" : snd.Path.ToString();

_eventStream.Publish(new Info(rcpPath, rcp.GetType(),
string.Format("Message {0} from {1} to {2} was not delivered. {3} dead letters encountered.{4}",
deadLetter.Message.GetType().Name, sndPath, rcpPath, _count, doneMsg)));
}

if (done)
{
((IInternalActorRef) Self).Stop();
$"Message [{deadLetter.Message.GetType().Name}] from {sndPath} to {rcpPath} was not delivered. [{_count}] dead letters encountered {doneMsg}." +
"This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' " +
"and 'akka.log-dead-letters-during-shutdown'."));
}

if (done) Context.Stop(Self);
return true;
}
}
Expand Down

0 comments on commit 07e9ad8

Please sign in to comment.