Skip to content

Commit

Permalink
Use filename pattern for watchign for only configured files and honou…
Browse files Browse the repository at this point in the history
…r timezone configuration
  • Loading branch information
elzik committed Sep 15, 2023
1 parent 4bf9f67 commit 842ffba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
28 changes: 21 additions & 7 deletions src/Elzik.FmSync.Worker/FmSyncWorker.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
using Elzik.FmSync.Infrastructure;
using Microsoft.Extensions.Options;

namespace Elzik.FmSync.Worker
{
public class FmSyncWorker : BackgroundService
{
private readonly ILogger<FmSyncWorker> _logger;
private readonly FmSyncOptions _fmSyncOptions;
private readonly FileSystemOptions _fileSystemOptions;
private readonly IFrontMatterFileSynchroniser _fileSynchroniser;
private readonly List<FileSystemWatcher> _folderWatchers;

public FmSyncWorker(ILogger<FmSyncWorker> logger,
FmSyncOptions fmSyncOptions, IFrontMatterFileSynchroniser fileSynchroniser)
public FmSyncWorker(ILogger<FmSyncWorker> logger, IOptions<FmSyncOptions> fmSyncOptions,
IFrontMatterFileSynchroniser fileSynchroniser, IOptions<FileSystemOptions> fileSystemOptions)
{
_logger = logger;
_fmSyncOptions = fmSyncOptions;
_fileSynchroniser = fileSynchroniser;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
if (fmSyncOptions == null)
{
throw new ArgumentNullException(nameof(fmSyncOptions));
}
if (fileSystemOptions == null)
{
throw new ArgumentNullException(nameof(fileSystemOptions));
}
_fileSystemOptions = fileSystemOptions.Value;
_fmSyncOptions = fmSyncOptions.Value;
_fileSynchroniser = fileSynchroniser ?? throw new ArgumentNullException(nameof(fileSynchroniser));
_folderWatchers = new List<FileSystemWatcher>();
}

Expand All @@ -23,9 +36,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
foreach (var directoryPaths in _fmSyncOptions.WatchedDirectoryPaths)
{
_logger.LogInformation("Configuring watcher on {DirectoryPath} for new and changed " +
"MarkDown files.", directoryPaths);
"{FilenamePattern} files.", directoryPaths, _fileSystemOptions.FilenamePattern);

var folderWatcher = new FileSystemWatcher(directoryPaths, "*.md")
var folderWatcher = new FileSystemWatcher(directoryPaths,
_fileSystemOptions.FilenamePattern ?? string.Empty)
{
EnableRaisingEvents = true,
IncludeSubdirectories = true,
Expand Down
9 changes: 3 additions & 6 deletions src/Elzik.FmSync.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
var configuration = hostContext.Configuration;

const string optionsKey = "FmSyncOptions";
var options = configuration.GetSection(optionsKey).Get<FmSyncOptions>() ?? throw new InvalidOperationException(
$"{optionsKey} configuration could not be found.");
services.AddSingleton(options);
services.AddSingleton<IMarkdownFrontMatter, MarkdownFrontMatter>();
services.AddSingleton<IFile, FileAdapter>();
services.AddSingleton<IFrontMatterFileSynchroniser, FrontMatterFileSynchroniser>();
services.Configure<FmSyncOptions>(hostContext.Configuration.GetSection("FmSyncOptions"));
services.Configure<FileSystemOptions>(hostContext.Configuration.GetSection("FileSystemOptions"));
services.Configure<FrontMatterOptions>(hostContext.Configuration.GetSection("FrontMatterOptions"));
services.AddHostedService<FmSyncWorker>();
})
.ConfigureAppConfiguration((_, config) =>
Expand Down
8 changes: 7 additions & 1 deletion src/Elzik.FmSync.Worker/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
},
"FmSyncOptions": {
"WatchedDirectoryPaths": [ "C:\\TempObsidian" ]
}
},
"FileSystemOptions": {
"FilenamePattern": "*.md"
},
"FrontMatterOptions": {
"TimeZoneId": ""
}
}

0 comments on commit 842ffba

Please sign in to comment.