diff --git a/src/activities/Elsa.Activities.File/Services/FileSystemWatchersStarter.cs b/src/activities/Elsa.Activities.File/Services/FileSystemWatchersStarter.cs index ba1886487f..cb0ac0c3e8 100644 --- a/src/activities/Elsa.Activities.File/Services/FileSystemWatchersStarter.cs +++ b/src/activities/Elsa.Activities.File/Services/FileSystemWatchersStarter.cs @@ -47,6 +47,7 @@ public async Task CreateAndAddWatchersAsync(CancellationToken cancellationToken using var scope = _scopeFactory.CreateScope(); var triggerFinder = scope.ServiceProvider.GetRequiredService(); + var triggerRemover = scope.ServiceProvider.GetRequiredService(); await triggerFinder.FindTriggersAsync(cancellationToken: cancellationToken); var triggers = await triggerFinder.FindTriggersByTypeAsync(cancellationToken: cancellationToken); @@ -59,7 +60,30 @@ public async Task CreateAndAddWatchersAsync(CancellationToken cancellationToken var notifyFilters = bookmark.NotifyFilters; var path = bookmark.Path; var pattern = bookmark.Pattern; - CreateAndAddWatcher(path, pattern, changeTypes, notifyFilters); + try + { + CreateAndAddWatcher(path, pattern, changeTypes, notifyFilters); + } + catch (IOException ex) + { + _logger.LogWarning(ex, + $"Watcher with path \"{path}\" and pattern \"{pattern}\" causes IOException. Removing Trigger.", + path, + pattern, + changeTypes, + notifyFilters); + await triggerRemover.RemoveTriggerAsync(trigger); + } + catch (ArgumentException ex) + { + _logger.LogWarning(ex, + $"Watcher with path \"{path}\" and pattern \"{pattern}\" is not valid. Removing Trigger.", + path, + pattern, + changeTypes, + notifyFilters); + await triggerRemover.RemoveTriggerAsync(trigger); + } } } finally diff --git a/src/core/Elsa.Abstractions/Services/Triggers/ITriggerRemover.cs b/src/core/Elsa.Abstractions/Services/Triggers/ITriggerRemover.cs new file mode 100644 index 0000000000..af46da48f0 --- /dev/null +++ b/src/core/Elsa.Abstractions/Services/Triggers/ITriggerRemover.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Elsa.Models; + +namespace Elsa.Services +{ + public interface ITriggerRemover + { + Task RemoveTriggerAsync(Trigger trigger); + } +} \ No newline at end of file diff --git a/src/core/Elsa.Core/Extensions/ElsaServiceCollectionExtensions.cs b/src/core/Elsa.Core/Extensions/ElsaServiceCollectionExtensions.cs index 32af245c72..677c60b969 100644 --- a/src/core/Elsa.Core/Extensions/ElsaServiceCollectionExtensions.cs +++ b/src/core/Elsa.Core/Extensions/ElsaServiceCollectionExtensions.cs @@ -245,6 +245,7 @@ private static ElsaOptionsBuilder AddWorkflowsCore(this ElsaOptionsBuilder elsaO .AddScoped() .AddTransient() .AddScoped() + .AddScoped() .AddBookmarkProvider() .AddBookmarkProvider(); diff --git a/src/core/Elsa.Core/Services/Triggers/TriggerRemover.cs b/src/core/Elsa.Core/Services/Triggers/TriggerRemover.cs new file mode 100644 index 0000000000..0d31c21086 --- /dev/null +++ b/src/core/Elsa.Core/Services/Triggers/TriggerRemover.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Elsa.Models; +using Elsa.Persistence; + +namespace Elsa.Services.Triggers +{ + public class TriggerRemover: ITriggerRemover + { + private readonly ITriggerStore _triggerStore; + + public TriggerRemover(ITriggerStore triggerStore) + { + _triggerStore = triggerStore; + } + + public async Task RemoveTriggerAsync(Trigger trigger) + { + await _triggerStore.DeleteAsync(trigger); + } + } +} \ No newline at end of file