Additional model properties

This commit is contained in:
Cameron Cowen 2021-05-28 22:13:51 +01:00
parent f498184eca
commit 7893d6765a
5 changed files with 37 additions and 16 deletions

View file

@ -1,3 +1,4 @@
using Elsa.Activities.File.Models;
using Elsa.ActivityResults;
using Elsa.Attributes;
using Elsa.Services;
@ -29,11 +30,17 @@ namespace Elsa.Activities.File
public string Directory { get; set; }
public string FileName { get; set; }
public string Pattern { get; set; }
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
return base.OnExecute(context);
}
public async override ValueTask<IActivityExecutionResult> ExecuteAsync(ActivityExecutionContext context)
{
_logger.LogInformation($"Directory={Directory}");
var input = context.GetInput<FileSystemChanged>();
_logger.LogInformation($"Directory={input.Directory}");
return Done();
}
}

View file

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Elsa.Activities.File.Bookmarks
{
public record FileSystemChangedBookmark(WatcherChangeTypes ChangeType, string Directory) : IBookmark
public record FileSystemChangedBookmark(WatcherChangeTypes ChangeType, string Directory, string Pattern) : IBookmark
{ }
public class FileSystemChangedBookmarkProvider : BookmarkProvider<FileSystemChangedBookmark, WatchDirectory>
@ -17,7 +17,8 @@ namespace Elsa.Activities.File.Bookmarks
{
new FileSystemChangedBookmark(
ChangeType: await context.Activity.GetPropertyValueAsync(x => x.ChangeType),
Directory: await context.Activity.GetPropertyValueAsync(x => x.Directory))
Directory: await context.Activity.GetPropertyValueAsync(x => x.Directory),
Pattern: await context.Activity.GetPropertyValueAsync(x => x.Pattern))
};
}
}

View file

@ -17,14 +17,24 @@ namespace Elsa.Activities.File.Extensions
public static async Task TriggerFileSystemChangedWorkflowsAsync(this IWorkflowDispatcher workflowDispatcher,
string directory,
string pattern,
WatcherChangeTypes changeType,
string fileName,
string fullPath,
CancellationToken cancellationToken = default)
{
var input = new FileSystemChanged(changeType, directory, fileName, fullPath);
var bookmark = new FileSystemChangedBookmark(changeType, directory);
var trigger = new FileSystemChangedBookmark(changeType, directory);
var input = new FileSystemChanged(changeType, directory, fileName, fullPath)
{
ChangeType = changeType,
Directory = directory,
FileName = fileName,
FullPath = fullPath,
Pattern = pattern,
TimeStamp = DateTime.Now
};
var bookmark = new FileSystemChangedBookmark(changeType, directory, pattern);
var trigger = new FileSystemChangedBookmark(changeType, directory, pattern);
await workflowDispatcher.DispatchAsync(new TriggerWorkflowsRequest(_activityType, bookmark, trigger, input), cancellationToken);
}
}

View file

@ -27,5 +27,9 @@ namespace Elsa.Activities.File.Models
public string FileName { get; set; }
public string FullPath { get; set; }
public string Pattern { get; set; }
public DateTime TimeStamp { get; set; }
}
}

View file

@ -17,22 +17,22 @@ namespace Elsa.Activities.File.Services
{
private FileSystemWatcher _watcher;
private ILogger _logger;
private IOptions<FileWatcherOptions> _options;
private FileWatcherOptions _options;
private IWorkflowDispatcher _workflowDispatcher;
public FileWatcherService(ILogger<FileWatcherService> logger, IOptions<FileWatcherOptions> options, IWorkflowDispatcher workflowRunner)
{
_logger = logger;
_options = options;
_options = options.Value;
_workflowDispatcher = workflowRunner;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting service");
if (!Directory.Exists(_options.Value.Path))
if (!Directory.Exists(_options.Path))
{
_logger.LogError($"Folder to watch does not exist: {_options.Value.Path}");
_logger.LogError($"Folder to watch does not exist: {_options.Path}");
return Task.CompletedTask;
}
@ -40,9 +40,9 @@ namespace Elsa.Activities.File.Services
{
NotifyFilter = NotifyFilters.FileName
| NotifyFilters.DirectoryName,
Filter = _options.Value.Pattern,
Path = _options.Value.Path,
IncludeSubdirectories = _options.Value.SubDirectories
Filter = _options.Pattern,
Path = _options.Path,
IncludeSubdirectories = _options.SubDirectories
};
_watcher.Created += Watcher_Created;
_watcher.EnableRaisingEvents = true;
@ -70,8 +70,7 @@ namespace Elsa.Activities.File.Services
private async void Watcher_Created(object sender, FileSystemEventArgs e)
{
_logger.LogInformation($"File created: {e.FullPath}-{e.ChangeType}");
await _workflowDispatcher.TriggerFileSystemChangedWorkflowsAsync(_watcher.Path, e.ChangeType, e.Name, e.FullPath);
await _workflowDispatcher.TriggerFileSystemChangedWorkflowsAsync(_options.Path, _options.Pattern, e.ChangeType, e.Name, e.FullPath);
}
}
}