Skip to content

Commit

Permalink
Remove triggers that have a malformed path (elsa-workflows#3121)
Browse files Browse the repository at this point in the history
* create a check to see if the path is valid before creating the watcher. This is so the file system watcher does not try to create directory names that are malformed.

* remove test code.

* add a way to remove a trigger. Used by the file watcher starter service if the path for the file to watch is invalid.

Co-authored-by: Adam S <[email protected]>
  • Loading branch information
AdamJohnSwan and AdamJohnSwan authored Jun 13, 2022
1 parent 1929a7f commit 9006904
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task CreateAndAddWatchersAsync(CancellationToken cancellationToken

using var scope = _scopeFactory.CreateScope();
var triggerFinder = scope.ServiceProvider.GetRequiredService<ITriggerFinder>();
var triggerRemover = scope.ServiceProvider.GetRequiredService<ITriggerRemover>();
await triggerFinder.FindTriggersAsync<WatchDirectory>(cancellationToken: cancellationToken);

var triggers = await triggerFinder.FindTriggersByTypeAsync<FileSystemEventBookmark>(cancellationToken: cancellationToken);
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/core/Elsa.Abstractions/Services/Triggers/ITriggerRemover.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ private static ElsaOptionsBuilder AddWorkflowsCore(this ElsaOptionsBuilder elsaO
.AddScoped<IGetsTriggersForWorkflowBlueprints, TriggersForBlueprintsProvider>()
.AddTransient<IGetsTriggersForActivityBlueprintAndWorkflow, TriggersForActivityBlueprintAndWorkflowProvider>()
.AddScoped<ITriggerFinder, TriggerFinder>()
.AddScoped<ITriggerRemover, TriggerRemover>()
.AddBookmarkProvider<SignalReceivedBookmarkProvider>()
.AddBookmarkProvider<RunWorkflowBookmarkProvider>();

Expand Down
24 changes: 24 additions & 0 deletions src/core/Elsa.Core/Services/Triggers/TriggerRemover.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 9006904

Please sign in to comment.