Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 55d5df4

Browse files
Merge pull request #28 from ParticularLabs/catch-file-exceptions
Catch exceptions when file parsing fails
2 parents 97c3b30 + 386d188 commit 55d5df4

1 file changed

Lines changed: 40 additions & 20 deletions

File tree

src/NServiceBus.FileBasedRouting/FileBasedRoutingFeature.cs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,45 @@ protected override void Setup(FeatureConfigurationContext context)
6767

6868
static void UpdateRoutingTable(XmlRoutingFileParser routingFileParser, XmlRoutingFileAccess routingFile, UnicastRoutingTable routingTable, UnicastSubscriberTable subscriberTable, bool nativeSends, bool nativePublishes)
6969
{
70-
var endpoints = routingFileParser.Parse(routingFile.Read());
70+
try
71+
{
72+
var endpoints = routingFileParser.Parse(routingFile.Read());
7173

72-
var commandRoutes = new List<RouteTableEntry>();
73-
var eventRoutes = new List<RouteTableEntry>();
74+
var commandRoutes = new List<RouteTableEntry>();
75+
var eventRoutes = new List<RouteTableEntry>();
7476

75-
foreach (var endpoint in endpoints)
76-
{
77-
var route = UnicastRoute.CreateFromEndpointName(endpoint.LogicalEndpointName);
78-
foreach (var commandType in endpoint.Commands)
77+
foreach (var endpoint in endpoints)
7978
{
80-
if (nativeSends)
79+
var route = UnicastRoute.CreateFromEndpointName(endpoint.LogicalEndpointName);
80+
foreach (var commandType in endpoint.Commands)
8181
{
82-
log.Warn($"Selected transport uses native command routing. Route for {commandType.FullName} to {endpoint.LogicalEndpointName} configured in {routingFile.FileUri} will be ignored.");
82+
if (nativeSends)
83+
{
84+
log.Warn($"Selected transport uses native command routing. Route for {commandType.FullName} to {endpoint.LogicalEndpointName} configured in {routingFile.FileUri} will be ignored.");
85+
}
86+
commandRoutes.Add(new RouteTableEntry(commandType, route));
8387
}
84-
commandRoutes.Add(new RouteTableEntry(commandType, route));
85-
}
8688

87-
foreach (var eventType in endpoint.Events)
88-
{
89-
if (nativePublishes)
89+
foreach (var eventType in endpoint.Events)
9090
{
91-
log.Warn($"Selected transport uses native event routing. Route for {eventType.FullName} to {endpoint.LogicalEndpointName} configured in {routingFile.FileUri} will be ignored.");
91+
if (nativePublishes)
92+
{
93+
log.Warn($"Selected transport uses native event routing. Route for {eventType.FullName} to {endpoint.LogicalEndpointName} configured in {routingFile.FileUri} will be ignored.");
94+
}
95+
eventRoutes.Add(new RouteTableEntry(eventType, route));
9296
}
93-
eventRoutes.Add(new RouteTableEntry(eventType, route));
9497
}
95-
}
9698

97-
routingTable.AddOrReplaceRoutes("FileBasedRouting", commandRoutes);
98-
subscriberTable.AddOrReplaceRoutes("FileBasedRouting", eventRoutes);
99+
routingTable.AddOrReplaceRoutes("FileBasedRouting", commandRoutes);
100+
subscriberTable.AddOrReplaceRoutes("FileBasedRouting", eventRoutes);
101+
102+
log.Debug($"Updated routing information from {routingFile.FileUri}");
103+
}
104+
catch (Exception e)
105+
{
106+
log.Error($"Failed to update routing information from {routingFile.FileUri}. The last valid routing configuration will be used instead.", e);
107+
throw;
108+
}
99109
}
100110

101111
class UpdateRoutingTask : FeatureStartupTask, IDisposable
@@ -112,7 +122,17 @@ public UpdateRoutingTask(Action updateRoutingCallback, TimeSpan routeFileUpdateI
112122

113123
protected override Task OnStart(IMessageSession session)
114124
{
115-
updateTimer = new Timer(state => updateRoutingCallback(), null, routeFileUpdateInterval, routeFileUpdateInterval);
125+
updateTimer = new Timer(state =>
126+
{
127+
try
128+
{
129+
updateRoutingCallback();
130+
}
131+
catch (Exception)
132+
{
133+
// ignore exceptions to prevent endpoint crashes
134+
}
135+
}, null, routeFileUpdateInterval, routeFileUpdateInterval);
116136

117137
return Task.CompletedTask;
118138
}

0 commit comments

Comments
 (0)