Skip to content

Commit

Permalink
Improve logging and error handling in DropIn services
Browse files Browse the repository at this point in the history
Removed logging from DropIn.Unconfigure method and added
ILogger dependency to DropInDirectoryMonitorHostedService.
Updated csproj to include Microsoft.Extensions.Logging.Abstractions.
Enhanced error handling in UnloadDropInAssemblyAsync method.
Minor formatting improvements for readability.
  • Loading branch information
RenatoCapelo committed Sep 5, 2024
1 parent 2efdbe2 commit 3e4b7f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
12 changes: 2 additions & 10 deletions samples/drop-ins/SampleDropIn/DropIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ public async ValueTask ConfigureAsync(IServiceProvider serviceProvider, Cancella

public void Unconfigure(IServiceProvider serviceProvider)
{
var logger = serviceProvider.GetRequiredService<ILogger<DropIn>>();
try
{
var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>();
activityRegistry.Remove(typeof(ActivityRegistry), activityRegistry.Find<SampleActivity>()!);
logger.LogInformation("Drop-in unconfigured.");
}catch(Exception ex)
{
logger.LogError(ex, "Error unconfiguring drop-in.");
}
var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>();
activityRegistry.Remove(typeof(ActivityRegistry), activityRegistry.Find<SampleActivity>()!);
}
}
6 changes: 4 additions & 2 deletions src/common/Elsa.DropIns/Elsa.DropIns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="NuGet.Packaging" />
<PackageReference Include="NuGet.Protocol" />
<PackageReference Include="System.Formats.Asn1" />
<PackageReference Include="ThrottleDebounce" />
</ItemGroup>

<!--Overridden for vulnerability reasons with dependencies referencing older versions.-->
<!--Overridden
for vulnerability reasons with dependencies referencing older versions.-->
<ItemGroup>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Elsa.DropIns.Core\Elsa.DropIns.Core.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Elsa.DropIns.Core;
using Elsa.DropIns.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ThrottleDebounce;

Expand All @@ -13,6 +14,7 @@ namespace Elsa.DropIns.HostedServices;
public class DropInDirectoryMonitorHostedService : BackgroundService
{
private readonly IOptions<DropInOptions> _options;
private readonly ILogger<DropInDirectoryMonitorHostedService> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly RateLimitedFunc<string, Task> _debouncedLoader;
private readonly RateLimitedFunc<string, Task> _debouncedUnloader;
Expand All @@ -22,9 +24,14 @@ public class DropInDirectoryMonitorHostedService : BackgroundService


/// <inheritdoc />
public DropInDirectoryMonitorHostedService(IOptions<DropInOptions> options, IServiceProvider serviceProvider)
public DropInDirectoryMonitorHostedService(
IOptions<DropInOptions> options,
IServiceProvider serviceProvider,
ILogger<DropInDirectoryMonitorHostedService> logger
)
{
_options = options;
_logger = logger;
_serviceProvider = serviceProvider;
_debouncedLoader = Debouncer.Debounce<string, Task>(LoadDropInAssemblyAsync, TimeSpan.FromSeconds(2));
_debouncedUnloader = Debouncer.Debounce<string, Task>(UnloadDropInAssemblyAsync, TimeSpan.FromSeconds(2));
Expand Down Expand Up @@ -82,7 +89,7 @@ private async Task LoadDropInAssemblyAsync(string fullPath)
foreach (var dropInDescriptor in dropInDescriptors)
{
var dropIn = (IDropIn)Activator.CreateInstance(dropInDescriptor.Type)!;
if(_installedDropIns.TryGetValue(fullPath, out var installedDropIns))
if (_installedDropIns.TryGetValue(fullPath, out var installedDropIns))
{
installedDropIns.Add(dropIn);
}
Expand All @@ -98,7 +105,17 @@ private Task UnloadDropInAssemblyAsync(string fullPath)
{
if (_installedDropIns.TryGetValue(fullPath, out var installedDropIn))
{
installedDropIn.ForEach(dropIn => dropIn.Unconfigure(_serviceProvider));
installedDropIn.ForEach(dropIn =>
{
try
{
dropIn.Unconfigure(_serviceProvider);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error unconfiguring drop-in {DropIn}", dropIn.GetType().Name);
}
});
_installedDropIns.Remove(fullPath);
}
return Task.CompletedTask;
Expand Down

0 comments on commit 3e4b7f9

Please sign in to comment.